When using the vi text editor which of the following keys when in command mode will change to insert mode and place the cursor at the end of the current line?

Gray at the temples and in need of reading glasses, the 43-year-old vi (pronounced vee-eye) editor 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 to edit source code. 

[ Download now: Vim cheat sheet ]

Note: vi is often a symbolic link to vim (vi Improved) or an alias to vim.

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

$ vi filename.txt

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 that 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.

[ Free eBook: Manage your Linux environment for success. ]

Note: 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. To the non-vi initiated, write means save, and quit means exit vi. If you’ve made mistakes along the way in your editing and want to back out (abandon) all non-saved changes, enter Command mode by pressing Esc and type :q! This command quits without saving any changes and exits vi.

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 table 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.

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.

CommandPurpose
$ 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.

[ Download now: Vim cheat sheet ]

Written by Chris Gregg and Dominique Yahyavi, with modifications by Peter Johnston, Nick Troccoli, and Lisa Yan

Click here for a walkthrough video.

vim is a text editor that is a clone of the vi editor, which was created around 1976. Despite its age, vim is one of the most popular editors in the world, and it is available on virtually every computing platform (or vi is available). vim has a bit of a learning curve, meaning that you may get somewhat frustrated with it until you have used it a few times.

Overview

vim has two "modes": COMMAND mode and INSERT mode. In COMMAND mode, you execute commands (like undo, redo, find and replace, quit, etc.). In INSERT mode, you type text. There is a third mode, VISUAL mode, that is used to highlight and edit text in bulk.

  • To go into INSERT mode from COMMAND mode, you type i. To go back to COMMAND mode, you type the esc key. vim starts out in COMMAND mode. Over time, you will likely spend more time in COMMAND mode than INSERT mode.
  • Note that all commands mentioned below (except for arrow keys) only work in COMMAND mode.

Opening vim

To open a file in vim (or create a new one if a file with this name does not exist):

$ vim filename

If vim is already open and you would like to edit a different file, use the :e filename command (e.g. :e myOtherFile.c). Again, if this file exists in your current directory it will open it, or it will create it if it does not already exist in your current directory.

Saving and Quitting vim

:w myprogram.c save the current changes to a file. If you don't specify a name, changes will be saved to the current file. If you would like to save the file under a different name, specify a filename.

:q quits Vim. If you have unsaved changes, you will be asked whether or not you'd like to save your changes before quitting.

:q! quit without saving unsaved changes.

:wq save and quit.

Arrow keys work in both modes to navigate as you would expect. The preferred mode in vim, however, is to use the j``k``h``l keys instead in COMMAND mode (these do not work in INSERT mode), which allows users to keep their fingers on the home row to move around the screen. Here's how they work:

j: down k: up h: left l: right

There are also several additional keyboard shortcuts to navigate within a file.

w move to the next word. B move to the previous word ("Back")

b move to the beginning of the word ("beginning"). e move to the end of the word ("end")

0 move to the beginning of the line. $ move to the end of the line

:123 jump to line number 123. 123G jump to line number 123 (equivalent to :123)

Ctl-f page down ("forward"). Ctl-b page up ("back")

Ctl-u half a page up. Ctl-d half a page down.

gg jump to the first line in file. G jump to last line in file

Searching

Search is another great way to move your cursor.

/foo search the file for "foo". This will highlight all matches in the editor for you to see.

n move the cursor to the next search match

N move the cursor to the previous search match

:noh to turn off highlighting until the next search. Alternatively, search for a bogus string, like /thiswordwontexist

Editing Text

dd or :d deletes the current line

yy or :y or Y "Yank" (cut) the current line

p Paste the text you yanked or deleted. This will put characters after the cursor and put lines below the current line depending on what you yanked. To paste above the current line, use P.

x delete the character underneath the cursor

Nx delete the N characters under and following the cursor (e.g. 3x deletes the 3 characters under and following the cursor).

Entering INSERT Mode

When you are ready to enter text, you should switch to INSERT mode. There are a few ways to do this:

