en
  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

498

There are various 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 V i Im proved. In this tutorial we will guide you how to use Vi text editor in Linux environment.

You can use it to create a 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 the environment.

Command syntax:

or filename

for example:

vi 1gbits.txt

In the above command, we created a file called 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 the above command will be like below:

1_6

You will see the "~" 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 can open a file with this editor. Now we will explain its basic concepts. 

Vi operation modes

To work as a VI editor, you need to understand its operating 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 the Insert mode, any key would be taken as an input for the file that you are currently working on. 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've made you need to press 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 the 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 a file with another name, you can specify a 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 in 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 the cursor one character to right.

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

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

Other methods to move in a file:

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

$ move cursor to end of line 

0 or | move cursor to the beginning of line

W moves cursor to next word

b move cursor one word to left

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

(Move cursor to the beginning of current sentence

} Move cursor to the beginning of the next paragraph

{Move the cursor to the beginning of the current paragraph

G Moves the cursor to the end of the file                

1G Moves the cursor to the first line of the file

nG Moves the cursor to the nth line

.: Moves the cursor to line N.

H Move cursor to the top of page

nH Moves the cursor to the line from the top of the current page

M Moves the cursor to the middle of the screen

L Moves the cursor to the bottom of the page

n Moves

to the end of the blank line delimited word

fc Move the 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 the cursor

I Insert before line

A Append after the cursor

A Append after line

o Open a new line after the current line

O Open a new line before the 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 the screen down one line

Deleting Text in Vi

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

Command explanation 

x                       Delete character to the right of the cursor

X Delete characters before corsure

dw delete a word                    

dd Delete current line 

D Delete

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 somewhere else using below commands:

Command Explanation

yy Copy current line into "unnamed" storage buffer.

P Paste the unnamed storage buffer after the 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 and

Undo last change

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

~ Toggle upper and lower case

: f file Rename current file to file

: show your position in file and file name.

: w filename writes file in a file named filename

: e filename opens another file named 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);  

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

dw delete current word (end with ESC);  

cw change current word (end with ESC);  

change current character   

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

dd delete current line   

D delete part of the current line to the right of the cursor 

x delete current character    

ma currrent position   

dump out in the 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 the 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 for the 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, feel free to comment here. We will help you to solve the issue.

 

 

 

 

 

 

    

 

 

 

 

 

Leave A Comment