Linux VI Editor Shortcuts, Tips and Productivity Hacks For Beginners

VI Editor Shortcuts, Tips and Productivity Hacks

In this Linux tutorial, you will learn the VI editor shortcuts, some tips, and few productivity hacks that can be used for DevOps Engineer & Developers

If you are a DevOps engineer or someone who works on Linux systems, For editing files, vi editor is used quite often. Unlike GUI editors, first-time VI editor users get intimidated by it as you have to use various keystrokes for controlling the editing.

However with a little bit of practice and using the vi editor shortcuts in our day-to-day activities will save your time, increases Productivity and you will start loving the VI editor. Moreover, it is a very powerful test editor in the Linux ecosystem.

VI Editor Shortcuts For Beginners

In this article, we will cover the necessary shortcuts that you could use in day-to-day Linux activities that include the vi editor.

VI editor Modes

Before getting into shortcuts, you should understand the three modes of vim editor.

There are three modes in VI editor.

  1. Normal Mode
  2. Insert Mode
  3. Visual Mode
  4. Command Mode
  5. Replace Mode

Normal Mode

When you open any file in the VI editor, the default mode is Normal Mode. In this mode, you can cursor to browse through the file using different shortcuts.

Following are the shortcuts to move the cursor in different directions. If you add a number to the shortcut, the movement gets multiplied.

VI ShortcutAction
hMove one character left (4h will move four characters left)
jMove one line down ( 4j -> 4 lines down)
kMove one line up (5k -> 5 lines up)
l Move one character right (8l -> 8 characters right)

Next, we will learn the shortcuts to browse through words. Here the lower and uppercase characters have different functions.

VI ShortcutAction
wGoes to the beginning of the word. W goes to the beginning of the next word after the whitespace.
bGoes to the beginning of the previous word. B goes to the beginning of the previous word before the whitespace.
eGoed to the end of the word. E goes to the end of the word before the whitespace.

The next shortcut is required most of the time when you work with files.

VI ShortcutAction
0Goes to end if the line
$Goes to the beginning of the line

Insert Mode

In insert mode, you can edit the file. Following are the shortcuts for editing. Again, the lower and uppercase characters have different functions here.

VI ShortcutAction
i VI editor goes in to insert more. ( I –> Goes to the beginning for the line in insert mode)
aThe cursor moves to the next character in insert mode ( A – moves to the end of the line in Insert mode)
oInserts a new from the cursor’s current position in insert mode. (O – inserts a new line above the cursors current position in insert mode)

Visual Mode

The visual mode makes it easy to select and modify text in the VI editor. To enter the visual mode just enter “v“.

You will be able to select multiple lines.

Command Mode

In command mode, you can execute commands and manipulate text. For example, find and replace a text in the whole file.

You need to know the following to work with the command mode.

VI ShortcutAction
sTo start the command mode
%Considers all the lines in the file.
sfor substitution.
/gglobal change

We will look at some of the examples for command mode later in the article.

Replace Mode

Replace mode is to replace existing texts in a document.

place the cursor at the first character of a work and enter “R” in normal mode. Once you start typing, the word will get replaces.

Setting up VI environment

Before diving into shortcuts and commands, you must understand the vi editor settings. You can set all the necessary vi editor parameters in the ~/.vimrc file that will be loaded by default.

If you don’t have the vimrc file, create one using touch ~/.vimrc

Following are the common parameters that you might need in the vimrc file.

set number
set autoindent
set shiftwidth=4
set softtabstop=4
set expandtab

The set number parameter will set the line number for your vi editor.

You can set and unset this temporarily by running :set nonumber from the editor

vi setnumber

Cut Copy Paste in Vi Editor

Follow the steps given below to copy and paste using the visual mode.

  1. Enter the visual mode using ESC + v and move the cursor to select the string you want to copy.
  2. Use capital V to select the whole line.
  3. Use d to cut or y to copy.
  4. Place the cursor in the desired location and press p to paste.

The following Gif shows the demo.

vicut

Deleting Lines and Words

Here is how you can delete lines and words in VI editor.

  1. Place the cursor on the line you want to delete and press dd to delete that line
  2. To delete a specific number of lines, you can use d10d. This will delete 10 lines starting from the cursor. You can give any number in place of 10 based on your needs.
  3. To select and delete specific line, use Shift + V (Visual Mode) and then use up and down arrows for the selection. Once you selected the lines, press d for deleting all the selected lines.

To delete words, enable the insert mode (ESC + I) and you can do the following.

  1. Ctrl + w will delete the last word where the cursor is in.
  2. Ctrl + u will delete all the words which are immediate to the left of the cursor.

Searching and Replacing Texts

You can search through your files by pressing / in normal mode.

For example, if you want to search a keyword data, you should do the following.

/data

To find the next occurrence, just press n

Note: Searches are case-sensitive. If you want a case-insensitive search, you should set :set ignorecase in the editor.

Enter the command mode (ESC + :) and use the following syntax for replacing a pattern.

:%s/pattern/replace/g

Use the following syntax for replacing every occurrence with a prompt. It will highlight all the occurrences. You can choose which occurrence to replace and which one to leave as it is.

:%s/pattern/replace/gc

Convert to Upper and Lowercase

For any case text conversions, keep the cursor on the line and use the following shortcuts in normal mode (ESC).

  1. To convert a line to uppercase use gUU
  2. To convert a line to Lowercase, use guu

Copying Contents From Another File in Vi Editor

This is not something that you do very often. However, if you need to add the contents of another file to the current file you are editing, place the cursor on the desired line and you can use the following syntax in the command mode. The contents of the specified file will be copied from the next line of the cursor.

:r /path/to/file

For example, if you copy CPU info to an existing file, you would use the following command.

:r /proc/cpuinfo

Executing/Copying Contents From Command

You can execute other Linux commands inside the editor in the command mode (ESC + :)

To execute commands from the editor, you can use :! <command>. For example,

:! pwd

If you want the output of a command to be copied to the editing file, you can use :r! <command>. For example, to get and copy the eth0 IP address, you could do the following.

:r! ip addr | grep eth0 | grep inet | awk '{print $2}' | cut -d / -f1

Vi Editor Help

There are so many things you can do with VI editor.

If you want to become a VI editor power user, you should make use of the VIM tutor functionality.

vimtutor 

It opens up a command line tutorial in your terminal. And you can use all the normal vi commands to browse through the help document.

Wrapping Up

VI is a very powerful editor.

Understanding the full functionality and all the vi editor shortcuts takes time and constant practice.

We have explained some commands, tips, and vi editor shortcuts that will save some time while working with the vi editor.

If you think you have some tips, please share them with us in the comment section. It could help others.

If you are getting started as a DevOps Engineer, check out the important Linux Troubleshooting commands as well.

19 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like