Tips on Vi/Vim Editor for Linux newbies

1
15239

vim-editor

Inviting all Linux newbies, and even veterans, to try out these tips on using Vi/Vim editor efficiently and productively.

Earlier, most Linux users cut their teeth on Linux with the Vi editor. Those comfortable with using the GUI for Linux may scoff at using Vim for editing, but it does have a few advantages, particularly when working in multi-user systems and also when working on servers. This is because of the Vi editor’s efficiency and its minimal use of memory. It is powerful, highly configurable and always available on any system.
I have compiled a few Vi/Vim editor tips, which will make it easier for readers to use it and become more productive.

Abbreviations
Abbreviations can be set, which Vi will expand into full text whenever they are typed in edit mode. An abbreviation can be defined in the command mode by giving the following EX command:

:ab  abbr   full text

…where abbr is an abbreviation for the specified phrase. The expansion is done only when the abbreviation is typed as a word. Avoid repeating the abbreviation in the defined phrase.
The abbreviation can be disabled by typing the following command:

:unab abbr

Encryption in Vi
To encrypt any file in the Vi editor, type the following in command mode and press the Enter key:

:X

You will be asked to enter a password twice, which will be used to encrypt the file. The file is now saved in an encrypted format. When opening that file, the password needs to be re-entered. Of course, if you have encrypted a file containing a programming language source code, the file cannot be compiled.
To decrypt the encrypted file, open the file using Vi and type :X. Next, hit the Enter key twice; then save and exit the Vi editor by typing :wq. The file will be saved in the decrypted format.

figure-1-a-code-snippet-without-indent
Figure 1: A code snippet without indent

The .vimrc file 
The EX commands (those that you key in after typing : in the Vi editor), which you want to  execute whenever you start the Vi editor, can be saved in the .vimrc file in your home directory. If this file is not already there, you can create it. The .vimrc file can be modified like any other text file. Examples of some settings are given in Table 1.

Command Short form Action
 set number
se nu
Displays line numbers
set nonumber
Se nonu
Removes line numbers
set autoindent
se ai
Automatic indentation in insert mode
Set noautoindent
Se noai
Removes the automatic indentation feature in insert mode
se tabstop=3
se ts=3
Sets the number of spaces by which the tab indents during editing
syntax on Turns on highlighting of syntax
syntax off Turns off highlighting of syntax
set shiftwidth=4
set sw=4
The size of the indent, measured in spaces

Inserting the contents of an existing file 
Often, we need to insert the contents of an existing file into the current file. The Vi editor provides an easy way to do this. Take the cursor to the position at which you want the insertion and issue the following command:

: r  filename

This inserts the contents of the file from the current cursor position onwards.
Adding a number before r inserts the text file below the line with the particular line number:

:40r filename

Running an external command within Vi 
You can execute any command within the Vi editor by typing the following command within the command mode of Vi:

:!commandname

Insert the output of a Linux command 
You can insert the output of a command by giving the following command:

:r! :commandname

The command is executed and the output is inserted from the position where the cursor is located.

Opening Vi directly at a particular line 
You can open the Vim editor with the cursor placed directly at the line you want, by giving the following command:

$vim filename +n

The number ‘n’ in the command is the line number.

Opening Vi at the end of the file 
Adding the + sign and a space before the file name, opens the file and places the cursor at the last line. This is useful if you have to work on large files.

$vim + filename

Writing the buffer to a new file 
:w can be used to save the complete buffer, that is, the file being edited currently, under a new file name. This can be used when you have made a lot of changes in the file, but then realise that you do not want to overwrite the original file. To save the contents of the buffer, use the following command:

:w newname

You can now quit the original file by typing :q.

Indenting the source code 
In case you have typed dozens of lines of code without bothering about indentation, the quickest way to indent the code is to do the following;
Press the Esc key to go to command mode and then type the following:

gg=G

…where gg indicates the beginning of the file, = is for indenting and G indicates the end of the file.

Repeating the last change 
We can use the . (period) key to repeat the last change. This helps to reduce the typing required when carrying out repetitive tasks.

Undoing and redoing 
Type u to undo the last change.
Type Ctrl-R to repeat a change that has been undone.

figure-2-a-code-snippet-with-indent
Figure 2: A code snippet with indent

Jumping to matching braces
We can jump to the matching parenthesis (the curly brace or square bracket) by pressing the % key.  This is very useful when editing source code.

Preventing auto-indent 
When text is pasted in Vim from an already indented source file, Vim will further apply its auto-indenting feature (if it is enabled) to the pasted text, which will have a cascading effect on the text. To prevent auto-indenting, use the following command:

:set pastetoggle=<F2>

You can then press <F2> in the insert mode when you are ready to paste. After pasting, you can press <F2> again to go to the auto-indent mode.

Vimtutor 
You can type vimtutor at the prompt and go through the tutorial on Vim. It will help you to learn more about Vim, quickly.

1 COMMENT

  1. […] 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 […]

LEAVE A REPLY

Please enter your comment!
Please enter your name here