vi editor commands Linux / Ubuntu tutorial
It is most used editor in linux , there some advance version of vi also , that is VIM, VIM stands for Vi Improved.
Operation modes in vi editor
They can be divided into two main parts.
vi always starts in the command mode.
You can, move the cursor and cut, copy, paste the text,finding and replacing text.
This mode is for inserting text in the file.
By pressing ‘i’ on the keyboard we will enter in Insert mode and By pressinf Esc we come out from insert mode.
Starting the vi editor
To launch the VI Editor – Open the Terminal (CLI) and type
vi <filename_NEW> or <filename_EXISTING>
It will Creates a new file if it already does not exist, otherwise opens an existing file.
You will notice a tilde (~) on each line following the cursor it represents blank line.
Save or Exting from Vi editor
Before saving or exiting from editor you need to first press Esc key (To come out of the insert mode), then use below key.
|:w||Save the file but keep it open|
|:q||Quit without saving|
|:wq||Save the file and quit|
|Shift+zz||Save the file and quit|
If your file has been modified in any way, the editor will warn you of this, and not let you quit. To ignore this message, we use :q! ( also use may use :wq! i.e save and exit ignoring warn message )
Moving within a file
The default keys for navigation are mentioned below else; You can also use the arrow keys on the keyboard.
|k||Move cursor up|
|j||Move cursor down|
|h||Move cursor left|
|l||Move cursor right|
Most commands in vi can be prefaced by the number of times you want the action to occur.
Before editing the file you need to be in insert mode.
|i||Inserts text before the current cursor location|
|I||Inserts text at the beginning of the current line|
|a||Inserts text after the current cursor location|
|A||Inserts text at the end of the current line|
|o||Creates a new line for text entry below the cursor location|
|O||Creates a new line for text entry above the cursor location|
Below command used to deleted characters and lines in an open file −
|x||Deletes the character under the cursor location|
|X||Deletes the character before the cursor location|
|dw||Deletes from the current cursor location to the next word|
|d^||Deletes from the current cursor position to the beginning of the line|
|d$||Deletes from the current cursor position to the end of the line|
|D||Deletes from the cursor position to the end of the current line|
|dd||Deletes the line the cursor is on|
2x deletes two characters under the cursor location and 2dd deletes two lines the cursor is on.
Note: You should be in the “command mode” to execute these 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|
|U||Undo all changes to the entire line|
|o||Open a new line (goes into insert mode)|
|3dd||Delete 3 line|
|D||Delete contents of line after the cursor|
|C||Delete contents of a line after the cursor and insert new text. Press ESC key to end insertion.|
|4dw||Delete 4 words|
|x||Delete character at the cursor|
|R||Overwrite characters from cursor onward|
|s||Substitute one character under cursor continue to insert|
|S||Substitute entire line and begin to insert at the beginning of the line|
|~||Change case of individual character|
Now moving to Advance vi editor tutorial
Searching and Replacing in vi editor
Finding a Character String
The vi editor has two kinds of searches :
string and character. For a string search, the / and ? commands are used.
To search a character string, just type / followed by the string you want to search for, and then press Return (Enter key). vi positions the cursor at the next occurrence of the string.
For example, type /sata followed by Return, to find the string “sata,”
Type n to go to the next occurrence of the string. Type N to go to the previous occurrence.
To search backward in a file, you can use ? instead of /. In this situation, the directions of n and N are reversed.
Searches normally are case sensitive: a search for “sata” will not find “Sata” If you want vi to ignore case during a search, type :set ic To change it back to the default, case-sensitive mode, type :set noic.
To match any character, type a period (.) in the string at the location to be matched. For example, to find the next occurrence of “stringsearch” or “charsearch,” type:
For example, to find all strings beginning with a through z and ending with “stringsearch” and to find all occurrences of the string “charsearch”, type:
Replacing a Character String
The basic command form is:
Then press the Return key.
NOTE: The g stands for globally
prefix % the substitute command to make changes on all lines:
Replace All:Find each occurrence of ‘foo’ (in all lines), and replace it with ‘bar’.
Find and Replace with Confirmation
Find a word called ‘linux’ and replace with ‘ubuntu’, but ask for confirmation first, enter:
Find and Replace Whole Word Only
Find whole words exactly matching ‘LINUX’ to ‘ubuntu’; and ask for confirmation too:
Case Insensitive Find and Replace
Find ‘LINUX’ (match LINUX, linux, Linux and so on) and replace with ‘ubuntu’:
Same command with confirmation
Case sensitive Find and Replace
Same command with confirmation
Find and replace In the Current Line Only
Replace All Lines Between line 8 and line 12?
Find ‘UNIX’ and replace with ‘Linux’ all lines between line 8 and line 12, enter
vi and vim set command
vi – show lines numbers
Displays lines with line numbers on the left side
And to hide line numbers in vi, use this command
vim – wrap long lines
To wrap long lines in vim, use the vim “set wrap” command
if you don’t want to wrap long lines use this vim set command:
vi – ignoring case in searches
Ignores the case when searching
this command will distinguish between uppercase and lowercase characters in your searches.
Running Commands in vi editor
To run a command, you only need to go to the command mode and type :! command.
For example, you can type :! ls this will display output on screen.