Last updated on September 7, 2020 by Adrien Brochard
If my latest post on the topic did not tip you off, I am a Vim fan. So before some of you start stoning me, let me present you a list of obscure Vim commands. What I mean by that is: a collection of commands that you might have not encountered before, but that might be useful to you. As a second disclaimer, I do not know which commands you might know and which one you find useful. So this list really is a collection of relatively less known Vim commands, but which can still probably be useful.
I am a bit ashamed of myself for that one, but I only recently learned that the command
:x
is equivalent to:
:wq
which is saving and quitting the current file.
While in insert mode, you can press Ctrl+r
then type =
followed by a simple calculation. Press ENTER
, and the result will be inserted in the document. For example, try:
Ctrl+r '=2+2' ENTER
And 4
will be inserted in the document.
When you type something quickly, it happens that you write a word twice in a row. Just like this this. This kind of error can fool anyone, even when re-reading yourself. Hopefully, there is a simple regular expression to prevent this. Use the search (/
by default) and type:
()_s*1
This should display all the duplicate words. And for maximum effect, don't forget to place:
set hlsearch
in your ~/.vimrc
file to highlight all search hits.
Probably one of the most impressive tricks, you can define abbreviations in Vim, which will replace what you type with somethig else in real time. The syntax is the following:
:ab [abbreviation] [what to replace it with]
The generic example is:
:ab asap as soon as possible
Which will replace asap
with "as soon as possible" as you write.
This is maybe an all time favorite in the forums. Whenever you open a file that you do not have permission to write to (say a system configuration file for example) and make some changes, Vim will not save them with the normal command: :w
Instead of redoing the changes after opening it again as root, simply run:
:w !sudo tee %
Which will save it as root directly.
If you do not want someone to be able to read whatever is on your screen, Vim has the built in option to ROT13-encode your text with the following command:
ggVGg?
gg
for moving the cursor to the first line of the Vim buffer, V
for entering visual mode, and G
for moving the cursor to the last line of the buffer. So ggVG
will make the visual mode cover the entire buffer. Finally g?
applies ROT13 encoding to the selected region.
Notice that this should be mapped to a key for maximum efficiency. It also works best with alphabetical characters. And to undo it, the best is simply to use the undo command: u
Another one to be ashamed of, but I see a lot of people around me not knowing it. Vim has by default an auto-completion features. Yes it is very basic, and can be enhanced by plugins, but it can still help you. The process is simple. Vim can try to guess the end of your word based on the word you wrote earlier. If you are typing the word "compiler" for the second time in the same file for example, just start typing "com" and still in insertion mode, press Ctrl+n
to see Vim finish your word for you. Simple but handy.
Probably a lot of you know about vimdiff
command, which allows you to open Vim in split mode and compare two files with the syntax:
$ vimdiff [file1] [file2]
But the same result is achievable with the Vim command:
:diffthis
First open your initial file in Vim. Then open the second one in split mode with:
:vsp [file2]
Finally launch:
:diffthis
in the first buffer, switch buffer with Ctrl+w
and type:
:diffthis
again.
The two files will then be highlighted with focus on their differences.
To turn the diff off, simply use:
:diffoff
Vim keeps track of the changes you make to a file, and can easily revert it to what it was earlier in time. The command is quite intuitive. For example:
:earlier 1m
will revert the document to what it was a minute ago.
Note that you can inverse this with the command:
:later
Something that I always wanted to be comfortable doing when I started using Vim: easily delete text between brackets or parenthesis. Go to the first marker and simply use the syntax:
di[marker]
So for example, deleting between parenthesis would be:
di(
once your cursor is on the first parenthesis. For brackets or quotation marks, it would be:
di{
and:
di"
A bit similar to deleting inside a marker but for different purpose, the command:
dt[marker]
will delete everything in between your cursor and the marker (leaving it safe) if the marker is found on the same line. For example:
dt.
will delete the end of your sentence, leaving the .
intact.
This is not my favorite trick, but some might find it interesting. You can chain Vim and the xxd
utility to convert the text into hexadecimal with the command:
:%!xxd
And similarly, you can revert this with:
:%!xxd -r
Everything is in the title. If you want to force the screen to scroll and place whatever is under your cursor in the middle, use the command:
zz
in visual mode.
When editing a very big file, it is frequent to make changes somewhere, and jump to another place right after. If you wish to jump back simply, use:
Ctrl+o
to go back to where you were.
And similarly:
Ctrl+i
will revert such jump back.
This will generate an HTML page displaying your text, and show the code in a split screen:
:%Tohtml
Very basic but so fancy.
To conclude, this list was assembled after reading some various forum threads and the Vim Tips wiki, which I really recommend if you want to boost your knowledge about the editor.
If you know any Vim command that you find useful and that you think most people do not know about, feel free to share it in the comments. As said in the introduction, an "obscure but useful" command is very subjective, but sharing is always good.
This website is made possible by minimal ads and your gracious donation via PayPal or credit card
Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.
Xmodulo © 2021 ‒ About ‒ Write for Us ‒ Feed ‒ Powered by DigitalOcean