Skip to content
Logo Theodo

Survive Vim With these 5 Simple Tricks

Alexandre Chaintreuil5 min read

kbd { display: inline-block; padding: 3px 5px; font-size: 11px; line-height: 10px; color: #555; vertical-align: middle; background-color: #fcfcfc; border: solid 1px #ccc; border-bottom-color: #bbb; border-radius: 3px; box-shadow: inset 0 -1px 0 #bbb }

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.
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:

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

I hope this article made Vim less painful.

You can share your tips in the comments.

Liked this article?