When using the vi text editor, which of the following keys

The vi text editor has three modes: command, input, and ex.

Command mode

When starting, vi begins in command mode. If you are ever unsure which mode you're in, press Esc to return to command mode. In command mode, you can move around with the arrow keys, or by using the vi movement keys, as follows:

h left
j down
k up
l right

Several vi commands are listed in the table below:

Command Action
Ctrl-b Go back one page
Ctrl-f Go forward one page
x

Delete the character the cursor is on

Shift-x

Delete the character before the cursor

dd Delete the current line
Shift-d

Delete everything from the cursor to the end of the line

u

Undelete a line you just deleted

Shift-u

Undo all changes to the current line

Shift-z-z

Save your file and exit the vi editor

Input mode

The input mode lets you insert or append text. To insert text before the cursor's current position, in command mode, press i. Similarly, to append after the cursor, you can type a. Remember that you can't move around with the cursor keys in this mode. When you're done entering text, press Esc to go back to command mode.

Ex mode

The ex mode is an extension of command mode. To get into it, press Esc and then : (the colon). The cursor will go to the bottom of the screen at a colon prompt. Write your file by entering :w and quit by entering :q. You can combine these to save and exit by entering :wq. However, if you're finished with your file, it's generally more convenient to type Shift-z-z from command mode.

The lowly and now middle-aged vi editor is still a favorite among *nix system administrators and remains an essential tool for command-line file creation and editing.

Gray at the temples and in need of reading glasses, the middle-aged Vi (pronounced vee-eye) editor, released in 1976, is still a system administrator's best friend.

This advanced, yet simple to use, command-line editing program shows no sign of slowing down. You can use it to edit configuration files, create a grocery list, write a letter home to ask for money, create a new script, or even edit source code. 

Vi is often a symbolic link to Vim (Vi Improved) or an alias to Vim, an enhanced version of Vi.

[ Get the Vim cheat sheet. ]

It’s easy to invoke Vi. At the command line, you type vi <filename> to create a new file, or to edit an existing one.

$ vi filename.txt

Vi edit modes

The Vi editor has two modes: Command and Insert. When you first open a file with Vi, you are in Command mode. Command mode means you can use keyboard keys to navigate, delete, copy, paste, and do a number of other tasks—except entering text.

To enter Insert mode, press i. In Insert mode, you can enter text, use the Enter key to go to a new line, use the arrow keys to navigate text, and use vi as a free-form text editor. To return to Command mode, press the Esc key once.

[ Looking for a different text editor? Download the Emacs cheat sheet. ]

In Vi's Command mode, almost every letter on the keyboard has a function.

To save a file, you must first be in Command mode. Press Esc to enter Command mode, and then type :wq to write and quit the file. The other, quicker option is to use the keyboard shortcut ZZ to write and quit. In Vi, write means save, and quit means exit. If you’ve made mistakes along the way while editing and want to back out (abandon) all non-saved changes, enter Command mode by pressing Esc and typing :q! This command quits without saving any changes and exits Vi.

Note: Always make a copy of an existing file prior to editing with Vi or any editor. This is especially critical when editing system and configuration files.

Vi shortcuts

The best way to learn Vi is to create a new file and try it out for yourself. Feel free to use the common keyboard shortcut list below to help you learn Vi’s extensive vocabulary. This list of shortcuts is by no means exhaustive, but they will enable you to edit files and learn Vi in a short amount of time.

  • $ vi <filename> — Open or edit a file.
  • i — Switch to Insert mode.
  • Esc — Switch to Command mode.
  • :w — Save and continue editing.
  • :wq or ZZ — Save and quit/exit vi.
  • :q! — Quit vi and do not save changes.
  • yy — Yank (copy) a line of text.
  • p — Paste a line of yanked text below the current line.
  • o — Open a new line under the current line.
  • O — Open a new line above the current line.
  • A — Append to the end of the line.
  • a — Append after the cursor’s current position.
  • I — Insert text at the beginning of the current line.
  • b — Go to the beginning of the word.
  • e — Go to the end of the word.
  • x — Delete a single character.
  • dd — Delete an entire line.
  • Xdd — Delete X number of lines.
  • Xyy — Yank X number of lines.
  • G — Go to the last line in a file.
  • XG — Go to line X in a file.
  • gg — Go to the first line in a file.
  • :num — Display the current line’s line number.
  • h — Move left one character.
  • j — Move down one line.
  • k — Move up one line.
  • l — Move right one character.

[ Get more out of your text editor: 5 Vim features for power users. ]

