Ten unsorted Vi / Vim tricks: Volume 2


Edit text like a pro with Vi.

This is my second post about Vi / Vim tricks. Check out my first post for even more tips.

Please note that this list is unsorted, and there are many other 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) Open the file under the cursor

Believe me, this trick is great. You can open a file in Vim just by pressing g f, with the cursor over the file name.


Tip 2) Show, set, and reset Vim's variables

More than a trick, this is part of the standard Vim usage and configuration. Many people don't use the Vim configuration variables, so we're including these in our list as well.

  • :set will show the config variables that are currently different from the default value
  • :set all will show all config variables
  • :set <var>? will retrieve the value of just one variable (<var>)
  • :set <var>& will set the variable (<var>) to its default value
  • :set <var>=<value> will assign a value to a variable


Tip 3) Insert the output of an external command

You can use the :r <command> Vim command to insert the output of an external console command in the file that you're currently editing.


Tip 4) Enable syntax highlighting

In some cases the syntax highlighting feature is disabled by default. In order to enable it, you can use the :syntax on command. You can disable it again by executing :syntax off.


Tip 5) Jump to the matching bracket and select contents

You can use the % key to jump to a matching opening or closing parenthesis ( ), curly brace { }, or square bracket [ ]. This is especially useful when using Vim to write code.

Additionally, you can remap the % key (change the default action) to visually select the code in between. You can do that by executing :noremap % v%.


Tip 6) Highlight search matches

By executing :set hlsearch, you can highlight all search matches at the same time. This function can be temporarily disabled by using the :nohlsearch option or its abbreviated version, :nohl.


Tip 7) Record commands

With Vim, you can record several actions and then execute these actions again.

  • qa starts recording
  • q stops recording
  • @a plays & applies the recorded command
  • @@ repeats the process

In this example, first I start recording by pressing q a, then I convert the first line to uppercase (with gUU) appending the word 'YEAH!!' to the end of line. After that, I write the second line and stop recording by pressing q. After that, I apply the recorded macro to the second line by pressing @ a and repeat it by pressing @ @ a couple times.


Tip 8) Find word under the cursor

This easy but powerful trick will let you find the next or previous occurrence of the word that's currently under the cursor.

  • Use * to search for the next occurrence
  • Use # to search for the prior occurrence


Tip 9) Command history

To get a list of all of the commands executed in Vim, use the :history command.


Tip 10) Easter eggs!

Try by yourself...

  • :help 42
  • :help holy-grail
  • :help!
  • :help UserGettingBored
  • help bar
  • :Ni!

That's it! 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.