Vim Tips and Tricks
Vim is funny, I’ve been using it for 25 years but it often feels like I’m only using 10% of its potential.
Youtube Video by Henry Misc
I found this video by Henry Misc, and it inspired me to write some of his tips down for myself, to remember them better.
Quick exit
I already knew about ZZ to save and exit, but I need to practice more ZQ to exit without saving, instead of :q!.
Manipulate blocks of code
Inside any type of brackets or parenthesis you can write vi followed by the opening bracket, be it [, or {, or (, it will select just the code between the opening and closing brackets.
This also goes for strings between quotation marks like ’ or “.
There are some odd shortcuts too, where you can type vib to select contents inside the first set of parenthesis anywhere in the buffer, or viB to select contents inside curly braces. But I feel that they’re pretty limited, and you have to remember more to do less.
cib will jump to the first block of opening and closing () parenthesis, replace the contents inside them and start input mode.
Prepend and append to blocks of text
I already knew you could enter visual block mode with ctrl+v, and it will only select the first character of each line. This way you can then enter I, type a series of characters to prepend to each line, and hit Esc to watch them get prepended to every line you selected.
But I never knew how to append text to the end of these lines.
You enter ctrl+v again, and while in this mode you can actually type $ to go to end of line, and shift+a to append to end of line, type the characters you want to append, Esc, and voilá.
Toggle case
I rarely need this so I never remember that you can use ~ to toggle case of a single character.
You can also capitalize en entire word, starting from the cursor, by hitting g~w.
The w in this case is just a motion after the g~ command, so you can use other motions like it for “inner-tag”, which will capitalize everything inside of a html tag.
So you can replace it in this case with the motion i', meaning everything inside a single quoted string.
All of these can be undone by repeating the same command.
Selecting text
That brings me into something the video overlooked, or took for granted, selecting text with vi.
The visual select mode v can be followed by an i, and a pattern like mentioned above, to select all text within a string, or within brackets.
There is also vip to select a paragraph, and I’m sure more that I should add here as I discover them.
So vi' will select inside a single quoted string, vi" inside a double quoted string, vi( everything between two parenthesis, and so on.
Fix indent of entire buffer
I already knew about this one but it’s super useful. When vim knows your filetype, set ft?, then you can have vim cleanup the indentation of a file, so it matches the configured filetype, by hitting gg=G.
This does two things, gg jumps to start of file, and the =G command resets the indent of the file. But it will only work if Vim has an indent configuration for your filetype.
Jump between braces
The = equal sign can jump between opening and closing braces of a code block.
Sessions
I’ve been using sessions for years, they’re super useful.
The workflow is that you open a bunch of files in vim, you’re in the middle of working, and you have to do something else. To resume your work, save everything as a vim session with :mksession! ~/.vim/sessions/project.vim. Notice I’m using :mksession! that forces, because I usually already have a session file with that name, so I want to overwrite it.
Then to resume your work just run :so ~/.vim/sessions/project.vim.
Open URLs from Vim
Now this I don’t need, because I use the foot terminal and it allows me to hit ctrl+shift+o to highlight all URLs, and open them in my browser.
But in the video Henry also does this by simply hitting gx with the cursor on a URL. It didn’t work in my Vim on Fedora, I suspect the Netrw plugin is misconfigured.
Mark location in file
The command m followed by any letter will set a mark in a file, for example ma to set the a character as mark, now you can instantly jump to this location with 'a.
You can also jump between files, but you have to use uppercase letters for your mark. For example mA will allow you to type 'A from any other open file in vim, and you will jump to the previous file.
You can see all your marks with the :marks command.
Movement
Knowing how to move around in Vim is a very underrated skill.
- gg - top of file
- G - bottom of file
- shift+h - top of window
- shift+m - middle of window
- shift+l - bottom of window
These are called “jumps”, and are actually recorded by vim, you can run :jumps to see this record.
You can also cycle through old jumps in this list with ctrl+o to jump back, and ctrl+i to go forwards, in Normal mode.
The movement keys hjkl are not jumps, so even if you do 10k to go up 10 lines, it’s not recorded as a jump.
Read :he jump to see what counts as jumps.
For example if you use gg to jump to start of file, you can then use '' to jump back to your previous location, where you were before the last jump command, and this only works for qualified jumps.
'.will jump to the last edited location.'^jumps to the last place you exited Insert mode.
Movements relating to Insert mode
- I - enter Insert mode at beginning of line
- A - enter Insert mode at end of line
Go to line
This does not count as a jump!
Normally this is done with :32, in command mode, everyone knows that.
But Henry does it by typing the line number, followed by capital G, for example 32G will go to line 32.
Join lines
You can join multiple lines into one line with the capital J character. This will automatically insert a space between each line.
Or with gJ to join lines without inserting a space between them.
Incrementing and decrementing numbers
This I knew about but I use it maybe once a year, you can increment numbers with ctrl+x and decrement with ctrl+a. And it works for large numbers too, vim understands where the number begins and ends.
Creating numbered lists
The o command can be repeated to create bullet points, or lists. For example 10o0. followed by ESC will create 10 lines starting with the string 0..
Now you can combine this with vip to select the paragraph, which would be the list you just created in this case. Then hit g and ctrl+a, to increment the numbers from 1 to 10.
Equally if you hit ctrl+x instead it will decrement the numbers and create a negative list from -1 to -10.
This is actually not magic, vim maintains line numbers inside visual selections, so in this case there are 10 line numbers in this visual selection, regardless of where in your file buffer it is. And the command combination of g ctrl+a increments the number by its line number, so the first line is incremented once, line number 1, and the 10th line is incremented 10 times. …MAGIC! 🪄
Ergonomic tips
You can use ctrl+j instead of Enter key to insert a new line.
You can use ctrl+w in Insert mode to remove the last word behind the cursor, instead of backspace. Isn’t this called emacs-style in readline? 😱 (Not sure because you can’t use ctrl+y to undo the word deletion.)
Run commands from insert mode
This is a real game-changer, you can hit ctrl+o in Insert mode to temporarily run a command in Normal mode, and immediately after you’re back in Insert mode!
That is a trick I believe I can use regularly.
Searching
The * and # characters can be used in Normal mode to search the buffer for the word your cursor is currently on, * forward, and # backwards.
Then during searching you can of course use n and shift+n to jump forward and backward between found instances of that word.
:noh will remove the highlighting from the last word you searched for.
You can also use g* or g# to search for partial matches of that word, for example in function names, or variable names, where the word is not surrounded by a “word boundary”.
Sources
TO BE CONTINUED
There are two more videos I need to watch by Henry, I suggest you go and watch them yourself.
- Vim motions & tricks I wish I learned sooner
- More Vim tricks that blew my mind
- Useless Vim tips, purely for vibes