photo of man playing skateboard

Tips: Github CLI and more Vim text-object and file tricks


OK, it’s taken a while, but in the spirit of typing rather than clicking, here are some notes about how to do all the common things with…

Catching up on dependable pull requests. If you are like me and are getting pull request automatically, here is all you have to do, the only confusing thing is that you need the pull request number or a branch name that you are merging in.

gh pr view
# you will get a set of pull request numbers for instance #16, #17 and #18
# the -r flag says rebase the pull request into the default branch
# normally main (or master in the old days)
# -d means delete the PR when done
gh pr merge 16 -r -d
# if you want it to ask interactively about the flags
gh pr merge 17

If you want to fork a repo into an organization that you control note that since this is github specific, you don’t need or the full url, just organization/repo

# to fork the px4-autopilot repo into tong family/px4-autopilot
gh repo fork px4/px4-autopilot --org tongfamily

To create a new repo and the you can add it as a submodule or you can just clone it

# if you want interactive prompts
gh repo create tongfamily/net-px4
# if you know what you want, you can just use flags
gh repo create tongfamily/net-px4 --private --team dev --description "PX4" -g python

Vim motion command and text-objects

Well, that was awesome, but perhaps even more cool is what you can do with the latest VIM and what are called “text-objects”, the documentation is nearly unreadable, but the basic idea is that the old vi had this notion of objects, so if you wanted to deleted an entire word where the cursor is, you can do dw and if you want to change a word, it is cw that is the <command><motion key>.

Well, the latest vim extends this idea with two new operators call around or a and inside or i which means inside, so for instance if you want to change all the text inside a set of parenthesis, you get there and run. The basic idea is that things that are prefixed with a and i are actually “text-objects” rather than motion keys.

ci( - Change inside a parenthesis
ca( - Change everything including the parenthesis
di[ - delete everything inside the brackets there are around the cursor
da[ - delete the brackets too
cas - change everything around the current sentence

Now the really cool thing is that it has the notion of paragraphs (which are things with a blank line) so and like all vim commands, the complete syntax allows a repeat so it is <action> <number of repetitions> <a for around | I for inside> <text-object like (, [, s, p>

c2ap - change for the next 2 paragraphs

And yes they have a very complete list of text objects, just remember for this to work, you have to be inside these objects, so it is helpful to first move there, so you could do say f[ to find the first bracket and then do a ci[ which would change all the text inside of it.

aw - around a word which stops at non-alphanumeric characters like slashes
iw - inside a word 
aW - around a Word which means to the next white space (so an entire URL) 
iW - in a Word delimited by white space
as - around a sentence
is - inside a sentence
ap - around a paragraph (newline separated include white space)
a' - single quote include the quotes
i' - single quotes not including quotes
a" - double quotes (include the quotes)
i" - inside double quotes (leave them there)
a> - around angle brackets (include them), note that a< works too
i> - inside angle brackets
at - around an HTML tag
it - inside an HTML tag
a( - around parenthesis, not that a) and ab also work
i( - inside parentheses, note that that i) and ib also work
a{ - around braces (includes the braces). note that aB works
i{ - inside braces not that i} and iB also work

So some motion commands

f - find to a character landing the cursor on top of it
F - do this searching backwards
t - find a character landing just before the character
T - this is search to going backwards
^ - first character in the line
$ - last character
10| - the vertical bar means go to the 10th character 
w - forward a word delimited non-alphanumerics
W - forward a word delimited by white space
e - go back one word
E - go back a word until the next white space
) - move to the next sentence which is text that ends in ., ! or ? and a space
( - go back a sentence
} - go forward a paragraph which is delimited by a blank line
{ - got backward a paragraph

Finally, if you are really writing paragraphs remember that gqq does a forced word wrap of a paragraph which is really a more general form of the go quote command which takes a motion, so that gqG for example means go format all the way to the end of the document with a G

Finally a note on visual mode. This is actually pretty logical if you think about it, most of the time when you type a one command like say dW you have no idea what running the W command will do. So with visual mode, you reverse the command sequence with vWd and then with the v, you go into highlight mode, then run the motion command, it highlights the text that will be operated on, then it takes the command.

So for instance, if you want so say delete everything to the end of a paragraph, you don’t want to be surprised, you just do a vGGd and then if the GG doesn’t select what you want, you can fix it and then run the command. Nice!

And finally if you are deep in a file list, then you can do a go file or gf and it will try to open the file that is under the cursor and you can run :find to find files.

And if you are really lost, you can use NERDTree which let’s you do directory browsing up and down with :NERDtree.

Related Posts

This site uses Akismet to reduce spam. Learn how your comment data is processed.