Ten unsorted Vi / Vim tricks: Volume 1

Edit text like a pro with Vi.

In this post I will present ten easy but useful tricks using Vi / Vim commands. Vim (Vi Improved) is the modern version of Vi, the screen-oriented text editor originally created for the Unix operating system. Vim is present or installable in all Linux distributions like the ones in the Terminal.com snapshot store.

You might ask, why Vi?

Starting a turf war is far away from the objective of this post. Beyond personal preferences, learning Vi is useful for three main reasons:

  • Vi is always there. Since Vi is required by POSIX, you will find it in any *nix/Linux distribution.
  • Vi is extremely powerful. Editing text in a Unix based system is an everyday matter and with Vi you can speed up many editing tasks. Remember that in Unix, everything is a file and many of them are text files.
  • Vi is highly configurable. Additional plugins for auto-completion, text replacement, syntax highlighting... believe or not all those things are available in Vi.

Please note that this list is unsorted, and there are many more tricks to be learned. If there's a specific topic or question you'd like us to cover, please leave us a comment!

Tip 1) Getting some help

Vi has an extensive online help. You can access it by using the :h Vi command or the F1 key. If you're on a Mac, instead of F1, use fn + F1.

If you want to obtain specific help about a certain command, you can use the :h command Vi command. For instance: :h x

Tip 2) Search and replace

The search and replace function in Vi is one of the first tricks I learned, and one I use every day.

Searching for a character in a line

To search for a certain character within the current line, you can use the f command, followed by the character that we're looking for.

In this example, I'm pressing f 5 repeatedly in command mode.

Searching the entire file

You can also search for a word across an entire text file within Vi, by using the / command. In this example I will search for the word class, and then I will press n to go to the next occurrence.
Note that after the last occurrence, if you continue pressing the n key Vi will continue searching from the beginning of the file.

Searching for and replacing a word

Vi word replacement works more or less like in sed. The replacement command must be used starting with the colon character (:). Let's see an example:
Here I'm using the command :%s/app/application/gc to replace the word app with application. By adding the c flag at the end, I'm asked to confirm every replacement.

Tip 3) Lowercase to uppercase and vice versa

This one can be a little tricky, but once you understand it's like riding a bicycle.
Always in ex mode:

  • gU<n> then enter will convert to uppercase n+1 lines, from the cursor position. In this example, I press gU2 and enter:

  • gU<n> then right arrow will convert to uppercase n characters, from the cursor position. In this example, I press gU3 and right:

  • gU<n>w will convert to uppercase n words, from the cursor position. In this example, I press gU3w:

  • gUU will convert an entire line to uppercase:

Now, if you want to do the inverse operation (convert to lowercase) just replace the U with u in the command.

In that way, gu<n> will convert n+1 lines to lowercase, gu<n>w will convert n words, guu will convert the entire line to lowercase, etc.

Tip 4) Show line numbers

This simple trick will show line numbers in your Vim editor. Just execute the command :set nu or :set number and your lines will be numbered. To remove the line numbers, use the set nonumber command.

Tip 5) Execute an external command from within Vim

This function is also known as 'shell escape' as it has been used by many people to outsmart bad sudo security implementations.

To execute an external command from within Vim, just execute :!<command>. For instance:

To do a shell escape you can use a shell (like bash) as the command.

Tip 6) Insert an existing file into the current one

In case you need to insert the contents of a file into the one you're currently writing, use the :r <file> command. For instance:

Tip 7) Display changes performed since last save

This trick uses diff to display the difference between the file in disk and what you're currently editing. In order to do that, execute :w !diff % -

Tip 8) Indenting and un-indenting lines

You can indent a line by pressing >> (the > key, twice). You can also un-indent a line by pressing <<.

If you want to indent or un-indent a certain amount of lines, indicate the number of lines to be indented before the indentation command.

In this example, I execute 4>> and then 3<<. Let's see the results:

Tip 9) Undo and redo

When you're in edit mode, press esc to go back to the normal (command) mode and u to undo your last changes.

You can press u repeatedly to undo more actions.

If you want to 're-do' your changes which were undone, press Ctrl-R.

Tip 10) Open Vi / Vim in a specific line

You can open Vim with the cursor in a certain line by executing Vi in this way:

vim <filename> +<line number>

Check this example:

That's all folks! I hope you've enjoyed my post. As always, please leave any questions or followup requests in the comments section.

Now go out there and write something amazing!

Enrique Conci

Read more posts by this author.