How To Use Vi Text Editor An Easy Guide

How To Use Vi Text Editor An Easy Guide

 
 

Introduction to vi

Vi is a command based & standard Unix text editor. It is fast and powerful you need not remove your fingers from your keyboard while editing a file in it. It has short commands to manipulate the text. And it is available for nearly all Unix like operating systems. Originally it was developed by bill joy in 1976. Later various versions are released with improvement in it.


Starting vi in your terminal

1. Open your terminal

2.Type vi filename.txt

  • If filename.txt exist in your system it will open with the content in it
  • Otherwise, it will create a new file with the name filename & .txt extension
    and opens in your terminal for editing

Vi Operating modes

So before start using vi as your text editor you should have some basic understanding of how it operates in different modes. The editor operates in the following two modes –

1. Command mode 

  • It is the default mode of the editor and  when you first start a file in vi it opens in command mode
  • In this mode commands are given to accomplish different operations on files like moving cursor around text, deleting text, saving the content of file, etc.
  • vi commands are case sensitive that means you can’t use uppercase letters
    instead of lowercase and vice versa.

2. Text or Insertion mode

  • This is the mode in which every character typed is added into file
  • You can go into insert mode in many ways but have to exit with<esc> key

Moving cursor around text

You can use the following keys to move the cursor around the text. So before using these keys in vi to navigate around text you will have to go into command mode.

Use the keys in command mode

CommandDescription
kMove one line up
jMove one line down
hMove one character left
lMove one character right
bLeft one word
wRight one word
0Move cursor to the beginning of the current line
$Moves cursor to the end of current line

Note: On some Unix or Unix based platforms arrow keys are allowed to use for moving cursor around the text 


Entering, modifying and changing text

The following keys or combination of keys can be used to enter, modify or change the inserted text

Use these keys in command mode

CommandDescription
iStart text entry mode
xDelete one character
ddDelete a line
dwDelete a word
rReplace a character
ROverwrite text

Exiting vi

Once you are done writing, reading or modifying a file in vi you can exit by using the following combination of keys. And before that, you have to activate command mode for vi. And you just have to press enter after the commands starting with a :(colon)

Use these commands in command mode

CommandDescription
ZZWrite(if changes are made) then quit
:wqWrite, then quit
:qQuit(work if there are no changes made in the file)
:q!Quit without saving the changes that are made into file

Searching, copying and pasting text in vi

Use the following in command mode

CommandDescription
/stringSearch forward (top to bottom) for text
?stringSearch backward (bottom to top) for text
yyCopy the current line
5yyCopy 5 lines begin with the current line
p (in small)Paste that text after the cursor position
P (in capital)Paste that text before the cursor position

Set line number and wrap margin in vi

Some time it is very useful to using line number while writing text

Use in command mode

CommandDescription
:set nuWill displays line number
:set nonuWill hide line number
:set wm=nSet the margin n spaces from the right side of the screen
:set wm=0Turn off the wrap margin

Manipulating editor screen in terminal

You can use the following window commands in vi editor to move up or down screen several times

Use in command mode

CommandDescription
ctrl+fMoves forward one screen
ctrl+bMove backward one screen
ctrl+dMove down (forward) one half screen
ctrl+uMove up (back) one half screen
ctrl+lRedraw the screen
ctrl+rRedraw the screen, removing deleted lines

 


Using vi editor

1.Open terminal type vi filename.txt

2. Now the file is open in command mode press i and start writing your text

3. You are currently in insertion mode press <esc> to switch to command mode Save Now press ZZ or:wq and enter to save the file.

Now it will return to the terminal by saving the contents of the file. I think the list of commands given above in the article is sufficient to start with it. You should try the commands on your own system so that you get more familiar with it.

Previous How To Compile And Run C Or C++ Code In Linux/Unix
Next File Ownership And Permissions In Linux/Unix

About author

You might also like

Uncategorized 0 Comments

Package Management In Arch Linux

Using Arch Linux as your operating system has its own advantage. It uses a rolling release model which means there is no major release of a new version instead a

Uncategorized 0 Comments

How To Set Password On A RAR Archive In Linux?

Files inside a RAR archive can have important data. So you may want to protect it from the unusual access. In Linux, the rar command provides a feature to set

Uncategorized 0 Comments

How To Install Apache Tomcat In Ubuntu 20.04 LTS?

Apache Tomcat or simply Tomcat is a web server and servlet container that is used to serve Java applications. It provides a pure Java HTTP server environment to run these

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply