Vi Commands Mac
You can open up Terminal on your Mac (perhaps by opening Spotlight on your Mac with Command⌘-Space key combination and type “Terminal”. Traverse to a folder you want to work in (documents will do. These vi commands help with searching for, and replacing text inside your plain text files. /pattern?pattern (backward) Find next search result:%s/old/new/g Replace all old with new (with prompt):%s/old/new/gc Replace all old with new (no prompts) Multi-file Editing These commands help navigating multiple files in vi. To type commands, you have to first activate Vim’s command mode. To do so, press the Esc key of your keyboard. You can then type any command of your choice. How do I Start Typing Text in Vim? Vim (Vi IMproved) is an open-source text editor for Unix or Linux systems. It is a powerful and stable tool for editing and writing text, both in the command-line interface and as an independent application in a GUI. Take the first step in learning Vim with the basic saving commands. Denotes the Esc key, and denotes the Enter key. The expression means that you should enter a command, means that you should enter a filename, and means that you should enter a character or number. The symbol ^ (caret) means that you should hold down the Ctrl key while pressing the indicated letter. Vi editor command keys.
How to Exit
:q[uit] | Quit Vim. This fails when changes have been made. |
:q[uit]! | Quit without writing. |
:cq[uit] | Quit always, without writing. |
:wq | Write the current file and exit. |
:wq! | Write the current file and exit always. |
:wq {file} | Write to {file}. Exit if not editing the last |
:wq! {file} | Write to {file} and exit always. |
:[range]wq[!] | [file] Same as above, but only write the lines in [range]. |
ZZ | Write current file, if modified, and exit. |
ZQ | Quit current file and exit (same as ':q!'). |
Editing a File
:e[dit] | Edit the current file. This is useful to re-edit the current file, when it has been changed outside of Vim. |
:e[dit]! | Edit the current file always. Discard any changes to the current buffer. This is useful if you want to start all over again. |
:e[dit] {file} | Edit {file}. |
:e[dit]! {file} | Edit {file} always. Discard any changes to the current buffer. |
gf | Edit the file whose name is under or after the cursor. Mnemonic: 'goto file'. |
Inserting Text
a | Append text after the cursor [count] times. |
A | Append text at the end of the line [count] times. |
i | Insert text before the cursor [count] times. |
I | Insert text before the first non-blank in the line [count] times. |
gI | Insert text in column 1 [count] times. |
o | Begin a new line below the cursor and insert text, repeat [count] times. |
O | Begin a new line above the cursor and insert text, repeat [count] times. |
Inserting a file
:r[ead] [name] | Insert the file [name] below the cursor. |
:r[ead] !{cmd} | Execute {cmd} and insert its standard output below the cursor. |
Deleting Text
<Del> or x | Delete [count] characters under and after the cursor |
X | Delete [count] characters before the cursor |
d{motion} | Delete text that {motion} moves over |
dd | Delete [count] lines |
D | Delete the characters under the cursor until the end of the line |
{Visual}x or {Visual}d | Delete the highlighted text (for {Visual} see Selecting Text). |
{Visual}CTRL-H or {Visual} | When in Select mode: Delete the highlighted text |
{Visual}X or {Visual}D | Delete the highlighted lines |
:[range]d[elete] | Delete [range] lines (default: current line) |
:[range]d[elete] {count} | Delete {count} lines, starting with [range] |
Changing (or Replacing) Text
r{char} | replace the character under the cursor with {char}. |
R | Enter Insert mode, replacing characters rather than inserting |
~ | Switch case of the character under the cursor and move the cursor to the right. If a [count] is given, do that many characters. |
~{motion} | switch case of {motion} text. |
{Visual}~ | Switch case of highlighted text |
Substituting
:[range]s[ubstitute]/{pattern}/{string}/[c][e][g][p][r][i][I] [count] | For each line in [range] replace a match of {pattern} with {string}. |
:[range]s[ubstitute] [c][e][g][r][i][I] [count] :[range]&[c][e][g][r][i][I] [count] | Repeat last :substitute with same search pattern and substitute string, but without the same flags. You may add extra flags |
Copying and Moving Text
'{a-zA-Z0-9.%#:-'} | Use register {a-zA-Z0-9.%#:-'} for next delete, yank or put (use uppercase character to append with delete and yank) ({.%#:} only work with put). |
:reg[isters] | Display the contents of all numbered and named registers. |
:reg[isters] {arg} | Display the contents of the numbered and named registers that are mentioned in {arg}. |
:di[splay] [arg] | Same as :registers. |
['x]y{motion} | Yank {motion} text [into register x]. |
['x]yy | Yank [count] lines [into register x] |
['x]Y | yank [count] lines [into register x] (synonym for yy). |
{Visual}['x]y | Yank the highlighted text [into register x] (for {Visual} see Selecting Text). |
{Visual}['x]Y | Yank the highlighted lines [into register x] |
:[range]y[ank] [x] | Yank [range] lines [into register x]. |
:[range]y[ank] [x] {count} | Yank {count} lines, starting with last line number in [range] (default: current line), [into register x]. |
['x]p | Put the text [from register x] after the cursor [count] times. |
['x]P | Put the text [from register x] before the cursor [count] times. |
['x]gp | Just like 'p', but leave the cursor just after the new text. |
['x]gP | Just like 'P', but leave the cursor just after the new text. |
:[line]pu[t] [x] | Put the text [from register x] after [line] (default current line). |
:[line]pu[t]! [x] | Put the text [from register x] before [line] (default current line). |
Undo/Redo/Repeat
u | Undo [count] changes. |
:u[ndo] | Undo one change. |
CTRL-R | Redo [count] changes which were undone. |
:red[o] | Redo one change which was undone. |
U | Undo all latest changes on one line. {Vi: while not moved off of it} |
. | Repeat last change, with count replaced with [count]. |

Moving Around
h or | [count] characters to the left (exclusive). |
l or | [count] characters to the right (exclusive). |
k or CTRL-P | [count] lines upward |
j or CTRL-J or CTRL-N | [count] lines downward (linewise). |
0 | To the first character of the line (exclusive). |
<Home> | To the first character of the line (exclusive). |
^ | To the first non-blank character of the line |
$ or <End> | To the end of the line and [count - 1] lines downward |
g0 or g<Home> | When lines wrap ('wrap on): To the first character of the screen line (exclusive). Differs from '0' when a line is wider than the screen. When lines don't wrap ('wrap' off): To the leftmost character of the current line that is on the screen. Differs from '0' when the first character of the line is not on the screen. |
g^ | When lines wrap ('wrap' on): To the first non-blank character of the screen line (exclusive). Differs from '^' when a line is wider than the screen. When lines don't wrap ('wrap' off): To the leftmost non-blank character of the current line that is on the screen. Differs from '^' when the first non-blank character of the line is not on the screen. |
g$ or g<End&gr; | When lines wrap ('wrap' on): To the last character of the screen line and [count - 1] screen lines downward (inclusive). Differs from '$' when a line is wider than the screen. When lines don't wrap ('wrap' off): To the rightmost character of the current line that is visible on the screen. Differs from '$' when the last character of the line is not on the screen or when a count is used. |
f{char} | To [count]'th occurrence of {char} to the right. The cursor is placed on {char} (inclusive). |
F{char} | To the [count]'th occurrence of {char} to the left. The cursor is placed on {char} (inclusive). |
t{char} | Till before [count]'th occurrence of {char} to the right. The cursor is placed on the character left of {char} (inclusive). |
T{char} | Till after [count]'th occurrence of {char} to the left. The cursor is placed on the character right of {char} (inclusive). |
; | Repeat latest f, t, F or T [count] times. |
, | Repeat latest f, t, F or T in opposite direction [count] times. |
- <minus> | [count] lines upward, on the first non-blank character (linewise). |
+ or CTRL-M or <CR> | [count] lines downward, on the first non-blank character (linewise). |
_ <underscore> | [count] - 1 lines downward, on the first non-blank character (linewise). |
<C-End> or G | Goto line [count], default last line, on the first non-blank character. |
<C-Home> or gg | Goto line [count], default first line, on the first non-blank character. |
<S-Right> or w | [count] words forward |
<C-Right> or W | [count] WORDS forward |
e | Forward to the end of word [count] |
E | Forward to the end of WORD [count] |
<S-Left> or b | [count] words backward |
<C-Left> or B | [count] WORDS backward |
ge | Backward to the end of word [count] |
gE | Backward to the end of WORD [count] |
A word consists of a sequence of letters, digits and underscores, or asequence of other non-blank characters, separated with white space (spaces,tabs,
A WORD consists of a sequence of non-blank characters, separated with whitespace. An empty line is also considered to be a word and a WORD.
( | [count] sentences backward |
) | [count] sentences forward |
{ | [count] paragraphs backward |
} | [count] paragraphs forward |
]] | [count] sections forward or to the next '{' in the first column. When used after an operator, then the '}' in the first column. |
][ | [count] sections forward or to the next '}' in the first column |
[[ | [count] sections backward or to the previous '{' in the first column |
[] | [count] sections backward or to the previous '}' in the first column |
Screen movement commands
z. | Center the screen on the cursor |
zt | Scroll the screen so the cursor is at the top |
zb | Scroll the screen so the cursor is at the bottom |
Marks
m{a-zA-Z} | Set mark {a-zA-Z} at cursor position (does not move the cursor, this is not a motion command). |
m' or m` | Set the previous context mark. This can be jumped to with the '' or '``' command (does not move the cursor, this is not a motion command). |
:[range]ma[rk] {a-zA-Z} | Set mark {a-zA-Z} at last line number in [range], column 0. Default is cursor line. |
:[range]k{a-zA-Z} | Same as :mark, but the space before the mark name can be omitted. |
'{a-z} | To the first non-blank character on the line with mark {a-z} (linewise). |
'{A-Z0-9} | To the first non-blank character on the line with mark {A-Z0-9} in the correct file |
`{a-z} | To the mark {a-z} |
`{A-Z0-9} | To the mark {A-Z0-9} in the correct file |
:marks | List all the current marks (not a motion command). |
:marks {arg} | List the marks that are mentioned in {arg} (not a motion command). For example: |
Searching
/{pattern}[/] | Search forward for the [count]'th occurrence of {pattern} |
/{pattern}/{offset} | Search forward for the [count]'th occurrence of {pattern} and go {offset} lines up or down. |
/<CR> | Search forward for the [count]'th latest used pattern |
//{offset}<CR> | Search forward for the [count]'th latest used pattern with new. If {offset} is empty no offset is used. |
?{pattern}[?]<CR> | Search backward for the [count]'th previous occurrence of {pattern} |
?{pattern}?{offset}<CR> | Search backward for the [count]'th previous occurrence of {pattern} and go {offset} lines up or down |
?<CR> | Search backward for the [count]'th latest used pattern |
??{offset}<CR> | Search backward for the [count]'th latest used pattern with new {offset}. If {offset} is empty no offset is used. |
n | Repeat the latest '/' or '?' [count] times. |
N | Repeat the latest '/' or '?' [count] times in opposite direction. |
Selecting Text (Visual Mode)
To select text, enter visual mode with one of the commands below, and usemotion commands to highlight the text you are interestedin. Then, use some command on the text.
v | start Visual mode per character. |
V | start Visual mode linewise. |
<Esc> | exit Visual mode without making any changes |
How to Suspend
CTRL-Z | Suspend Vim, like ':stop'. Works in Normal and in Visual mode. In Insert and Command-line mode, the CTRL-Z is inserted as a normal character. |
:sus[pend][!] or :st[op][!] | Suspend Vim. If the '!' is not given and 'autowrite' is set, every buffer with changes and a file name is written out. If the '!' is given or 'autowrite' is not set, changed buffers are not written, don't forget to bring Vim back to the foreground later! |
What is vi
The vi editor is elaborated as visual editor. It is installed in every Unix system. In other words, it is available in all Linux distros. It is user-friendly and works same on different distros and platforms. It is a very powerful application. An improved version of vi editor is vim.
The vi editor has two modes:
- Command Mode: In command mode, actions are taken on the file. The vi editor starts in command mode. Here, the typed words will act as commands in vi editor. To pass a command, you need to be in command mode.
- Insert Mode: In insert mode, entered text will be inserted into the file. The Esc key will take you to the command mode from insert mode.
By default, the vi editor starts in command mode. To enter text, you have to be in insert mode, just type 'i' and you'll be in insert mode. Although, after typing i nothing will appear on the screen but you'll be in insert mode. Now you can type anything.
To exit from insert mode press Esc key, you'll be directed to command mode.
If you are not sure which mode you are in, press Esc key twice and you'll be in command mode.
Using vi
The vi editor tool is an interactive tool as it displays changes made in the file on the screen while you edit the file.
In vi editor you can insert, edit or remove a word as cursor moves throughout the file.
Commands are specified for each function like to delete it's x or dd.
The vi editor is case-sensitive. For example, p allows you to paste after the current line while P allows you to paste before the current line.
vi syntax:
In the terminal when you'll type vi command with a file name, the terminal will get clear and content of the file will be displayed. If there is no such file, then a new file will be created and once completed file will be saved with the mentioned file name.
Linux vi example
Let's understand vi through an example:
To start vi open your terminal and type vi command followed by file name. If your file is in some other directory, you can specify the file path. And if in case, your file doesn't exist, it will create a new file with the specified name at the given location.
Example:
Look at the above snapshot, we are creating a new file file.txt (as this file doesn't exist) and have entered the full path for the directory Downloads.
Command mode
This is what you'll see when you'll press enter after the above command. If you'll start typing, nothing will appear as you are in command mode. By default vi opens in command mode.
Look at the above snapshot, it is blank as it is a new file. To start typing, you have to move to the insert mode. At the end of the terminal window, directory name and file name are displayed.
Insert mode
To move to the insert mode press i. Although, there are other commands also to move to insert mode which we'll study in next page.
Look at the above snapshot, after pressing i we have entered into insert mode. Now we can write anything. To move to the next line press enter.
Once you have done with your typing, press esc key to return to the command mode.
To save and quit
You can save and quit vi editor from command mode. Before writing save or quit command you have to press colon (:). Colon allows you to give instructions to vi.
exit vi table:
Commands | Action |
---|---|
:wq | Save and quit |
:w | Save |
:q | Quit |
:w fname | Save as fname |
ZZ | Save and quit |
:q! | Quit discarding changes made |
:w! | Save (and write to non-writable file) |
To exit from vi, first ensure that you are in command mode. Now, type :wq and press enter. It will save and quit vi.
Type :wq to save and exit the file.
Look at the above snapshot, command :wq will save and quit the vi editor. When you'll type it in command mode, it will automatically come at bottom left corner.
If you want to quit without saving the file, use :q. This command will only work when you have not made any changes in the file.
Look at the above snapshot, this file is modified and hence on typing :q it displays this message at bottom left corner.
The above file can be saved with the command :!q. It discards the changes made in the file and save it.
Look at the above snapshot, we have typed :!q, it will save our file by discarding the changes made.
Vi Commands
Linux vi editor is different from other editors. You have to use different keys to use different functions. Although, it's quite easy and interesting to use vi editor.
The vi editor commands are case sensitive.
Have a look at the vi commands in the following table.
To switch from command to insert mode:
How To Use Vim
Command | Action |
---|---|
i | Start typing before the current character |
I | Start typing at the start of current line |
a | Start typing after the current character |
A | Start typing at the end of current line |
o | Start typing on a new line after the current line |
O | Start typing on a new line before the current line |
To move around a file:
Commands | Action |
---|---|
j | To move down |
k | To move up |
h | To move left |
l | To move right |
To jump lines:
Commands | Action |
---|---|
G | Will direct you at the last line of the file |
`` | Will direct you to your last position in the file |
To delete:
Commands | Action |
---|---|
x | Delete the current character |
X | Delete the character before the cursor |
r | Replace the current character |
xp | Switch two characters |
dd | Delete the current line |
D | Delete the current line from current character to the end of the line |
dG | delete from the current line to the end of the file |
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 |
Start and end of line:
Vim Text Editor Mac
Commands | Action |
---|---|
θ | Bring at the start of the current line |
^ | Bring at the start of the current line |
$ | Bring at the end of the current line |
dθ | Delete till start of a line |
d$ | Delete till end of a line |
Joining lines:
Commands | Action |
---|---|
J | Join two lines |
yyp | Repeat the current line |
ddp | Swap two lines |
Move forward or backward:

Commands | Action |
---|---|
w | Move one word forward |
b | Move one word backward |
<n>w | Move specified number of words forward |
dw | Delete one word |
yw | Copy one word |
<n>dw | Delete specified number of words |
Search a string:
Commands | Action |
---|---|
/string | Forward search for given string |
?string | Backward search for given string |
/^string | Forward search string at beginning of a line |
/string$ | Forward search string at end of a line |
n | Go to next occurrence of searched string |
/<he> | Search for the word he (and not for there, here, etc.) |
/pl[abc]ce | Search for place, plbce, and plcce |
Replace all
Syntax:

Example:
Vi Commands Mac Os
Commands | Action |
---|---|
:1,$ s/readable/changed/ | Replace forward with backward from first line to the last line |
:3,6 s/letters/neww/g | Replace forward with backward from third line to the ninth line |
Text buffers:
Commands | Action |
---|---|
'add | Delete current line and put text in buffer a |
'ap | Paste the line from buffer a |
Abbreviation

Syntax:
Example:
Commands | Action |
---|---|
:ab au abbrevition and unabbreviation | Abbreviate au to be 'abbrevition and unabbreviation' |
:una au | Un - abbreviate au |
Vi Editor Index
Vi Editor
