Survive Vim with these 5 simple tricks

Posted by Alexandre on February 28, 2017

This article was origininally published on Theodo's blog


Sometimes, you have to ssh on a server to fix some configuration files or to test a feature that you cannot test locally. You don't have your favourite text editor with all your configuration here.

Most of the time, you have to rely on vim or nano in this situation.

Your coworkers told you that nano is for newbies and you want to gain some street credibility, so you use vim.

Your beard is not long enough for emacs. </br> You wouldn't be here anyway

Here is a guide to help you solve the annoying problems that you may face with vim.

When you forget to sudo

Your server has a configuration issue, and you have to fix the configuration file, so you go vim <path-to-your-config-file>.

You make a bunch of changes, Esc:x to save the file and exit and then:

E505: "<your-config-file>" is read-only (add ! to override)

You forgot about the sudo and now you think you have to drop your changes, and open vim again with sudo this time.

Fortunately, there is a solution: :w !sudo tee %

Let's analyse this command:

  • :w doesn't write in your file in this case. If you are editing file1 with vim and type :w file2, you will create a new file named file2 with your buffer and file1 will be left untouched. Here, you write to the "file" !sudo tee %.

  • !sudo: The bang ! lets you execute a command as if you were in a shell, in this case sudo to get superusers rights.

  • tee sounds like the letter T for a reason: it acts like a T-shaped pipe. It takes a filename in argument, and redirects the output to the file you gave it and to the standard output.

  • % refers to the current file in vim.

  • You now have the full equation: :w writes the buffer into tee (with superuser rights) that redirects the buffer into the current file (and to the standard output, but that's not useful here)

Pastemode

What's more frustrating than pasting a snippet of code and having your formatting all messed up, especially in languages like Python where indentation is part of your code?

Example:

You can switch to paste mode which allows you to paste text as is.

Simply type :set paste in normal mode and then you're good to go (do not forget to switch to insert mode before pasting). Look carefully, everything happens on the last line of the video.

You might wonder: why not stay in paste mode all the time? Well it changes some of the configurations of vim like smarttabs and smartindent.

However, if you have a lot of copy / pasting to do, you can use set pastetoggle=<F2> to set a toggle. Pressing F2 will switch paste mode on and off. You can replace F2 with any key you like. More info with :help paste.

Searching for text

Chances are that you will look for something in the file you are trying to edit.

In normal mode simply type /<your-search>. It will highlight text that matches and you can press n to go to the next or N to go to the previous occurrence.

Notice how the text is left highlighted even after you entered insert mode? You can stop the highlighting by typing :noh as in "no highlight".

Block comment

Sometimes, you have to test your file with a part of the code removed and the quickest way to do this without losing it is to comment it.

To comment multiple lines at once, simply put the cursor on the first column by pressing 0 at the top (or bottom) of the block you want to comment and press Ctrl + v

You are now in visual block mode where you can select a bloc of text, in our case a column. Go down or up and select the lines you want to comment. Press Shift + I and insert your programming language comment symbol. It will only print on the highest row selected. Then press Esc Esc and boom, your lines are commented!

Press u to cancel the change, or simply bloc select it again and press x.

Indent

Press v to select text and then press < or > to indent in either direction. You can combine it with navigation actions. For example > G will indent all lines from the cursor to the end of the file.

However, it will indent your file of shiftwidth spaces. By default this value is 8 and that is quite big. You can use :set shiftwidth=<your-value> to adapt it to the indent style of your file.

Bonus: quick navigation

  • $ goes to end of line
  • 0 goes to the beginning of the line (column 1) while ^ goes to the first non blank character
  • gg goes to the top of the file while G goes to the bottom
  • % goes to the matching parenthesis/bracket
  • Ctrl + F goes one page down and Ctrl + B goes one page up (think forward and backward to remember it)
  • u cancels your last action and Ctrl + r reapplies it

I hope this article made Vim less painful.

You can share your tips in the comments.