Vim tips

5 Vim tips for advanced users

This blog post lists out my top five Vim tips and tricks to bring your Vim game to the next level.

Here at Dogsbody Technology we are connected to servers, editing files and improving configs every day. All of this means that a good text editor is a must. That is why we chose Vim.

I assume you know the basics, If you don’t I recommend starting here.

Tip 1: Use Vim as a file browser

Have you ever accidentally opened a directory in vim? if you had, you would be pleasantly surprised that it creates a nice interface to find and start editing the file you meant.

Well you can get to that menu another way:

:Explore

From this menu you can use your arrow keys to select and open up new files for editing.

Of course if you already know the file name, just open it directly:

:open filename

Phew I was worried I might need to leave vim for a second then.

Tip 2: Try out multi-line inserts

*tap**tap**tap**tap**tap**tap**tap* the sound of a colleague commenting out a code block. Slow, noisy. Vim has a much quicker approach using visual blocks:

ctrl + v # Enter visual block mode and using your arrow keys select all of the lines you wish to edit
ctrl + shift + i # Enter insert mode
"Example text entry"
Esc # Stop entering text

A very similar method can be used to append text to lines.

ctrl + v 
end # Move the cursor to the end of the line - This works even if the line endings are different lengths 
ctrl + shift + a # Enter append insert mode
"Example text entry"
Esc

You can also use visual block mode to quickly delete a large area of text. Just select it and press “d”.

Tip 3: Make the most of macros

Macros are incredibly simple and just as powerful. They are a sequence of Vim inputs and commands that you can re-run at the touch of a button.

Here’s how to start recording your custom macro:

q # Start recording
j # or any other character you want to trigger it
# any collection of commands you want
q # Stop recording

Then to run the macro:

# To run the "j" macro
@
j

To quickly re-run the last macro, you can double tap “@” or just hold it.

Simple to learn, but when you combine it with other commands, you can quickly build something amazing.

For example – I had a JSON blob containing a list of different SSH keys multiple times. I needed to delete the second key that matched the word “frank”. Quickly creating a macro that runs a search, go to next line, delete. Manually this job would have taken half an hour, with Macros, just five mins.

Tip 4: All the things she ‘sed’

…running through my head

Vim has an inbuilt stream editor, this enables you to modify lines in a quick and replicable manner. But did you know that it comes with regex support out of the box?

Lets look at this command:

:% s/\(wibble\)/ --> \1 <-- /g
:g/[Ww]ibble/d

The first command finds the word “wibble” and puts two big arrows around it, not very useful but a good example:

  • The “%” at the beginning tells it to match all lines. Alternatively you could put in specific line numbers (e.x. 5,13 – match lines five to thirteen)
  • “s/$input/$output/g” start a text substitution. Ending g means match multiple times on one line.
  • \(wibble\) create a capture group looking for the word “wibble”.  Notice that the braces need escaping, unlike normal regex.
  • \1 return the contents of capture group one

The second command uses a slightly different function. It deletes all lines with the word wibble in them, matching both with an without a capital letter W.

If you need to brush up on your regex, we find the regexr site an indispensable resource.

Tip 5: Escaping

If you have ever seen this error “E45: 'readonly' option is set” then you have probably seen this work around…

:w ! sudo tee %

But did you know you can expand this to use any bash commands?

:w ! sort | uniq | tee %

These commands sort the file, then remove any duplicate lines before using tee to write the output back into your vim file.

That concludes my top five Vim tips. Vim is an astonishingly advanced tool that rewards you the further you dig into it.

Did you learn something new? Do you have your own Vim tips to share? Leave us a message in the comments below!

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *