DEV Community

Cover image for Understanding Vi Editor
Eghelonohor Akpotaire Humphrey
Eghelonohor Akpotaire Humphrey

Posted on

Understanding Vi Editor

What is vi?

vi is a screen-oriented text editor originally created for the Unix operating system.

Who is Bill Joy?

Bill Joy created vi in 1976.

How to start and exit vi

To start vi type

vi filename
Enter fullscreen mode Exit fullscreen mode

What are the command and insert modes, and how to switch from one to the other

Command Mode

Command Mode is the mode that give direct commands such as moving cursor, deleting text, copy and pasting, or saving file. This is usually the mode you are in when you initially open your file with vi.

Insert Mode

Insert Mode is the editing mode of the vi editor. You can type text and press the Enter Key to go to the next line. To enter this mode from the Command Mode just press i.

To switch from Command Mode to Insert Mode

press i.
Enter fullscreen mode Exit fullscreen mode

To switch from Insert Mode to Command Mode

press Escape Key(Esc)
Enter fullscreen mode Exit fullscreen mode

How to edit text

Open a new or existing file with vi filename .
Type i to switch into insert mode so that you can start editing the file.
Enter or modify the text with your file.
Once you're done, press the escape key Esc to get out of insert mode and back to command mode.
Type :wq to save and exit your file.
Enter fullscreen mode Exit fullscreen mode

How to cut and paste lines

dd   To cut

y    To copy

P(Upper case)    To Paste before the cursor
p(lower case)    To paste after the cursor
Enter fullscreen mode Exit fullscreen mode

How to search forward and backward

To search Forward

pressing /(forward Slash) and then typing your search pattern/word.
Enter fullscreen mode Exit fullscreen mode

To search Backward

pressing ?(question Mark) and then typing your search pattern/word.
Enter fullscreen mode Exit fullscreen mode

How to undo

u         To undo
CTRL + r  To redo
Enter fullscreen mode Exit fullscreen mode

How to quit vi

:q to quit (short for :quit)
:q! to quit without saving (short for :quit!)
:wq to write and quit
:wq! to write and quit even if file has only read permission (if file does not have write permission: force write)
:x to write and quit (similar to :wq, but only write if there are changes)
:exit to write and exit (same as :x)
:qa to quit all (short for :quitall)
:cq to quit without saving and make Vim return non-zero error (i.e. exit with error)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)