Vim editor is an improved version of vi file editor in linux. With vim editor, one can easily navigate between texts, edit/delete the texts in a file easily when comparing with vi editor. In this following post, you will learn what is vim editor and how to use it in linux.
Vim editor works with data in the buffer, when the file is opened using vim editor, vim editor reads the entire file contents and store it in memory buffer. Vi editor is the default file editor in Unix/Linux platform and one have to install vim editor using yum install vim in some versions of linux if not installed by default.
The vim editor has two modes of operations:
1. Command/Normal mode
2. Insert mode
1. Command/Normal mode:
When you open a file using vim command, the file will be open in command/Normal mode where we can navigate between lines or see the entire commands without editing the texts.
In the bottom right corner, based on the cursor location, the present cursor’s line number and column along with percentage of files displayed in the screen will be shown as below.
To display the line numbers for text the file from the command mode, give “:set nu” and enter.
While in the Command/Normal mode, you can use below shortcuts for navigation between texts.
Option | Description |
h | move the cursor left by one character. |
j | move the cursor down by one line (the next line in the text). |
k | move the cursor up by one line (the previous line in the text). |
l | move the cursor right by one character |
PageDown (or Ctl-f) | move the cursor forward by one screen of data. |
PageUp (or Ctl-b) | move the cursor backward by one screen of data. |
G | move the cursor into last line in the buffer. |
num G | move the cursor into the line number num in the buffer. |
gg | move the cursor into first line in the buffer |
:q | quit if no changes have been made the buffer data. |
:q! | quit and discard any changes made the buffer data. |
:w filename | save the file under a different filename. |
:wq | save the buffer data the file and quit. |
dd | delete a line where the cursor is |
yw | copy the word where the cursor is |
y$ | copy the text where the cursor is up the end of the line. |
p | paste the yanked(copied) text |
dw | Delete the word at the current cursor position. |
d$ | Delete to the end of the line from the current cursor position. |
J |
Delete the line break at the end of the line at the current cursor position.
|
a | Append data after the current cursor position. |
A |
Append data to the end of the line at the current cursor position.
|
r char |
Replace a single character at the current cursor position with char.
|
R text | Overwrite the data at the current cursor position with text |
shift+g | To move to the last line of the file. |
shift+a | To move the cursor to after the last word of the line. |
r | To redo the text from command mode. |
u | To undo the text from command mode. |
O | To insert a new line above the cursor position. |
o | To insert a new line below the cursor position. |
From the Command/Normal mode, you can switch into visual mode by pressing “v” key.
And can highlight or select the text using Up/Down or Left/Right arrow keys as below.
While in “Visual mode” if the “c” key is pressed, the highlighted or selected text will be cutted and can be pasted using “Esc” and “p” into the location where you want.
Note : “Backspace” key will not work when you are in Normal or Command mode. But the “Del” key will work while in Normal or Command mode. You can use “x” key to delete the text where the cursor is similar to vi editor when in command or normal mode.
Search and Replace the text from command mode of vim editor:
To search a text from the command mode, press “/”(forward slash) and give the search keyword and press “Enter” key. To navigate to the search results, you can use “n” key to move the cursor into search text.
To replace one word with another word, For example, snake instead of python as below using below option from the command mode.
:s/python/snake/g to replace the word python into snake in a present cursor line.
:#,#s/python /snake/g to replace all occurrences of “python” between two line numbers.
:%s/python/snake/g to replace all occurrences of “python” in the entire file.
:%s/python/snake/gc to replace all occurrences of “python” in the entire file, but prompt for each occurrence.
2. Insert mode :
After opening a file into command mode, you can get into insert mode to modify the existing text or for adding text after pressing “i” from the keyboard. Once the texts has been added or modified, for saving the file, press “Esc” to go into command mode and “:wq!” to write the modified file and quit the vim editor.
How do you feel about this post? Drop your comments below..