Vim Text Editor Basics:
Vi is the default and most commonly used text editor in Linux/Unix operating systems. Vim is an enhanced version of vi which is original Linux/Unix text editor.
Most of the Linux administrator prefer vim, for its bundled of features and ease of use. It can be used to edit all kind of text files e.g. plane text files, Linux configuration files.
Vi is the default text editor in Linux, to get the more advanced editor vim you need to install it using the following command
# yum install vim -y
Before using you should know that it can operate in two modes one is command mode and other is insert mode.
Once you will start vim editor it will always start in command mode. In this mode you can search, replace, copy, paste and other editing task.
The other mode is the editor mode where you can actually modify text into the editor.
Start with Vim:
We start vim with the command vim then specify the desire text file name.
# vim /filename
To open a file in vim text editor, run the vim command with filename as an argument.
If the file is already exist on the specified path, it will open the existing file else the edit will create and open new file with same file name which is mentioned in above command.
The editor will open the file in command mode, where you can perform some editing task and move the cursor by using arrow keys on your keyboard.
But you cannot edit the text until you press I button. Pressing I will activate the insert mode.
i - insert before the cursor
I - insert at the beginning of the line
a - insert (append) after the cursor
A - insert (append) at the end of the line
o - append (open) a new line below the current line
O - append (open) a new line above the current line
ea - insert (append) at the end of the word
Esc - exit insert mode
Cut, Copy, Paste and delete:
Open file in vim editor move cursor on desire line and use options below mentioned.
yy - yank (copy) a line
2yy - yank (copy) 2 lines
yw - yank (copy) word
y$ - yank (copy) to end of line
p - put (paste) the clipboard after cursor
P - put (paste) before cursor
dd - delete (cut) a line
2dd - delete (cut) 2 lines
dw - delete (cut) word
D - delete (cut) to the end of the line
d$ - delete (cut) to the end of the line
x - delete (cut) character
Search and replace:
/pattern - search for pattern
?pattern - search backward for pattern
n - repeat search in same direction
N - repeat search in opposite direction
:%s/old/new/g - replace all old with new throughout file
:%s/old/new/gc - replace all old with new throughout file with confirmations
Once you are done your editing you have to come back in command mode to be able save changes, you have to press Esc key to change mode from insert to command mode and use below options.
:w - write (save) the file, but don't exit
:wq or :x or ZZ - write (save) and quit
:q - quit (fails if there are unsaved changes)
:q! or ZQ - quit and throw away unsaved changes
:Some Additional Features:[Command Mode]
Show invisible characters
Don’t show invisible characters
Show line numbers
Don’t show line numbers
Indent after carriage return
Turn off autoindent
Show values of all possible parameters