Tricks to play with vim

card tricks plaisanter
Credit: flickr / plaisanter~

If you've been using vim as if it's simply a replacement for vi, you might be surprised by the many things it can do that go far beyond the editor that some of us have been using since 1976. The iMproved follow-up to vi, introduced in 1991, is incredibly customizable and extensible and provides a lot of features that most of us would never have expected in a text editor. In today's post, we're going to look at some of the more unusual of these features.

Sorting lines and removing duplicates

Sorting the contents of a file no longer requires that you use the sort command, direct the output to a second file, and then rename the second file. Instead, with vim, you can sort the contents of the file that you are working with without leaving your editing session. To illustrate, let's take a file containing a grocery list.

bananas
yogurt
spinach
chicken
bread
bananas
cheese

All you need to do when you sort the groceries file is type :sort u. As you likely guessed, the "u" stands for unique. This command will sort the file and remove duplicate lines.

bananas
yogurt
spinach
chicken
bread
bananas
cheese


:sort u

Once you press the enter key, your grocery list will look like this:

bananas
bread
cheese
chicken
spinach
yogurt

And, of course, you can still undo your changes, returning to the original list if you like by pressing "u".

Inserting the output of commands into your file

If you want to insert the current date and time into your file, open a new line to hold the data and then type :.! date. You'll then see something like this.

Sun Aug  9 18:55:03 UTC 2015
bananas
bread
cheese
chicken
spinach
yogurt

You can also use similar syntax to include other information. Maybe a list of who is currently logged in or an indication of how long the system has been up and running. Replace the date command shown above with the command of your choosing -- like who or uptime. This vim feature provides an easy way to grab command output and insert it anywhere in your file.

Incrementing and decrementing numbers

Incrementing and decrementing numbers within your text files is much easier than you might think. Instead of typing cw (change word), pressing ESCAPE and then typing a new number when you're positioned on the first digit of the number, you can just type Ctrl+a when positioned anywhere within the number to increment if or Ctrl+x to decrement it. And if you hold the keys down, you will keep incrementing or decrementing as much as you'd like, even into negative numbers.

In addition, if you type a number, such as 11 and then press Ctrl+a, the number you're positioned on will be incremented by 11. Type 7 and press Ctrl+x and the number will be decremented by 7. Another surprising trick for a text editor!

Converting numbers from hexadecimal number to decimal and back again

To convert a hexadecimal number to decimal without leaving vim, press colon followed by the echo command and your hex number in the 0x format shown below.

:echo 0xffff

You'll then see the number translated into decimal. In this case, you'll see 65535. You can also do the same thing with a command like this one.

:echo printf ('%d',0xffff)

In both cases, you'll see the decimal result, though vim won't insert the number into your text. It just does the conversion and displays the result to you.

You can also go from decimal to hex using a similar command to one of those above.

:echo printf ('%x', 65535)
ffff

Getting past permission problems

Ever get deeply into editing a file and then realize you need to be root to save it? From vim, you can issue a sudo command (assuming sudo is set up to permit you to run commands as root) and still save your changes -- without having to save the file to a different location (e.g., /tmp) and then use additional commands to get it back to where it needs to be.

:w !sudo tee %

Reverting changes

If during your editing, you ran into some problems and want to revert the file you are changing to what it looked like sometime earlier in your editing session, you can back out your changes, going back to sometime earlier in your session without having to abandon all of your changes. Just specify the how far back you want to go. In this example, we putting the file back to what it looked like 15 minutes ago.

:earlier 15m

If you go back too far, you can move forward using the command :later command.

Wrap Up

In some of the offices in which I've worked, people with accounts on Unix systems often work with vim without even knowing that they're using it. That's because vi might ber set up as an alias to vim. So, unless they ask about vi, as shown below, the vi that they think they're invoking might really be vim.

$ which vi
alias vi='vim'
        /usr/bin/vim

The vim editor has a lot of features that you wouldn't expect to find in an editor. Some of them might prove very handy!

This article is published as part of the IDG Contributor Network. Want to Join?

To express your thoughts on Computerworld content, visit Computerworld's Facebook page, LinkedIn page and Twitter stream.
Related:
Windows 10 annoyances and solutions
Shop Tech Products at Amazon
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.