VIM Editor in Linux

Vim editor

Vim editor is used to create and edit a text file. There are two modes in vim editor. One is the command mode and another is the insert mode.

Command Mode

In the command mode, user can move around the file, create a text, delete text, exit the file, and manipulate the files.

Insert Mode

In the insert mode, user can insert text. Option i is used to change to insert mode.

Basic syntax

vim filename

Press esc + options for the output

Text Entry Options

The text entry options are used for entering the text in the file.

Cursor Movement Options

For the cursor movement the following options are used.

h &ndash It is used to move the cursor one character to the left

l &ndash It is used to move the cursor one character to the right

k &ndash It is used to move the cursor one line up

j - It is used to move the cursor down one line

nG(or):n &ndash It is used to move the cursor to the specified (n) line follow the example Ex: 4G, To move to the 4th line, press 4 and then ‘ G’ Now the cursor moves to the 4th line.

CTRl + f - ‘ Ctrl + f’ forwards to a fresh screen which starts with last line of the page.

CTRl + b &ndash ‘ Ctrl + b’ reverse the ‘ Ctrl + f’ option.

$ - It is used to move the cursor to the end of current line

0 &ndash It is used to move the cursor to the beginning of current line

Exit Options

These options are used to exit from the editor.

esc:wq(or)shift + gg - It is used to write a file to disk and quit the editor

:q! &ndash It is used to quit the editor without any warning

:q(Quit) &ndash It is used to quit the editor press esc and then the option ‘ :q’ . A warning message is displayed if the modified file is not saved

Delete Options

These options are used to delete specified contents from the editor.

x &ndash It is used to delete a character in the current cursor position

dw &ndash It is used to delete a word from the current cursor position

db &ndash It is used to delete a word backward from the current cursor position

dd - It is used to delete the current line

d$ - It is used to delete the contents from the current cursor location till the end of the line

d^ - It is used to delete the contents from the current cursor location till the beginning of the line

Copy Options

The copy options are used to copy the contents in the vim editor.

yy &ndash It is used to copy the current line

5yy &ndash It is used to copy five lines from cursor location

y$ - It is used to copy the contents till the end of current line from cursor position

yw &ndash It is used to copy the contents till the end of current word from cursor position

Paste Options

This option enables to paste the contents which are copied.

p -To paste the copied contents below the current cursor location

P &ndash To paste the copied contents above the current cursor location

2p &ndash To paste two lines from the copied content

u -To undo the last change in the file

CTRL + R -To redo the last change in the file

J &ndash To move contents from the current cursor to the next line

File Manipulation Options

These options are used to manipulate the files

:w -To write changes to original file

:e -To edit a file

:r &ndash To read the contents of the file

Searching Pattern Commands

/enabled &ndash It searches for the word enabled from the top to bottom of the file and highlights the matching word.

Type “ n” for next match and Shift + n to go back to previous match

Find and Replace Commands

:%s/oldname/newname/g &ndash It checks for the word old and Replaces all the word old in the file to new

:%s/oldname/newname/gc - It checks for the word old and ask for confirmation before replacing the word in the file.

:%s/Oldname/Newname/gci -This command is case insensitive and ask for confirmation. It checks for the word Old and replace the word with New after confirmation.

Opening Multiple Files

To open multiple files in vim editor, specify the file names separated with space while executing the vim command, Ex:vim test1 test2 test3.To switch between those files use the option “ :n” for going to next file.

You can also open any file at any time from vim editor by executing “ :e filename” .

:sp filename &ndash To open a new file in a split window use the option ‘ :sp’ .

:vsp filename -To open a new file in a split window vertically use the option ‘ :vsp’ .

Ctrl + ww &ndash To switch between the files which are opened in the editor use ‘ Ctrl +ww’ .

VIM Advanced Navigation

If we are in a situation to deal with large file which contain thousands of line, using arrow keys will waste lot of our time, To avoid this, we use ‘ g’ operator.

To get to the end of the file hit use “ Shift + G” ,to get to the beginning of the file use gg command and to navigate your cursor on n line use nG, Ex:5G,navigates the cursor to the 5th line.

Visual Mode

Visual mode is used to mark lines and then perform operations like copying, deleting.

aw -It marks a word in visual mode

ab &ndash It marks a block of words with ()

aB &ndash It marks a block of words with {}

Esc -To exit visual mode use ‘ Esc’ key

Other Useful Commands

.(dot) -It repeats the last executed command

:set nu &ndash Displays line number in the file you are editing, to remove the line number from the file.

: colorscheme colorname -color scheme is used to change the color of the Vim editor. Ex: :colorscheme blue.

Syntax on - syntax can be turned on or off based on your need, if its turned on it will display the color syntax for .xml, .html and .perl files.
:set ignorecase -If ignorecase option is set, it ignores the case while searching a for word in the file.

:set smartcase -This allows case sensitive search if the word you are searching contains an uppercase character.

Tag : vim
FAQ
Q
How to view the content of the file with line number using the vim editor on Linux?
A
You can click "Esc" at that point squeeze ":set nu" to view the content of the file with line number using the vim editor on Linux.
Q
How to do the undo functionality in vim editor in Linux?
A
You can click "Esc" at that point squeeze "u" to give the contribution of fix usefulness (undo) in vim supervisor on Linux.
Q
What are the different operating systems will have vi editor is by default?
A
Here I have to mention some of the different operating systems will have vi editor or clone in by default. Such as Unix, Dos, OS/2, Mac System 7.
Q
What is the alternative or clone application available for vi editor?
A
Here you can see the alternatives or clone application for vi editor. Such as STvi (STevie), elvis, vile, vim, and nvi, xvi. elvis is available for: Amiga, DOS, OS/2, Unix, VMS. STevie is a
Q
How to Install vim Text Editor on Ubuntu?
A
You will just type the "vim" on your Ubuntu terminal. Then you will see the entire list of vi editor available for Ubuntu. So you can install that packages using the "apt-get install" command