Skip to main content

Chapter 1 : Basics of Vi editor

 Notes by vishalk17 ( t.me/vishalk17 ) Devops 

=====================================================================

 

1.   Starting vi

Open a file with vi. Type: vi myfile.txt

2.   vi Modes

Command Mode

o   Command mode is the mode you are in when you start (default mode)

o   Command mode is the mode in which commands are given to move around in the file, to make changes, and to leave the file

o   Commands are case sensitive: j not the same as J

Insert (or Text) Mode

o   The mode in which text is created.

o   There is more than one way to get into insert mode but only one way to leave: return to command mode by pressing <Esc>

3.   Setting Basic Options in vi

Displaying Line Numbers

    From Command Mode
  
    :set nu    Display line numbers
  
    :set nonu  Hide line numbers
 
 
 

4.   Basic commands

To repeat and undo:

Commands

Action

u

Undo the last command

.

Repeat the last command

Command to cut, copy and paste:

Commands

Action

dd

Delete a line

yy

(yank yank) copy a line

p

Paste after the current line

P

Paste before the current line

Command to cut, copy and paste in blocks:

Commands

Action

<n>dd

Delete the specified n number of lines

<n>yy

Copy the specified n number of lines

Search a string:

Commands

Action

/string

Forward search for given string

?string

Backward search for given string

 

To move to the start or end of the file:

  • :1 - Moves the cursor to the start of the page.
  • :$ - Moves the cursor to the end of the page.

5.   Exiting vi

o   To exit you must be in command mode-press <Esc> if you are not in command mode

o   exit vi by typing: :x

From Command Mode

 

    ZZ     Write/save then quit

 

    :wq    Write/save then quit

 

    :x   Write/save then quit

 

    :q!    Quit without saving changes to file

 

6.   Basics Summary 

  A Basic vi Session

o   To enter vi, type: vi filename <Return>

o   To enter insert mode, type: i

o   Type in the text: This is easy.

o   To leave insert mode and return to command mode, press: <Esc>

o   In command mode, save changes and exit vi by typing: :x

You are back at the Unix prompt.

 

Comments