Linux VI Editor Shortcuts, Tips and Productivity Hacks For Beginners

Linux VI Editor Shortcuts, Tips and Productivity Hacks

In our last blog post,  we have covered Linux CLI Productivity tips. When you work on Linux systems, vi editor is something which we use very often for editing files. It is not like using a GUI editor and people using it for the first time gets 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 the 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.

Note: Most of the commands explained in this tutorial works in normal mode. (Press ESC to make sure you are in Normal mode before executing commands)

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 that 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 line number for your vi editor. You can unset this temporarily by running :set nonumber from the editor.

Cut Copy Paste

1. press ESC and click v and move the cursor to select the string you want to copy. Use capital V to select the whole line.

READ  How To Setup an NFS Server and Client For File Sharing

2. press y to copy.

3. Place the cursor in the desired location and press p to paste.

Deleting Lines/Words

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 and then use up and down arrows for the selection. Once you selected the lines, press d for deleting all the selected lines.

In insert mode, you can do the following.

1. Ctrl + w will delete the last word where the cursor is in.

3. Ctrl + u will delete the all the words which are immediate to left of the cursor.

Searching and Replacing Texts

You can search through your files by pressing /.

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

/data

To find next occurrence, just press n

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

2. Use the following syntax for replacing a pattern.

:%s/pattern/replace/g

3. Use the following syntax for replacing  every occurrence with a prompt. It will highlight all the occurrences.

:%s/pattern/replace/gc

Convert to Upper and Lowercase

For any case conversions, keep the cursor on that line and use the following shortcuts.

1. To convert a line to uppercase use gUU

2. To convert a line to Lowercase, use guu

Copying Contents From Another File

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

:r /path/to/file

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

:r /proc/cpuinfo

Executing/Copying Contents From Command

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

:! pwd

2. 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 eth0 IP address, you could do the following.

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

Getting Help

At any point in time, if you need help on the vi editor, you can run vimtutor command to open the command line tutorial in your terminal.

READ  Docker Multi-Host Networking Tutorial - Using Consul

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 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 tip, please share it with us in the comment section. It could help others.

Free DevOps Resources

Get DevOps news, tutorials and resources in your inbox. A perfect way If you want to get started with devops. Like you, we dont like spam.

Leave a Reply

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