Topics:   Command line utilities   Text editors   Linux  

When using the vi text editor, which of the following keys

Ken has used Red Hat Linux since 1996 and has written ebooks, whitepapers, actual books, thousands of exam review questions, and hundreds of articles on open source and other topics. Ken also has 20+ years of experience as an enterprise sysadmin with Unix, Linux, Windows, and Virtualization. More about me

Get the latest on Ansible, Red Hat Enterprise Linux, OpenShift, and more from our virtual event on demand.

OUR BEST CONTENT, DELIVERED TO YOUR INBOX

Example: > vi letter will open a new file called letter to edit, or if letter already exits, open the exiting file.

Command Effect
vi filename edit filename starting at line 1
vi +n filename edit filename beginning at line n
vi +filename edit filename beginning at the last line
vi -r filename recover filename after a system crash
vi +/patter filename edit filename starting at the first line containing pattern

Command Mode vs. Insert Mode

Insert mode is the mode to be in when inserting text into the file. Command mode is the mode to be in when giving commands which will move the cursor, delete text, copy and paste, save the file etc.

When entering a file, vi is in command mode. To enter text, you must enter insert mode. If in insert mode, enter command mode by hitting the escape, <esc>, key.

To insert text:

Command Insert Text
i before cursor
a after cursor
A at the end of the line
o open a line below the current line
O open a line above the current line
r replace the current character
R replace characters until <ESC>, overwrite

To move the cursor:

You must be in Command Mode to use commands that move the cursor. Each of these commands can be preceded with a Repeat Factor.

Examples:
8j will move the cursor down 8 lines
3w will move the cursor 3 words to the right.

Command Moves the cursor
SPACE, l (el), or right arrow space to the right
h or left arrow space to the left
j or down arrow down one line
k or up arrow up one line
w word to the right
b word to the left
$ end of the line
0 (zero) beginning of the line
e end of the word to the right
- beginning of previous line
) end of the sentence
( beginning of the sentence
} end of paragraph
{ beginning of paragraph

To Delete Text:

The d command removes text from the Work Buffer. The amount removed depends on the Repeat Factor and the Unit of Measure you enter after d. If you delete by mistake: give the command u (undo) immediately after you give the delete command.

Examples: 3dd will delete 3 lines beginning with the current line.
3dw or d3w will delete 3 words

Command Action
d0 delete to beginning of line
dw delete to end of word
d3w delete to end of third word
db delete to beginning of word
dW delete to end of blank delimited word
dB delete to beginning of blank delimited word
dd delete current line
5dd delete 5 lines starting with the current line
dL delete through the last line on the screen
dH delete through the first line on the screen
d) delete through the end of the sentence
d( delete through the beginning of the sentence
x delete the current character
nx delete the number of characters specified by n.
nX delete n characters before the current character

Viewing Different Parts of the Work Buffer:

^Character means that you should hold down the Control key while striking the indicated character key.

Command Moves the cursor
^D forward one-half screenful
^U backward one-half screenful
^F forward one screenful
^B backward one screenful
nG to line n (Ex: 25G moves the cursor to line #25)
H to the top of the screen
M to the middle of the screen
L to the bottom of the screen
^L refresh the screen

Yanking (copy) and Putting (paste) Text:

Example: 3yy will yank (copy) 3 lines
p will put the 3 lines just yanked on the line below the current cursor.

In the following list M is a Unit of Measure that you can precede with a Repeat Factor, n.

Command Effect
yM yank text specified by M
y3w yank 3 words
nyy yank n lines
Y yank to the end of the line
P put text above current line
p put text below current line

Changing Text

Example: cw allows you to change a word. The word may be replaced by as many word as needed. Stop the change by hitting < esc &gt.
c3w allows you to change 3 words.

Ending an Editing Session

Command Effect
:w writes the contents of the work buffer to the file
:q quit
:q! quit without saving changes
ZZ save and quit
:wq save and quit
:w filename saves to filename (allows you to change the name of the file)

Miscellaneous commands

Command Effect
J join the current line and the following line
:set number number the lines on the screen (not actually added to file)
:set nonumber turns off numbering of lines
:r filename reads filename into the current file at the location of the cursor
:set showmode displays INPUT MODE at the lower right hand corner of screen
~ change uppercase to lowercase and vice-versa

Searching for an expression

When in command mode if your enter / the cursor will go to the bottom of the screen. Follow this with an expression and vi will find the next occurrence of that expression in the file. Example: /Bill will find the next occurrence of Bill. If this is followed by the command: n, the next occurrence of Bill is found.