Skip to main content

5 Vim features for power users

Learn how to work with multiple files, comment several lines at once, write a macro to generate a number list, and more.
Person typing in a text editor on a laptop

Photo by Karolina Grabowska from Pexels

I first discovered the Vi editor back in 1986, when the company I worked for took delivery of a Zilog System 8000 running ZEUS—Zilog's version of Unix (a port of Unix Version 7). Its clone Vim was originally an acronym for vi imitation but was subsequently changed to Vi iMproved in 1993. Originally only available on Unix systems, Vim has since been ported to many modern operating systems and also ships with Apple macOS.

Vim now has a large user base, and there are many publicly available tweaks and enhancements available for free download. A major advantage of it as an editor is that you can use it anywhere once you have learned the basic building block commands. Despite using Vim for more years than I care to remember, I still find things that surprise me!

This article focuses on a very small subset of some useful advanced features.

1. Work with multiple files

When working on a project, I often find myself using multiple files. Fortunately, Vim allows you to have several files open in different tabs. To initiate this behavior, issue the following command (from command mode):

:tabe <filename>

This command enables tab expansion mode to make it easy to find files. Also, history is enabled and can be accessed by hitting the Up arrow.

To switch between tabs, use:

  • gt — Switch to the tab on the right
  • gT — Switch to the tab on the left

2. Switch case

Most power users know the ~ command to switch a single character's case, but this can also be expanded using Vim movement commands. For example, if you want to switch the word to the right of the cursor to upper case, enter:


So, if the cursor is at the start of a line, this results in the following change:

This is a line of text => THIS is a line of text

To change multiple words, put a number in front of the command, like so:


So now the change becomes this:

This is a line of text => THIS IS a line of text

[ Free eBook: Manage your Linux environment for success. ]

To change the whole line, try:


The result is:

This is a line of text => THIS IS A LINE OF TEXT

You can use any of the Vim movement commands for these tasks. For example, use b for the previous word and {, }, (, ) for sentences and paragraphs, and so forth.

To reference Vim movement commands, use

:help motions.txt

3. Use block mode

Have you ever heard the sound a keyboard makes as someone bashes out comments one line at a time? Here's a quick and easy way to comment a whole block. Given the sample file below:

>1 this is a test
2 this is a test
3 this is a test
4 this is a test
5 this is a test
6 this is a test
7 this is a test
  1. Position the cursor at the start of line 1 and press CTRL+V. At the bottom of the window, you will see the words VISUAL BLOCK appear.
  2. Now use either the Down arrow or the j key to move to the beginning of the last line. The first character of each line is highlighted.
  3. Now press I#.
  4. Finally, select ESC.

Voila! The entire block is now commented out. You can also perform other common operations on text blocks similarly, such as removing leading whitespace or trailing garbage.

The reverse operation to uncomment the block works similarly.

  1. Position the cursor at the start of line 1 (you can start it wherever you like and use the movement keys as needed, but for this example, I'll keep it consistent).
  2. Hit CTRL+V. Again, the words VISUAL BLOCK appear at the bottom of the window.
  3. Repeat the operation from before, using the Down arrow or j to move the cursor to the beginning of the last line.
  4. Now use the Right arrow or l movement keys to highlight the hash signs and the one space preceding the start of the text.
  5. Hit D.

Comments are now removed from the block!

4. Increment or decrement numbers

Another frequent task is adjusting a code block or list in a text file to increment numbers. Instead of manually editing each line with something like cW, just position the cursor over the number and use one of the following:

  • CTRL+A to increment the number
  • CTRL+X to decrement the number

5. Generate a list of numbers with a simple macro

Here is another common task. For this example, create a list from 1 to 20. Yes, I know there are myriad ways of doing this outside Vim, but this is about Vim and its shortcuts.

First, create a file with a single line containing line 1.

The cursor should be on the 1 as it should have nowhere else to go.

Select qayyp (which will record a macro into register a and copy line 1), then CTRL+A, which will increment line 2.

You should now have two lines, as follows:

>line 1
line 2

Now, leaving the cursor where it is, type q, ESC, and then 18a.

You should now see the following:

>line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
line 11
line 12
line 13
line 14
line 15
line 16
line 17
line 18
line 19
line 20 

6. Bonus

Finally, here are some additional useful timesaving commands:

  • ga — Print the ASCII value of the character under the cursor (also shows in octal and hex).
  • gi — Move to the last insert you did and enter Insert mode.
  • gv — Start visual mode and use the previous selection made.
  • gI — Start inserting at the beginning of the line no matter what the first characters are.

Wrap up

Vim is powerful and available for all major platforms. While many Linux users get complacent after learning the basics, there are many time-saving opportunities baked right into the application. These five tasks (and the bonus commands) may inspire you to learn more Vim tricks.

Check out these related articles on Enable Sysadmin

Topics:   Text editors   Command line utilities  
Author’s photo

Jack Knight

Jack is a veteran Unix/Linux guy who has worked in many different sectors and industries including electronics, telcos, gaming, finance, and government. He's experienced in multiple cloud platforms, automation tooling, and platform engineering. More about me

Red Hat Summit 2022: On Demand

Get the latest on Ansible, Red Hat Enterprise Linux, OpenShift, and more from our virtual event on demand.

Related Content