# 05-A.2.3: Vim - Command-Line Mode

## Command-Line Mode

You may be asking, "Why does vim have so many modes, why is that necessary?" Well, you must remember that the original vi, the forerunner to vim, was created a LONG time ago, when keyboards did not have arrow keys, there were no graphical user interfaces, and everything was done on a character-based terminal.

Vim uses one mode to insert text into the file; one mode to navigate around the file - although vim allows use of the arrow keys to move while in insert mode; one mode that allows us to select chunks of code to modify; and now we have the Command-Line mode where we can do a majority of the editing.

In Command-Line we use certain keys or key sequences to edit the file and to move around within the file. Depending on where you look for information about vim, it may tell you that the Command-Line mode is only for the commands that start with ':' - but we are including other editing and search functionality as well.

Remember - you need to make sure you are in Normal mode (which is actually the same as Command-Line mode) - if you are not sure what mode you are in you can simply hit the ESC key which is harmless if you are in Normal mode already.

## ex Commands

There is an editor called ex, which is even older than vi, and it has some interesting syntax to its command. These commands are all entered by using a '':' and then the actual command. There are lots of vim resources that discuss the use of these very powerful commands, but the syntax is very different, especially if you are not used to operating from a command line.

Some examples of ex commands (some of these we have already seen):

Mode Name Description
:wq Write file to disk and quit the editor
:q! Quit - does NOT save file before quit
:q Quit (a warning is printed if a modified file has not been saved)
:w Writes the file back to the original file
:w myFile Writes the current file to the file named myFile; it is created if it doesn't exist (if it does exist you will need to issue :w! myFile)
:/[some pattern] Search forward in the file for the pattern - places cursor at the beginning of the line
:?[some pattern] Search backward in the file for the pattern - places cursor at the beginning of the line
:10,25 w newFile Writes lines 10 through 25 to a file named newFile (if that file exists you will need to issue 10,25 w! newFile)

The example below is only showing the bottom of the terminal window, which is where the ex commands show up as soon as you enter the colon character. This image shows writing the current file to a file named myTest.

## Cursor Movement

To move the cursor you can use the arrow keys. There is also a set of commands to move the cursor to specific places within the file. These commands allow you to move quickly throughout the file. These keystrokes DO NOT show up anywhere on the terminal window.

Key Description
nG Cursor goes to the specified n (where n is some integer value) line
^F (CTRl F) Forward screenful
^B (CTRL B) Backward screenful
^f One page forward
^b One page backward
^U Up half screenful
^D Down half screenful
$Move cursor to the end of current line 0 (zero) Move cursor to the beginning of current line w Forward one word b Backward one word ## Edit Commands There are keys/key sequences that edit the file. These keystrokes DO NOT show up anywhere on the terminal window. Key x Delete character dw Delete word from cursor on db Delete word backward dd Delete line d$ Delete to end of line
d^ (d caret, not CTRL d) Delete to beginning of line
yy Yank current line
y\$ Yank to end of current line from cursor
yw Yank from cursor to end of current word
5yy Yank, for example, 5 lines
p Paste below/after cursor
P Paste above/before cursor

The two paste commands have two slightly different uses. If you yank an entire line from the file, then p (lower case) will paste that line below the current line and P (upper case) will paste it above the current line. If, however, you have yanked a word or even a part of the line, then p (lower case) will paste the text after the cursor position on the same line and P (upper case) will paste it before the cursor position on the current line.