Vi editor is available on Linux distributions by default. Vim is the advanced and modified form of vi editor. So, everything available in vi is available in vim too, with additions features like: allowing to edit files using network protocols like SSH and HTTP, edit files inside a compressed archive (gzip, zip, tar, etc) and lot more .You can find more about about Vim editor in it’s official website. Vim is a versatile and powerful text editor. Since, vim is the advanced form of vi editor, I recommend everyone to use vim. To install Vim in Fedora type:
sudo yum install vim
To install in Ubuntu type:
sudo apt-get install vim
1)Starting the Vim editor:
First thing, after installing the vim editor, let’s learn how to open vim editor. Vim or vi editor is a terminal based editor. Go to terminal and type vim or vi to open the editor. If you want to open text or script file using vim type:
Add -r to recover the file that was being edited when system crashed as:
vim -r file_name
2) Two modes of vi editor:
Vi editor has two modes of operation: Insert mode and Command mode. To go to the insert mode press i. You can add text only in the insert mode. To exit from the insert mode hit Esc and you will be on the command mode. To save file, to exit and to enjoy various commands of vim you should be in the command mode.
3) Getting out of vi Editor:
As we already know, there are two types of mode in vim or vi editor: insert and command mode. To exit the vim editor you must be in the command mode. Simply type :q! to exit from the editor. If you want to save the file and exit from the editor type :wq. If you want to only save the file without exiting type :w.
4) Select the text:
To select the text, you need to be in the command mode. Press v(–Visual–) and move with the arrow keys: <up> <down> <left> <right> or with <Home>, <End>, <Page Up > <Page Down>.
To select all the text. In command mode type:
5) Copy, cut paste and deleting text:
+y Copy the selected text. d Cut the selected text. p Paste the copied or cut text. Del Delete the selected text.
6) Searching for text:
In the command mode type / or ?, then type the word you want to search and hit Enter.
/ Search the file downwards for the word specified after the /. ? Search the file upwards for the word specified after the ?.
While searching remember Linux is a case sensitive which means “hello” and “Hello” are two different words in Linux.
7) Undo and Redo:
To undo the previous actions, type “u”.
To Redo, hit “Ctrl+R”.
8) Execute External commands within vim:
You can execute external commands within vim. In command mode type: :!command.
If I want to execute ls command within vim editor then I would type:
|vim||Open the vim editor|
|vim file_name||Open the file with vim editor|
|Esc||Return to the command mode|
|:q!||Exit the vim editor|
|:wq||Save and exit the vim editor|
|:w||Save the file|
|v||Move with arrow and select the text|
|ggVG||Select all the text|
|y+||Copy the selected text|
|d||Cut the selected text|
|p||Paste the copied or cut text|
|Del||Delete the selected text|
|/||Search the file downwards for the word specified after /|
|?||Search the file upwards for the string specified after the ?|
|u||Undo the previous actions.|
|:!command||Execute command within vim editor|
Note : ! is an Exclamatory sign (If it’s unclear).