What is VI Editor:
We all know in Linux / Unix kernel stores everything as a file. It means there is no concept of extensions (*.pdf,*.mp3.. etc) . So in order to open/edit these files we definitely need one editor. There are many ways to edit the files in Unix but the best way is as per my concern is VI Editor. It is a screen oriented text editor.
History about VI Editor:
The vi editor was developed starting around 1976 by Bill Joy, who was then a graduate student at the University of California at Berkeley.
“ed” or “editor “ was the original Unix text editor. Like other early text editors, it was line oriented and used from dumb printing terminals. But Joy wasn’t happy with it and he wrote a screen editor, which he called “em,” or “ed for mortals” and later he developed it as “ex” a display editor that is a super set of ed and a number of extensions. He then developed vi as a “visual interface” to ex. That is, it allows text to be viewed on a full screen rather than only one line at a time. vi takes its name from this fact.
GUI-mode text editors include gedit and Emacs, both of which have become very common on Linux and other Unixes today
Now a days you would find an improved version of vi editor which is called VIM. Here VIM stands for Vi IMproved.
Starting with VI Editor :
By using VI command we can edit existing files as well as we can create new files.
You can also start VI without a file name, but when you want to save your work ,you will have to tell VI which filename to save it into later.
- vi file name à It opens the existing file ,if the file name doesn’t exist it will create new file.
- Ex: vi test
- vi –R filename à It opens the existing file in read-only mode.
(Note: For checking important files like configuration , always use –R option in order avoid editing on those files)
- When we start with VI for first time, we will see that left side of screen filled with ~ (tilde) symbol which indicates that those lines are blank lines and at the end we can see the file name along with size if the file already exists.
VI Editor Modes :
VI has two modes.
- Insert Mode
- Command mode
The vi editor starts with command mode .
If you want to go for insert mode press “i” or “insert” key and to come out of the insert mode you need to press Esc key which takes you command mode.
So in order to exit from the command mode ,as the name suggests we use some commands like
:q! – Quit from vi editor without saving
:wq – Quit from vi editor after saving your work
:w – Saves your work and stays on vi prompt.
- You can specify a different file name to save to by specifying the name after the :w
For example, if you wanted to save the file you were working as another file name called filename2,you would type : w filename2 .
Command mode Commands
Format of vi commands :
[Count] [Command] [where]
Most commands are one character long, including those which use control characters. The count is entered as a number beginning with any character from 1 to 9.
For example, the x command deletes a character under the cursor. If you type 23x while in command mode, it will delete 23 characters.
Some commands use an optional where parameter, where you can specify how many lines or how much of the document the command affects, the where parameter can also be any command that moves the cursor.
- a – enter insert mode, the characters typed in will be inserted after the current cursor position. If you specify a count, all the text that had been inserted will be repeated that many times.
- i – enter insert mode, the characters typed in will be inserted before the current cursor position. If you specify a count, all the text that had been inserted will be repeated that many times.
- o – creates new line for text entry below the cursor position.
- I – inserts text at the beginning of the current line.
- A – inserts text at the end of the current line.
- O – creates newline for text entry above the cursor position.
Cursor movement commands:
- h – move the cursor to the left one character position.
- l – move the cursor to the right one character position.
- j – move the cursor down one line.
- k – move the cursor up one line.
- w – move the cursor to the start of next word or punctuation mark.
- b – move the cursor to the start of the previous word or punctuation mark.
- W – move past the next space.
- B – move cursor to the beginning of the previous word or punctuation mark.
- e – move cursor to the end of next word or punctuation mark.
- E – move cursor to the end of next word, ignoring punctuation.
- H – move cursor to the top of the screen
- M – move cursor to the middle of the screen
- L – move cursor to the bottom of the screen
- G – Go to the line number specified as the count. If no count is given, then go to the end of file.
- $ – end of the line
- ^ – starting from the line.
Deleting commands :
- x – deletes a single character under cursor
- dd – deletes entire line under cursor
- dw – deleted word under cursor
- db – delete word before cursor
- d^ – deletes from cursor position to beginning of the line.
- d$ – deletes from cursor position to end of the line.
- ndd – deletes n lines starting from the line which is under
- nx – deletes n characters starting from character which is under cursor.
- dnw – deletes n words
- D – deletes all characters in the line and leaves empty line.
Copy ,Paste & undo commands :
- yy or Y – copy line to Buffer
- yw – copy word to Buffer
- znyy – copy n lines into a buffer called z
- p – paste general buffer after cursor
- P- paste general buffer before cursor
- J- join the lines
- u – undo last change
- U – undo all changes on line.
- cw – change word under the cursor
- cc – removes contents of the line ,leaving you in insert mode
- c$ – change to end of line
- r – replace character under cursor followed after pressing r . ex :rc – replaces charcter under cursor with c
- s – Substitutes character with string, leaving you in insert mode
- S – deletes the line under the cursor and replaces with new text ,leaving you in insert mode.
- R – overwrites multiple characters beginning with the character currently under the cursor.
v ? – find a word going backwards ex: ?techm and press enterv / – finds a word going forwards ex: /techm and press enterv n – repeat the last search given by “ ? “ or “ / “v F – finds a character on the line under the cursor going backwardsv f – finds a character on the line under the cursor going forwardsv t – find a character on the current line going forward and stop one character before itv T – find a character on the current line going backward and stop one character before it.v ^ – Matches beginning of linev $ – matches end of linev . – matches any single characterv * – matches zero or more of the previous characterv [ – starts a set of matching, or non-matching expressions… For example: /f[iae]t matches either of these: fit fat fet In this form, it matches anything except these: /a[^bcd] will not match any of these, but anything with an a and another letter: ab ac ad .
- < ,> – Put in an expression escaped with the backslash to find the ending or beginning of a word.
For example: /\<the\> should find only word the, but not words like these: there
The character search searches within one line to find a character entered after the command. The f and F commands search for a character on the current line only. f searches forwards and F searches backwards and the cursor moves to the position of the found character.
The t and T commands search for a character on the current line only, but for t, the cursor moves to the position before the character, and T searches the line backwards to the position after the character.
These two sets of commands can be repeated using the ; or ,command, where ; repeats the last character search command in the same direction, while ,repeats the command in the reverse direction.
Manipulating Character/Line Formatting:
- ~ – switch the case of the character under the cursor.
- < – Shift the lines up to where to the left by one shift width. “<<” shifts the current line to the left, and can be specified with a count.
- – Shift the lines up to where to the right by one shift width. “>>” shifts the current line to the right, and can be specified with a count.
- J – Join the current line with the next one. A count joins that many lines
.(dot) – current line
n – line number n
.+m – current line plus m lines
$ – last line
/string/ – a line that contains “string”
% – entire file
[addr1],[addr2] – specifies a range.
The following example replaces every (caused by the g at the end of the command) of apple with pear.
We can use our keyboard to control the VI console using control key with single character combination
Below are the list keyboard combinations to control your VI screen
ctrl + d – Moves forward ½ screen
ctrl + f – Moves forward one full screen
ctrl + u – Moves backward ½ screen
ctrl + b – Moves backward one full screen
ctrl + e – Moves screen up one line
ctrl + y – Moves screen down one line
ctrl + I – redraws screen
ctrl + g – Display current line number and file information
Settings for VI:
You can customize the way VI behaves upon start up. There are several edit options which are available using the :set command.
To use these commands you have to come in command mode and then type :set followed by any of the following options:
:set ic – Ignores case while searching
:set ai – Set autoindent
:set noai – unset autoindent
:set nu – Displays lines with line numbers on the left side
:set sw – Sets width of a software tabstop .
For example you would set a shift width of 4 with this command : :set sw=4
:set bf – Discards control characters from input
:set term – Print terminal type
:set ro – changes file type to “Read Only”
:set ws – If wrap scan is set ,if the file not found at the bottom ,it will try to search at the beginning of the file
:set wm – If this option has a value greater than zero ,the editor will automatically “word wrap “ .
:set showmode – Display mode on last line of screen
:set noshowmode – Turnoff show mode
:set list – show invisible characters
:set nolist – don’t show invisible characters
:set showmatch – shows matching set of parentheses as they are typed.
:set noshowmatch – Turnoff show match
:set warn – This option warns you if you have the modified file but haven’t saved it yet
:set wi – This option set up number of lines on the window that VI uses . For ex to set
the VI editor to use only 12 lines of your screen you would use this :set wi=12
:set mesg : à Turnoff messages if this option is unset using :set nomesg ,so nobody can bother you while using the editor
I tried to cover almost all commands that we use vi editor .
Please share your feedback .Happy learning 🙂