i enter INSERT mode before the cursor

I enter INSERT mode at the start of the current line

a enter INSERT mode appending after the cursor

A enter INSERT mode appending to the end of the current line

Then, just type as normal to enter text. You can use the arrow keys to navigate, or switch back to COMMAND mode and use the shortcuts mentioned earlier.

Undo/Redo

u undo the last action

U undo all recent changes made to the current line

ctrl-r redo

Highlighting text

If you want to highlight multiple lines or particular words in a line, you can do so in VISUAL mode. There are three different visual modes:

v enters character-wise visual mode to highlight one character at a time.

V enters line-wise visual mode to highlight entire lines at a time.

ctrl-v enters block-wise visual mode to highlight over rows and columns, etc.

Once you enter these modes by typing these in COMMAND mode, you can use the keyboard navigation (e.g., hjkl or w or b) to highlight text). Then you can batch delete (d), yank (y), or paste (p) whatever is highlighted. To exit VISUAL mode, use esc.

VISUAL mode is also useful for adjusting indent to a batch of text. For example, if you wanted to reduce the indent of a set of lines, you can enter block-wise visual mode (ctrl-v) to select a vertical column of indents (using keyboard navigation), and then batch delete with d. To increase the indent of a set of lines, enter block-wise visual mode and select a vertical column, and then batch insert at the beginning of this block with I, type in your indentation, and then use esc. You should see all highlighted lines get the same inserted space.

More Resources

The best way to get familiar with vim is to just start using it to edit files - type something, anything! Over time, you'll become more comfortable with the standard commands, and pick up more advanced ones. If you are looking for more references for how to use vim, check out the builtin vimtutor tutorial program on myth to help you learn vim. Run it by doing the following:

$ vimtutor

There is also a fun online vim tutorial, called Vim Adventures that can give you practice with the basic commands as well.

Frequently Asked Questions

When I do vim filename, I get a big scary message that says something about "Found a swap file." What does this mean, and what do I do?

This most often happens when Vim quits unexpectedly, e.g. if you lose your connection to myth while editing a file. (It can also happen if you try to edit the same file in two terminal windows; you shouldn't do this.)

Here's the recommended process for resolving it: First, hit o for "open read-only". This shows you the version of the file that was last saved. Look through it. Does it have your most recent changes?

  • Yes, this is the latest version of the file: Great! To clean up the swap file, :q out, then run vim on the file again. This time, hit d to delete the swap file. (Warning: you can't undo this.)

  • No, some of my changes are missing: OK, don't panic. Quit out of vim, and start it again. This time, hit r for "recover." This will show you the version of the file from the interrupted session. If that's (closer to) the version you want to keep, save it, then follow the steps above to clean up the swap file.

  • How do I avoid the above issue coming up next time? Make sure you exit Vim cleanly. Don't just hit the "X" button on your terminal. Also, don't close your laptop lid while Vim is open (more generally, putting your laptop to sleep while connected to myth could lead to other problems, because it generally turns off your internet connection).

Which command will be used in vi editor to insert text at cursor position?

Type I to insert text at the beginning of a line. The command moves the cursor from any position on that line. Press Esc to return to command mode after you type the desired text.

Which of the following keys can be used to move from command mode to insert mode?

If you want to type in text, you need to be in Insert mode. To move from Command mode to Insert mode, press "i" (no quotes). To move from Insert mode to Command mode, press "ESC" (the Escape key). NOTE: If your terminal doesn't have an ESC key, or the ESC key doesn't work, use Ctrl-[ instead.

What key do you press to get into command mode in vi?

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. To the non-vi initiated, write means save, and quit means exit vi.

What is the command to determine which mode you are in when using vi editor?

vi Editor Insert mode: You can switch to the Insert mode from the command mode by pressing 'i' on the keyboard. Once you are in Insert mode, any key would be taken as an input for the file on which you are currently working. To return to the command mode and save the changes you have made you need to press the Esc key.