Learn Linux: use vi command

Vi command line

Vi command overview

In other post, we showed you some most useful Linux commands, you can check that article here. Today, we will show you detail about a very useful command when we working with linux. That is vi command. VI command is on almost every UNIX and Linux system. This tutorial will show you how to

  • Navigate a document using vi command
  • Use basic vi modes
  • Insert, edit, delete, copy and find text by vi command


You must have a basic knowledge of linux and a working Linux system or you can go here to practice online. Sometimes different versions of a program will bring out differently output, so your output may not always look exactly like the example show here.

Let’s figure out vi command in detail

Start vi:

vi filename
vi myfile.txt

Exit vi:

Pressing Esc will leave insert mode and return you to command mode where these commands should work.

Quit editing the file and skip all changes.
Write the file and quit whether modified or not.It's generally safer to omit the ! unless you know what you're doing.
Run a shell command. 
:! ls 
After hit Enter the ls result will show and hit enter again to return vi editing. 
This will reload the file, discard all changes you have made.
Write the file if it has beend modified. Then exit. This is very common way for normal vi exit.


1. When you type (:), the cursor will move to the bottom line of your screen then you can type in the command and parameters.
2. If you omit the (!) from the command. you may receive an error message such as saying changes have not beend saved or the output file cannot be written
3. The (:) commands have longer form are (:quit, :write, :edit). Use it if you want

Navigate vi

These commands help you move around in a file

Move left one character on the current line
Move down to the next line
Move up to the previous line
Move right one character on the current line
Move to the next word on the current line
Move to the next end of word on the current line
Move to the previous beginning of the word on the current line
Ctrl + f 
Scroll forward one page
Ctrl + b 
Scroll backward one page


If you type a number before any of these commands, then the command will be executed that number of times. E.g: 5l will move right 5 characters.

Moving to lines

Moves to a specific line in your file.
E.g: 3G will move to line 3. With no parameter, G will go to the last line of the file
Moves relative to the top line on the screen.
E.g: 3H moves to the line currently to the 3rd line from the top of your screen.
Is like H, except that movement is relative to the last line on screen
E.g: 3L moves to the line currently to the 2nd line from the bottom of your screen.

Search text and line

Use / to search forward for ocurrence of string in text
Use ? to search backward for ocurrence of string in text
Move to the next occurrence of search string
Move to next occurrence of search string in opposite direction
Returns line number of current line at bottom of screen
Returns the total number of lines at bottom of screen
Ctrl + g
Provides the current line number, along with the total number of lines, in the file at the bottom of the screen

Text handle

Each of these commands puts the vi editor into insert mode; thus, the key must be pressed to terminate the entry of text and to put the vi editor back into command mode.

Insert text before cursor, until  hit
Insert text at beginning of current line, until  hit
Append text after cursor, until  hit
Append text to end of current line, until  hit
Open and put text in a new line below current line, until  hit
Open and put text in a new line above current line, until  hit
Delete single character under cursor. If you type a number before x, it will delete that number of characters starting with character under the cursor 
Delete the single word beginning with character under cursor. dNw (N is a number), this will delete N words beginning with character under cursor; 
E.g, d5w deletes 5 words
Delete the remainder of the line, starting with current cursor position
Delete entire current line. Ndd (N is a number), this will delete N lines, beginning with the current line; 
E.g., 5dd deletes 5 lines

Related Post


  • Nilangshu Roy

    January 3, 2016

    Very useful. Thanks

  • svd

    January 3, 2016

    How to copy and paste text into other file? Into/from other program?

    • webtechsharing

      January 3, 2016

      It will be simple.
      1. If you copy from other file in you local PC it will be easy just copy Ctrl + c then in vi edit mode Shift + Insert or just click Right Mouse
      2. If you want to copy text from other file in your server. Please follow this step:

      2.1 Copy in Command mode

      • Type :tabnew /path/to/the/file/has/text/need/to/copy
      • Move the cursor to the line that needs to be copied and type yy or Y, if you need to copy 2 lines just type 2yy or 2Y
      • If you want to copy all lines from the current line to the end of the file, just move cursor to the first line which needs to be copied and type yG
      • If you only want to copy one word, just move cursor to very first of the word then type yw
      • If you only want to copy from current cursor to the end of the line, just type y$
      • After copied, quit the file with :q

      2.2 Paste in command mode

      • Move cursor to the start pasting point, type p

      I hope this helps. Kind regards.


Leave a Reply