What are some obscure but useful Vim commands

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.

Saving a file and exiting

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.

Basic calculator

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.

Finding duplicate consecutive words

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:

\(\<\w\+\>\)\_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.

Abbreviations

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.

Save a file that you forgot to open as root

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.

Crypt your text on the go

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'

Auto-completion

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.

Look at the diff between two files

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

Revert the document in time

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

Delete inside markers

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"

Delete until a specific maker

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.

Turn Vim into a hex editor

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

Place the text under your cursor in the middle of the screen

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.

Jump to previous/next position

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.

Render the current file as a web page

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.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.

Adrien Brochard

I am a Linux aficionado from France. After trying multiple distributions, I finally settled for Archlinux. But I am always trying to improve my system by stacking up tips and tricks.

Latest posts by Adrien Brochard (see all)

10 thoughts on “What are some obscure but useful Vim commands

  1. For the "di{marker}", you don't have to be on either marker, just inside them. And since the "i{token}" and "a{token}" are considered motions, you can use them in other contexts such as "ci[" to delete the contents of the "[…]" you're in, and leave you in insert mode; or "gUi(" to change the current sentence to upper case.

    Also, if you want to sharpen your vim-fu, check out http://vimgolf.com where you can see all sorts of obscure commands at play.

  2. Actually :x and :wq are not equivalent. :x saves only if buffer is changed (file on disk is different from file in memory). if buffer is the same as file then buffer will not going to be saved into file. But :wq will save in any case. Yes the end result is the same - file on disk has the same content. But you need to know that time of last editing (modification) is stored in file system as mtime (you can observe it by using 'ls -l' command). And in the case of :wq mtime ALWAYS changes but in the case of :x it does not changes if buffer and file are the same. Not big deal... but if you are using some backup tool that looks into mtime file data then in case of :wq file can be backed up although it didn't change. So I suggest to use :x instead of :wq command.

    About diff. There are two enhancements. If you already have file opened (or execute :e file1) and you add file 'vsp file2' - so you have two windows vertically split. There is no need to do :diffthis two times for each of the window. There is nice 'windo' (like in English: windows do) Vim command to execute command in all windows. So after opening having both files in two vertical windows, just execute: windo diffthis (so execute diffthis in both windows).

    There is also one shorter way of doing it. If one file is already opened (if not open it wit :e file1), you can just execute: 'vert diffsp file2' and second file will be opened vertically (as vsp does) and automatically diffthis to both windows. That is why Vim is so great. If you are doing one think over and over again there got to be some sorter way of doing it.

    Hope this helps.

  3. :r! the output will come to file. :)
    Example:
    :r! ls /home/ -> This will take all folders inside /home and put inside vim

    :r! sed '4p; d' file -> This will take line 4 of another file and put inside vim

  4. The "TO" characters of HTML conversion command :%Tohtml should be uppercase, i.e. :%TOhtml.
    At least on my Ubuntu 10.04 and 12.04 system.

  5. For deleting between markers you can simply type in normal mode:
    d%
    The cursor just has to be place on the opening or closing marker. It works at list with ([{

  6. Just adding my 2 cents here. I once happened to be connecting via remote desktop to a machine with a keyboard configuration that made the colon character almost impossible to find. So how could I save the file I was working on? (Didn't want to change the keyboard configuration). Simple. Went into command mode by pressing Esc, pressed the Caps lock key, and hit the Z key twice. In other words, ZZ can also be used to exit a file saving changes. (Hope it helps).

  7. As for all Vim commands you don't have to use visual mode to apply them. So instead of using "ggVGg?" you can just use "ggg?G", which means "gg" jump to top, "g?" apply Roth-13 (for next command) "G" apply until end of file.

  8. Some other commands I use from time to time:
    If you write text (like mails), you can use "das" or "dis" to delete the sentence where the cursor is located. It recognizes "?", "!" and "." as sentence delimiters.
    When writing HTML/XML/... "dat" and "dit" become handy. Just move inside a tag and remove its content ("dit") or the whole tag ("dat"). Instead of removing the content, it is often useful to change the content (for a header or a link for example), type "cit" to remove the content and go into edit mode.
    To change numbers without calculating the final result, you can use ctrl+a (increase/add) and ctrl+x (decrease) the number under the cursor. So to add 492 to a number type "492". Vim also recognizes different integer formats, like hex or octal which can be configure with the 'nrformats' option.
    ctrl+i and ctrl+o are quite useful, but I mainly need to jump back (and forth) to the very last position, therefore you can use '' (two single quotes) or `` (two backticks) respectively.

Leave a comment

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