vi (visual editor) is a terminal based text editing application, originally created for the Unix operating system. It is a fast, powerful and most popular and classic text editor in the Linux family. It is available in almost all Linux and Unix Distributions, and works the same across different platforms and Distributions. Vi does not contain any menu, instead it uses combinations of keystrokes to accomplish an action. It requires very few resources to run. An improved version of the vi editor which is called the VIM (Vi IMproved) has also been made available now. The vi editor is a full screen editor and has two modes of operation:
Note : In most Linux Systems By default vi is installed. To upgrade it into vim you need to install vim by :
Contents :
The above picture shows the default screen when we start vi standalone. At startup the vi editor is in command mode. Now in order to go to insert mode just press i , and you are in insert mode, which is shown in the bottom-left side of the terminal screen.
after typing something, to go back in the command mode press the Esc button. You can also press the Esc button twice to make sure you are in command mode. Now to type a command in command mode press ' : ' ( shift + ; ) and your command then hit enter. For example to save our typed text
:w file.txt
where w for write the data in file and file.txt is file name where the data would be written. You can also notice the ~(tilde) lines on the left side of the screen which indicate the unused lines of the current buffer or file. We can also open an existing a file or create a new one at startup of vi then pass the file name as argument
2. Commands To Save and exit VI editor :
3. Text Editing (Adding and Inserting text) :
5. Deleting Text :
The following commands allow you to delete text from buffer
6. Replacing Text :
For demonstration let we have three files data1.txt, data2.txt and a c file. Now first we gonna open data1.txt
:vsp tempr.c
now again we are going to open data2.txt file by
:sp data2.txt
To jump from one file to another press Control + w, where you need to press w twice.
13. Opening multiple files within Tabs :
- Command mode : This is the default mode when you open the vi. In command mode whatever the user type is interpreted as a command. This mode enables user to perform administrative tasks such as saving the files, executing the commands, moving the cursor, cutting and pasting the lines or words, as well as finding and replacing it. Command mode Commands are case sensitive, so you should need to use the right letter case.
- Insert Mode : This mode is for inserting text in the file. The user can switch to the Insert mode from the command mode by pressing 'i' on the keyboard. In the insert mode, every character typed is interpreted as input and placed in the file. To return to the command mode you need to press the Esc key.
Note : In most Linux Systems By default vi is installed. To upgrade it into vim you need to install vim by :
$ sudo apt-get install vim
or $ sudo apt install vim
Contents :
- Starting The vi editor
- Commands To Save and exit VI editor
- Text Editing (Adding and Inserting text)
- Cursor movement
- Deleting Text
- Replacing Text
- Undo and Redo text
- Copying and Pasting Text
- Searching Text
- Insert another file into Current opened file
- Control Commands for screen manipulation
- Opening multiple files with Splitted screen
- Opening multiple files within Tabs
- Configuring vimrc file
1. Starting The vi editor
To start the vi editor just open the terminal and type vi or vim on terminal.
$ vi
The above picture shows the default screen when we start vi standalone. At startup the vi editor is in command mode. Now in order to go to insert mode just press i , and you are in insert mode, which is shown in the bottom-left side of the terminal screen.
after typing something, to go back in the command mode press the Esc button. You can also press the Esc button twice to make sure you are in command mode. Now to type a command in command mode press ' : ' ( shift + ; ) and your command then hit enter. For example to save our typed text
:w file.txt
where w for write the data in file and file.txt is file name where the data would be written. You can also notice the ~(tilde) lines on the left side of the screen which indicate the unused lines of the current buffer or file. We can also open an existing a file or create a new one at startup of vi then pass the file name as argument
$ vi test.txt
now to save the modifications and changes of the file, we just need to use :w command. And to quit the vi editor use the :q command. To open a file in read only mode put -R flag in the arguments $ vi -R test.txt
or we can also view $ view test.txt
2. Commands To Save and exit VI editor :
To save file after editing and modifications, go to command mode and use below commands :
Command | Details |
---|---|
:w | save the file (write all the modifications and changes in it.) |
:wq | save the file and quit the editor |
:q | quit or exit the vi editor |
:q! | quit or exit the vi editor without saving the modifications and changes in file. |
:x | save the file and quit the editor (same as :wq command) |
Shift+zz | Save the file and quit |
3. Text Editing (Adding and Inserting text) :
The following commands allow you to insert and add text in current you are working with. Each of these commands puts the vi editor into insert mode.
Command | Details |
---|---|
i | insert text before cursor |
I | insert text at beginneing of the current line |
a | append text after cursor |
A | append text to end of current line |
o | open and put text in a new line below current line |
O | open and put text in a new line above current line |
The following commands allow you to move cursor
Command | Details |
---|---|
Up-arrow or k | moving cursor up |
Down-arrow or j | moving cursor down |
Left-arrow or h | moving cursor left |
Right-arrow or l | moving cursor right |
gg or :0 | move cursor to the first line of file |
shift+g or :$ | move cursor to the last line of file |
0 | move cursor to the beginning of a line |
shift+$ | move cursor to the end of a line |
5. Deleting Text :
The following commands allow you to delete text from buffer
Command | Details |
---|---|
x | Delete single character under cursor |
Nx | Delete N characters, starting with character under cursor, e.g. 10x delete 10 characters |
dw | Delete the single word beginning with character under cursor |
dNw | Delete N words beginning with character under cursor, e.g, d8w delete 8 words |
dd | Delete entire current line |
Ndd or dNd | Delete N lines, beginning with the current line, e.g. 7dd deletes 5 lines |
D | Delete contents of a line after the cursor |
C | Delete contents of a line after the cursor and insert new text. Press ESC key to end insertion. |
6. Replacing Text :
Command | Details |
---|---|
cw | Change word : delete the current word and go to insert mode to put the new word |
r | Replace the character |
R | overwrite characters from cursor onward |
s | Substitute one character under cursor and go to insert mode |
S | Substitute entire line and begin to insert at the beginning of the line |
~ | Change the case( Lower to upper / Upper to Lower) of individual character |
The following command is used to unto and redo
Command | Details |
---|---|
u | undo last change |
Ctrl+R | Redo changes which were undone |
The following commands allow you to copy and paste text.
Command | Details |
---|---|
yy | Copy (yank, cut) the current line |
Nyy or yNy | Copy (yank, cut) the next N lines, including the current line |
p | Paste the copied text after the cursor (lowercase p) |
P | Paste the copied text before the cursor (uppercase P) |
The following commands allow you to search text :
Command | Details |
---|---|
/string | search forword for occurence of string in text |
?string | search backword for occurence of string in text |
n | move to next occurence of search string |
N | move to next occurence of search string in opposite direction |
The following commands allow you to Insert another file into Current opened file.
11. Control Commands for screen manipulation :
The following commands can be used with the Control Key to performs various screen-related functions :
12. Opening multiple files with Splited screen :
Command | Details |
---|---|
:r filename | insert the content of file [filename] after the current line |
:w newfile | write the content of the currently opened file on [newfile] |
:M,Nw filename | write the content from line M to line N on a new file named filename, e.g :2,9w code.c |
:w! filename | write current contents over a pre-existing file named filename |
:s/search/replace/g | The :s/ (substitution command) enables you to quickly replace words or group of words in file. e.g. :s/bricks/walls/g at this example bricks is replaced with walls, where the last g stands for global. |
11. Control Commands for screen manipulation :
The following commands can be used with the Control Key to performs various screen-related functions :
Command | Details |
---|---|
Ctrl + f | move forward one screen |
Ctrl + b | move backward one screen |
Ctrl + d | move down (forward) one half screen |
Ctrl + u | move up (back) one half screen |
Ctrl + l | redraws the screen |
Ctrl + r | redraws the screen, removing deleted lines |
12. Opening multiple files with Splited screen :
In vi we can also open multiple files with splitted screen. Commands for screen splitting :
Command | Details |
---|---|
:sp | For horizontal split |
:vsp | For vertical split |
For demonstration let we have three files data1.txt, data2.txt and a c file. Now first we gonna open data1.txt
$ vi data1.txt
after that in command mode open tempr.c file in vertical split mode:vsp tempr.c
now again we are going to open data2.txt file by
:sp data2.txt
To jump from one file to another press Control + w, where you need to press w twice.
13. Opening multiple files within Tabs :
To open files in Tabs, use -p flag at startup of vi editor. For example
$ vi -p code.c data1.txt data2.txt
this will open all three file with tree tabs
To jump from one file to another tab press Ctrl +Alt + PageUp/PageDown Button.
14. Configuring vimrc file :
vimrc is a configuration file used by vim editor to save settings and configs which is used by the editor at startup. vimrc stands for vim Run-time Configuration. In Unix-based systems (Linux, Mac OS X, etc.), vimrc files are saved to the user's home directory as a hidden file with no filename prefix (.) ~/.vimrc, and in Windows Vim editor, uses a file with the name _vimrc instead of .vimrc. Some of the basic configuration of vimrc file are :
To try it out create a .vimrc file at your home directory and put those lines on your .vimrc file then launch vim again, and now you will see the changes.
So, these are some very basics about vi/vim editor. And I hope you like it.
To jump from one file to another tab press Ctrl +Alt + PageUp/PageDown Button.
14. Configuring vimrc file :
vimrc is a configuration file used by vim editor to save settings and configs which is used by the editor at startup. vimrc stands for vim Run-time Configuration. In Unix-based systems (Linux, Mac OS X, etc.), vimrc files are saved to the user's home directory as a hidden file with no filename prefix (.) ~/.vimrc, and in Windows Vim editor, uses a file with the name _vimrc instead of .vimrc. Some of the basic configuration of vimrc file are :
Command | Details |
---|---|
set ts=4 | set tabs 4 space long |
colo delek | set theme as delek |
set number | show line numbwer |
To try it out create a .vimrc file at your home directory and put those lines on your .vimrc file then launch vim again, and now you will see the changes.
So, these are some very basics about vi/vim editor. And I hope you like it.