How to use Vi or Vim editor in Linux
Posted on August 22nd, 2016
Almost all UNIX system available screen-oriented text editor is vi editor. Once we all learn about vi, we can use as fast and powerful editor. Vi uses combination of keystrokes in order to accomplish commands and it has no menus. To create a new file or edit an existing file from scratch we can use vi editor. Also we can use this editor to just read a text file. VIM is the improved version of vi editor. Here VIM stands for Vi IMproved. Some of the other ones are Nvi, Nano, Elvis and Vile.
The vi is commonly considered as the de facto standard in Unix editors because,
1) In all flavors of UNIX system vi is usually available.
2) Implementations of vi are very similar across the board.
3) Few resources are requiring.
4) According to many users, Vi is more user friendly.
Before we work on the VI editor, we need to understand its operation modes. There are two type operation modes.
1) Command mode
2) Insert mode
Only for understanding commands vi editor opens this mode. In this mode we can move the cursor and cut, copy, past the text. Also we can save the changes we have made in this mode. We can use the right letter case because all commands are case sensitive.
This mode is used for inserting text in the file. By pressing “i” on the keyboard we can switch to insert mode. We can use any key taken as input for the file on which we are currently working on, once we are in insert mode. To go back to the command mode and save the changes we have made press the Esc key.
Starting vi editor
There are following way we can start using vi editor.
vi filename – Create a new file if it already does not exist, otherwise opens existing file.
vi -R filename – Opens an existing file in read only mode.
view filename – Opens an existing file in read only mode.
If we specify an existing file, then the editor would open it. If it does not exist, then we can create a new file.
Vi Editing commands
I — Insert at cursor (goes into insert mode)
A — Write after cursor (goes into insert mode)
A — Write at the end of line (goes into insert mode)
ESC — Terminate insert mode
U — Undo last change
o — Open a new line (goes into insert mode)
dd & 3dd –Delete line and Delete 3 lines.
D — Delete contents of line after the cursor
C — Delete contents of line after the cursor and insert new text. Press esc key to end insertion.
dw & 4dw — Delete word and Delete 4 words
cw — Change word
x — Delete character at cursor
r — Replace character
R — Overwrite characters from cursor onward
S — Substitute one character under cursor continue to insert
S — Substitute entire line and begin to insert at beginning of the line
~ — Change case of individual character
Make sure that we press the right command otherwise we will end up making undesirable changes to the file.
The following are useful commands which you can use along with Control Key.
CTRL+d — Move forward 1/2 screen
CTRL+f — Move forward one full screen
CTRL+u — Move backward 1/2 screen
CTRL+b — Move backward one full screen
CTRL+e — Moves screen up one line
CTRL+y — Moves screen down one line
CTRL+u — Moves screen up 1/2 page
CTRL+d — Moves screen down 1/2 page
CTRL+b — Moves screen up one page
CTRL+f — Moves screen down one page
CTRL+I — Redraws screen
Copy and Paste Commands
We can copy and paste lines or words from one place to another place.
yy — Copies the current line
yw — Copies the current word from the character the lowercase w cursor is on until the end of the word
p — Puts the copied text after the cursor
P — Puts the yanked text before the cursor
Moving within a file
These are the default keys for moving in the command mode. Also we can use the arrow keys on the keyboard.
K — Move cursor up
j — Move cursor down
h — Move cursor left
l — Move cursor right
Saving and Closing the file
To exit from the editor in the command mode we should save the changes to the files.
Shift+zz — Save the file and quit
:w — Save the file but keep it open
:q — Quit without saving
:wq — Save the file and quit
There are some advanced commands that simplify day-to-day editing and allow for more efficient use of vi.
J — Join the current line with the next one. A count joins that many lines.
<< — Shifts the current line to the left by one shift width.
>> — Shifts the current line to the right by one shift width.
~ — Switch the case of the character under the cursor.
^G — Press CNTRL and G keys at the same time to show the current filename and the status.
U — Restore the current line to the state it was in before the cursor entered the line.
u — Undo the last change to the file. Typing ‘u’ again will re-do the change.
:f — Displays current position in the file in % and file name, total number of file.
:e # — Use to toggle between two opened files.
:n — In case you open multiple files using vi, use :n to go to next file in the series.
:p — In case you open multiple files using vi, use :p to go to previous file in the series.
:N — In case you open multiple files using vi, use :N to go to previous file in the series.
:r file — Reads file and inserts it after current line.
:nr file — Reads file and inserts it after line n.
:f filename — Renames current file to filename.
:w filename — Write to file filename.
:e filename — Opens another file with filename.
:cd dirname — Changes current working directory to dirname.
If you need any further assistance please reach our support department.