Vim does not need to be introduced. Due to its steep learning curve, it is often considered the bane of new Linux users. But once you get the hang of it, there’s no denying, that it’s a very powerful tool. In this post I would like to share some Vim tips, that I’ve learned over the years, that will improve your productivity. It is not aimed at Vim beginners. If you are just starting out with Vim, I recommend the excellent Byte of Vim by Swaroop C H.
Save file without sudo
I think we’ve all been in this situation – you make edits to some system configuration and when you try to save them, you realize you don’t have sufficient privileges. No need to worry, you can save it with sudo straight from Vim, using this command:
:w !sudo tee “%” > /dev/null
Since I find myself myself in such situations a lot, I have mapped it to
:W command in my vimrc:
user@computer$ echo ‘command W w !sudo tee % > /dev/null’ >> .vimrc
Paste code from clipboard
Here’s another model situation. You paste code from the clipboard, but it inevitably ends up indented off the screen. The reason for that is the ex line editor, which runs at the heart of Vim. So Vim pastes code not as a block, but line by line, applying the indenting rules in the process. Fixing it manually can be quite a pain. But luckily, the indentation can be temporarily turned off. Just enter:
After pasting the code, it will get automatically unset. If you ever need to turn it of manually, just run:
Comment out blocks of text
Staying with code, there are times when you need to temporarily comment out blocks of it. Instead of commenting out line by line, you can select the whole block in visual block mode. Switch into it using
ctrl+v key combination. When you have the block selected, press
I to enter insert mode and type
#. Press escape and the block should get commented out. The whole sequence is as follows:
select block of code
To uncomment the block again, just select the line of pound characters in visual block mode and press x.
Strip trailing whitespace
Trailing whitespace can often mess up your commits, so it is a good practice to get rid of it. You can just write a simple substitute function:
exe “normal mz”
exe “normal `z”
And since doing things manually just leads to forgetting them, run it automatically when saving a file. Add the following snippet to your vimrc:
autocmd BufWrite :call StripTrailingWhitespace()
Word and character count
While Vim does not show character or word count in status line, it is easy enough to find it out. Just press
g followed by
ctrl+g. All the information will be displayed in the status line.
Insert newline without entering insert mode
When formatting files, I often want to add a blank line. However, doing it with
O enters insert mode, which I have to promptly escape from. Therefore, I have added a mappings
]o to add newline while staying in normal mode. You can add them to vimrc like that or change them to any key combination you prefer.
nmap [O O
nmap ]o o
Escape normal mode
I don’t know about you, by I am not fond of stretching my hands all the way to press Esc. You can avoid hand sore by mapping it to some key combination, that is not frequently used. I prefer
jj. Just add this to your vimrc:
These are some of the tricks that I use daily. My vimrc is publicly available in my dotfiles repository, so you can check it out. Also, see my post about managing your Vim plugins. And if you have any Vim tips to share, leave a comment below the article.