10 Time-Saving Tips for UNIX Vim Beginners

Vim is the preferred editor for many developers. And why shouldn't it be? With the right set of commands and terminal shortcuts, it helps to get the work done in less time. In this article, we'll focus on some shortcuts and tips for Vim beginners to help you speed up your work.

1. Configure .vimrc

When I started with Vim editor, I spent quite some time trying to indent my code. Little did I know that I could configure settings such as indent, syntax highlighter, line number, etc., for Vim using the .vimrc file.

Create a file called .vimrc in your home directory (I'm using Ubuntu Box) and add the general settings like line number, indent, etc.

set number # show line number
set autoindent # auto indent the code
set nowrap # don't wrap lines

2. Quitting the Editor Without Closing the Terminal

Saving and quitting the changes made to a file using Vim editor is a breeze. All you need to remember is to press the ESC key to switch to visual mode, put a colon, type wq and press Enter to save the changes and exit.

:wq

In case the changes are not to be saved, press the ESC key, put a colon and type q!.

:q!

3. Clearing a Line or the Whole Line

Clearing a whole line of code by pressing the backspace key seems a little like overkill. You can remove a line of code by placing the cursor on the line to be removed, switching to the visual mode and then pressing d twice to remove the line.

Now there may be cases when you want to remove a couple of lines from the code. In such cases:

Switch to the visual mode by pressing the ESC key.

Move to the start of the line and press m a to mark the start of the line.

Move to the last part of the code that needs to be removed and press m ' a and the code section gets removed.

4. Copy and Paste a Block of Code

Often you need to copy a single line of code or a large chunk of code. So, while working with Vim, this shortcut for copying and pasting a single line of code or a code block really comes in handy.

Switch to visual mode by pressing the ESC key.

Move the cursor to the start of the line which needs to be copied.

Press V to select the whole line.

Press d to cut the line or y to copy the line.

Move the cursor to the line where you want to paste and press p to paste after the cursor.

5. Undo and Redo Changes

While typing code in Vim or any other editor, you often need to undo and redo certain changes. To undo a certain change in Vim, switch to the visual mode and press u. To redo a certain change, switch to the visual mode and press CTRL+R.

6. Commenting Code

Commenting certain portions of the code is a tedious task, especially if it's multiple lines. We can use markers to comment blocks of code.

Switch to visual mode by pressing the ESC key.

Move the cursor to the first line of code that needs to be commented.

Type mk and move to the last line which needs to be commented. mk is a marker for the line to be commented

Move the cursor to the last line to be commented.

Type :'k,.s/^/#/ where k denotes the end of the marker, s denotes the selection, ^ denotes the beginning of the line and # denotes the character to be replaced.

7. Searching Using Vim

Searching is an important feature which is required most of the time. To search for a particular word in a file, switch to the visual mode and type in the word to be searched.

/word-to-be-searched

Pressing n would move the search forward, and pressing N would move it backward.

8. Read a File to Vim

When I started with Vim, I would normally copy the content of another file into Vim by opening the file and copying and pasting. Reading an external file's content into Vim is a breeze. Switch to the visual mode and press :read <File Name>. This is a really nice shortcut, rather than manually opening the file and copying the content.

9. Read the Command Output to Vim

Sometimes you need to copy the output of a particular command to Vim. With a little bit a modification to the above file-reading command, we can read the output of a command to Vim. Switch to the visual mode by pressing the ESC key. Enter :read !dir, which will add the listed directory names to the Vim editor.

10. Move to the Last Edited Line

How many times have you wondered where the last change that you made in your file was? Having a shortcut to move the cursor to the last position really helps.

Switch to the visual mode and type g; to move to the last changed position.

Wrapping It Up

In this tutorial, we saw 10 beginner's tips to get started with Vim editor in the UNIX environment. With a little bit of practice, you should be able to work faster in the Vim editor.

Do let us know your thoughts, suggestions or corrections in the comments below. Also share any other Vim tips or shortcuts that you feel may be of help for beginners.

Watch out for upcoming tutorials by checking my instructor page. It includes all the tutorials as soon as they are published.