Image

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.
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:
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:
gUw
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:
gU2w
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:
gU$
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
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
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.
Comments are now removed from the block!
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:
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
Finally, here are some additional useful timesaving commands:
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.
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