Vim Text Editor
Share on:Edit on:Table of Contents
- Introduction
- Starting VI Editor
- Inserting Text
- Saving and Exiting
- Copying, Pasting and Deleting
- A Simple Example Using VI Editor
Introduction
VIM is improved version of text editor called VI. The name VI is derived from the command visual, which was used in line editor called ex in older days.
VI is a powerful text editor standardized by POSIX and Single Unix Specification (SUS). So It comes with every Linux distribution released.
VI or VIM editor operates in two modes.
- Command Mode
- Insert Mode
Starting VI Editor
Command vi followed by file name is used to start VI editor.
When we open VI text editor, the default mode will be command mode. We can’t write anything into file in this mode.
me@linux ~ $ vi prog.c
If the file prog.c doesn’t exist, it will be created. If file exists, the content of the file will be shown. The editor looks like following:
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"prog.c" [New File] 0,0-1 All
Inserting Text
Simply pressing the key i
changes vi editor from command mode to insert mode. You should see the text – INSERT – at the bottom of terminal. Now we can enter text into the file.
There are other ways to get into insert mode. Press Esc
to return to command mode and then follow these commands. Note that vi editor should be in command mode to enter any command.
Command | Operation |
---|---|
i | Inserts text to the left of the cursor. |
I | Inserts text at the beginning of the line, no matter where the cursor is positioned on the current line. |
a | Begins inserting after the character (append) on which the cursor is positioned. |
A | Begins inserting at the end of the current line, no matter where the cursor is positioned on that line. |
o | Begins inserting text on a new, empty line, below the current line. This is the only command that will allow you to insert text BELOW the LAST line of the file. |
O | Begins inserting text on a new, empty line, above the current line. This is the only command that will allow you to insert text ABOVE the FIRST line of the file. |
r | Replace single character under cursor position. |
R | Replace all characters, starting under cursor position, until Esc is pressed. |
In insert mode we can write text just like any other editors. Arrow keys can be used for cursor movement. Backspace and Delete keys can be used for removing text.
Saving and Exiting
There are certain commands we use to save the content and exit this vi editor. We use these commands in command mode.
Command | Operation |
---|---|
:w | Saves Current File |
:q | Quits Editor |
:q! | Forces the Editor to Quit. Changes may lost |
:wq | Changes Saved and Quit |
:wq! | Forces Editor to Save Content and Quit |
Copying, Pasting and Deleting
Though we can manage editing files with just above commands and operations, following commands are used for quick and efficient editing.
Command | Operation |
---|---|
D | Delete Text from Cursor Position to End of the Line. |
dd | Delete Entire line at Cursor |
ndd | Delete n number of lines. Ex: 2dd, 5dd etc. |
x | Deletes/Cuts Character at Cursor Position |
X | Deletes/Cuts Character Before Cursor (Just like Backspace). |
y | Copy Text at Cursor |
Y | Copy Entire Line at Cursor |
p | Paste Text at Cursor Position |
P | Place Text Before Cursor Position |
A Simple Example Using VI Editor
Let’s create a simple C Program file called prog.c. Open the Terminal.
Enter the command vi prog.c and it looks like following.
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"prog.c" [New File] 0,0-1 All
Press i. Now VI editor enters into insert mode. You should see – INSERT – at the end.
Now type the program (or any content). Here I am writing a simple Hello World! program. This looks like following.
#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
return 0;
}
~
~
~
~
~
~
-- INSERT -- 5,11-18 All
Next step is to save the file. Before we enter any command, we need to change VI editor into command mode. Press Esc
key. – INSERT – must be gone. Now type :w
and press Enter. This should save your file. To quit vi editor enter the command :q
.