gb
  English
Phone:  +1 (217) 636-4164
Skype:  onegbits
  1. Home
  2. Blog
  3. Vi Text Editor

Vi Text Editor

Vi Text Editor

28

Jan, 18

395

There are different tools and methods in Linux operating systems to edit text files. The most popular text editor tools in Linux are vi, Nano, and VIM which is Vi Improved. In this tutorial we will guide you how to use Vi text editor in Linux environment.

You can use vi to create new file, edit an existing file, and read existing file.

Starting the vi editor

Using the following command, you can create a new file and enter vi environment.

Command syntax:

vi filename

for example:

vi 1gbits.txt

in above command we created a file named 1gbits.txt. Please note, if the file name that you provide after this editor exists, it will open the file otherwise, it will create a file with specified name.The output of above command will be like below:

1_6

You will see “~” character at the beginning of each line. This character shows an unused line. If you face a line that is not started with “~” character and has no content, this means that you have used invisible characters such as Space, Tab, etc. Up to here, you are able to open a file with this editor. Now we will explain its basic concepts. 

Vi operation modes

To work on VI editor, you need to understand its operation modes. They can be divided into two main parts. 

Command mode:

This mode allows you to do management tasks. You save changes, run commands, move the cursor, cut, copy, paste the text and lines, find and replace words in this mode. Any word that you write in this mode will be interpreted as command. 

Insert mode:

This mode is for inserting text in the file. Once you are in Insert mode, any key would be taken as an input for the file on which you are currently working. The default mode for Vi is command mode. You can switch to the Insert mode from the command mode by pressing 'i' on the keyboard. To return to the command mode and save the changes you have made you need to press the Esc key. You will see “Insert” at the bottom of the editor when you are in Insert mode. See the following image:

2_5

Notice: If you do not know which mode you are in, you can press ESC key twice and this will take you to Command mode.

Exiting Vi

In order to exit from Vi editor you must enter command mode using ESC key and type :q.

If you have made changes in file and you want to ignore them, you must use :q! command in command mode.

3_2

You can save your changes using :w command.

You can combine save and exit command together using :wq command. You can use ZZ key combination instead of :wq command.

To save file with another name, you can specify new file name after :w command as bellow:

:w azar2.txt

Note: Press Enter key after save and exit commands.

Moving within a file

You need to be in the command mode to move within a file. The default keys for navigation are mentioned below:

Keystroke                                Use

 K                                            move cursor one line up

 j                                              move cursor one line down

 h                                             move cursor one character to left

 l                                              Move cursor one character to right.

Notice: Commands are case sensitive. You should use the right letter case.

You can specify the times of command execution using number before the command. For example you can use 2j command to move cursor one line down from current line.

Other methods to move within a file:

You can use below commands to move around in files too.

$              move cursor to end of line

0 or |         move cursor to beginning of line

W              moves cursor to next word

b              move cursor one word to left

)              Move cursor to beginning of next sentence (delimeted by ".", "?" or "!")

(              Move cursor to beginning of current sentence

}              Move cursor to beginning of next paragraph

{              Move cursor to beginning of current paragraph

G              Moves cursor to end of file                

1G             Moves cursor to the first line of file

nG             Moves cursor to nth line.

:n             moves cursor to line n.

H              Move cursor to top of page

nH             Moves cursor to line n from top of current page

M              Moves cursor to middle line of screen

L              Move cursor to bottom of page

nL             Moves to nth line from the bottom of the screen

E              Move to the end of Blank delimited word

fc            Move cursor to next c character. You can replace any character with c.

Fc             Moves cursor back to c.

File editing in Vi

To edit files you must enter Insert mode. There are different methods to enter Insert mode:

Command                explanation

i                      Insert before cursor

I                      Insert before line

a                     Append after cursor

A                 Append after line

o                 Open a new line after current line

O                 Open a new line before current line

Things to do while in Insert Mode:

CTRL+d                  Move forward ½ screen

CTRL+f                   Move forward one full screen

CTRL+u                  Move backward ½ screen

CTRL+b                  Move backward one full screen

CTRL+e                  Moves screen up one line

CTRL+y                  Moves screen down one line

Deleting Text in Vi

Use the bellow commands to delete lines and words in a file:

Command                                     explanation

x                      Delete character to the right of cursor

X                                           Delete characters before corsure

dw                                          deletes a word                    

dd                                          Delete current line 

D                      Delete to the end of the line

d$                                          Deletes from the cursor to the end of the line

As mentioned above, you can also specify how many times  you want the commands to run. For example, the 2x command removes two characters from the cursor. The 2dd command removes two lines from the current location of the cursor.

Copy and Paste Commands

You can copy lines and words and paste them some where else using below commands:

Command              Explanation

yy   Copy current line into "unnamed" storage buffer.

P                 Paste unnamed storage buffer after current line.

P                  Paste unnamed storage buffer before current line.

Advanced commands in Vi:

There are advanced Vi commands that make editing easier and allow for the effective use of vi:

Command                Explanation

J                   Join lines

u                   Undo last change

G^               G key along with CTRL key shows current file name and its status.

~                 Toggle upper and lower case

:f file            Renames current file to file

:f                 show your position in file and file name.

:w filename         writes file in a file called filename

:e filename         opens an other file called filename

:cd dirname        changes current directory to dirname

nG             move cursor to line n  

control-f      scroll forward one screen   

control-b      scroll backward one screen  

i              insert to left of current cursor position (end with ESC)  

a              append to right of current cursor position (end with ESC)    

dw             delete current word (end with ESC)  

cw              change current word (end with ESC)  

r              change current character   

~              change case (upper-, lower-) of current character       

dd             delete current line   

D              delete portion of current line to right of the cursor 

x               delete current character    

ma              mark currrent position   

p              dump out at current place your last deletion (``paste'')      

u              undo the last command    

J              combine (``join'') next line with this one      

:w             write file to disk, stay in vi  

:q!            quit VI, do not write file to disk,  

 ZZ             write file to disk, quit vi   

:r filename    read in a copy of the specified file to the current                    buffer 

/string         search forward for string (end with Enter)    

?string        search backward for string (end with Enter)    

n              repeat the last search (``next search'')         

:s/s1/s2       replace (``substitute'') (the first) s1 in this line by s2  

Congratulations! You have learned how to use VI text editor

If you are facing any problem with the installation, fell free to comment here. We will help you to solve the issue.

 

 

 

 

 

 

    

 

 

 

 

 

Leave A Comment
We are with you

24 X 7 X 365

+1 (217) 636-4164

©2018 1Gbits. All rights reserved