The Complete Magazine on Open Source

Beginning with Vi/Vim Editor for Linux newbies

For Linux newbies and other typical users, here is the second set of tips on using Vi/Vim editor efficiently and productively.

This is a continuation of my previous article titled, ‘A few tips on Vi/Vim editor for Linux newbies’. I have compiled a few more Vi editor tips that are very simple but help to increase productivity tremendously.

Going to any line
In a large-sized file, going to a particular line directly is extremely useful and saves a lot of time. This can be done by going to the command mode and typing the following:

:50

…assuming that 50 is the line at which you want the cursor to be positioned. The other way is to type:

50G

…in the command mode to get the same result.
Typing just G in the command mode takes the cursor to the last line of the file and typing 1G takes the cursor to the beginning of the file.

Incrementing or decrementing a number
You can increment or decrement a number easily. Go to the command mode, position the cursor below the number and type the following:

Ctrl-a

This increments the number. Similarly, you can type the following to decrement the number on which the cursor is positioned:

Ctrl-x

Changing the case of letters
Without the shortcuts, changing the case of letters, words and sentences whenever required is a tedious procedure. With the shortcuts given below, it can be done very easily.
To toggle the case of the character below which the cursor is positioned, type ~
To change the case of the current line to upper case, type gUU
To change the case of the current line to lower case, type guu
To toggle the case of the current line, type g~~
To toggle the case of all characters from the cursor position to the end of the line, type g~$

Sorting within Vi
Sorting within Vi editor can be achieved without using the external sort command by typing the following in the command mode:

:sort

The above command is to sort the text.

:sort u

The above command can be used to sort lines and remove duplicate lines.

Figure 1: Vi editor before sort

Executing a filter command
You can execute a filter command on a part of the current file from within the Vi editor and replace the portion with the output of the command. Any command that reads from a standard input and sends the output to a standard output can be used as a filter within Vi. To illustrate this, we use the sort command. Type the following in the command mode of the Vi editor:

:1,$!sort

This sorts the file from the first line to the last line and replaces these lines with the sorted output. (Just a reminder: The ‘!’ symbol is used within the command line of the Vi editor to execute an external program from within a Vi editor.)
The -u option with sort will keep only the unique lines and remove the duplicate lines.

:1,$!sort -u

The line number 1 and the $ symbol in the above command can be replaced with any portion of the file with the numbers of the starting line and ending line of the particular portion. The portion selected for input will be replaced by the output of the sort command. To sort the lines starting from the contents of line number 5 to 15, give the following command:

:5,15! Sort

Using tr within Vi
The following command will convert all the letters in the line numbers 10 to 20 to upper case:

:10,20 ! tr a-z A-Z

Writing a portion of a file being edited
The command to write a portion of the current file being edited to a new file is as follows:

:20,50 w > newfile.txt

As a result, the lines from 20 to 50 are written in a new file named newfile.txt. The above facility of processing parts of the text and substituting the original content with the output makes the Vi editor very powerful.
Inserting the output of a command executed within the Vi editor
Most users are familiar with the method to execute a shell command within the Vi editor. For example:

:! ls

Less known is the fact that you can insert the output of the command given within the Vi editor by placing a ‘.’ (period) before the exclamation mark. For example, the following command will insert the current date into the file being edited, which can be used for documentation.

:.! date

Cursor movements
Vi editor has powerful cursor movement commands which, if mastered, can make editing faster.
H The cursor is positioned at the first line of the screen
M The cursor is positioned at the middle line of the screen
L The cursor is positioned at the last line of the screen

Scrolling through a file
The following commands can be used to scroll through a file.
Ctrl-f Scroll down by one screen
Ctrl-b Scroll up by one screen
Ctrl-u Scroll up by half a screen
Ctlr-d Scroll down by half a screen
zz Scroll the screen so that the current line appears at the middle of the screen.
The last one, zz, is very useful for viewing the block of code associated with the current line.

Editing a file opened without sudo
Let’s assume that you are editing a file which requires root access and, as a result, you are unable to save it as you have not opened Vi editor in the sudo mode. So instead of saving the file by taking many steps, including saving it in a temporary file and copying it later, you can save it straight away by using a combination of the tee and sudo commands, as follows:

:w !sudo tee %1

Figure 2: Vi editor after sort

Powerful delete commands
Apart from the commonly used commands for deleting, the ones I found very useful are the following.
di(  Deletes all characters within the parentheses
di” Deletes all characters within the quotes

Shortcut to save and quit
Generally, we type :wq in the command mode to save the file and go to the command prompt. A shortcut for the same is as follows:

:x

Recovering a file
Lastly, the following command will help you to recover a file after a crash, from the swap file of the file being edited:

$vi -r filename

Practising these Vi tips will help you to increase your efficiency, and the side effect could be that you postpone the onset of RSI (repetitive stress injury)!

  • Ravi

    Very informative band useful

  • Surya

    I wish i had known all these tips earlier.