Vi Mac Commands



Use the vi /path/to/file command to open an existing file with Vi. The vi /path/to/file command also works if the file doesn’t exist yet; Vi will create a new file and write it to the specified location when you save. Remember to use sudo if you want to edit a system file. This is the second part of the three part tutorial on mapping keys in Vim. You can read the other two parts from the following pages: Mapping keys in Vim - Tutorial (Part 1) Mapping keys in Vim - Tutorial (Part 3) 1 Finding unused keys 2 Key notation 3 Maps with the same prefix 4 Map comments 5 Supplying a count to a map 6 Using multiple Ex commands in a map 7 Using space characters in a map 8. Marking text (visual mode) v - starts visual mode From here you can move around as in normal mode (h, j, k, l etc.) and can then do a command (such as y, d, or c) V - starts linewise visual mode. 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. Vim is in Insert mode with the use of the a, A, i, I, o, and O commands. Once in Insert mode, the editor will stay in that mode until you press an Esc key. Every other key pressed is directly inserted into the file at the current cursor location. Visual mode happens when you use a v, V, and Ctrl-v.

Updated on July 13, 2020

Vim was made available in 1991 and is a free, open source software. Available both as a command line interface and as a standalone program with a GUI, Vim is a text editor that is a modal version of the vi editor created for Unix in the 1970s; Vim stands for vi improved. While it was designed with Unix in mind, versions of it are available for most operating systems and Vim is also available for Android and iOS smartphones.

While you may be familiar with the concept of a text editor, the modal part may throw you. A modal editor is one that allows you to edit text in different modes, and in the case of Vim, the mode determines what the alphanumeric keys on your keyboard do and how Vim editor commands work.

For example, in insert mode, your keyboard behaves normally, so what you type in is what you see, just like with a standard text editor. However, if you switch to command mode, the letters on your keyboard will allow you use Vim commands to move within the text. If you play video games that use the left-hand keys on the keyboard to move your character around, this concept is probably familiar to you.

To open a file using Vim you can use the following command (simply replace filename.css with your actual file name).

The idea behind having a modal text editor is that it allows you to write and edit text, including code, without requiring your hands leave the keyboard. Vim isn't for everyone, and it requires you to learn a variety of Vim editor commands to get the most out of it. That said, many people who have started using it and become comfortable with it won't even consider another editor. The image below shows an exampe of what a css file looks like when viewed using Vim.

This text editor is particularly well-suited for people who are programmers, coders, system administrators or individuals looking for a streamlined way to edit text. The editor allows you to edit text in multiple windows, which can be helpful to programmers and editors alike. If you're interested in giving Vim a shot, the following is a basic explanation of Vim modes and a list of frequently used Vim commands, along with a definition of what they do.

Vim modes

There are some arguments as to how many modes that Vim has, but the modes you're most likely to use are command mode and insert mode. These modes will allow you to do just about anything you need, including creating your document, saving your document and doing advanced editing, including taking advantage of search and replace functions.

Command mode

This is the default mode that you'll be in once you open Vim. If you're in a different mode and want to go back to command mode, just hit the Escape key. This mode allows you to use Vim commands and move through your document. From command mode, you can also use last-line commands, which generally start with the use of a colon. For example, :w saves your file and :q allows you to exit Vim.

Insert mode

This mode allows you to enter text into your document. You can enter insert mode by pressing the i key. Keep in mind that to save your document, you'll need to go back to command mode since only text input is allowed in this mode.

List

Installing Vim

There are a few ways to install Vim and the one you end up using will sometimes depend on which system you're using.

Install Vim using Git:

According to Vim themselves, install Vim via Git is the simplest and most efficient method. Simply use the following commands:

If you already have Vim installed but want to update to the latest version, you may need to use additional Git commands which can be found here.

Install Vim on Ubuntu/Debian:

If you're using Ubuntu or Debian use apt-get to install Vim, like so:

Install Vim on CentOS/Fedora:

If you're using CentOS or Fedora, use yum to install Vim:

If you want a more advanced set of features on CentOS/Fedora, you'll need to install vim-enhanced, to do this, run the following command instead:

Vim commands

The following is a list of frequently used commands and what they do. Many of the commands can be made to repeat by adding a number to the command. This is not an exhaustive list because more advanced commands, such as how to use multiple buffers, are not included. However, just about all of the basic commands for opening, editing and saving documents are included as well as commands that enable you to find and replace text and work with multiple documents.

1. Basic Vim commands

The most simple commands allow you to open and close documents as well as saving them. As with most other text editors, there are protections in place to help you avoid exiting the editor without having saved what you're working on.

:help [keyword] - Performs a search of help documentation for whatever keyword you enter

:e [file] - Opens a file, where [file] is the name of the file you want opened

:w - Saves the file you are working on

:w [filename] - Allows you to save your file with the name you've defined

:wq - Save your file and close Vim

:q! - Quit without first saving the file you were working on

2. Vim commands for movement

When using movement commands, you can put a number in front of them to make Vim complete a command multiple times. For example, 5h will move your cursor five spaces to the left, and 90j will put your cursor at the beginning of the 90th line down from where your cursor currently is.

h - Moves the cursor to the left

l - Moves the cursor to the right

j - Moves the cursor down one line

k - Moves the cursor up one line

H - Puts the cursor at the top of the screen

M - Puts the cursor in the middle of the screen

L - Puts the cursor at the bottom of the screen

w - Puts the cursor at the start of the next word

b - Puts the cursor at the start of the previous word

e - Puts the cursor at the end of a word

0 - Places the cursor at the beginning of a line

$ - Places the cursor at the end of a line

) - Takes you to the start of the next sentence

( - Takes you to the start of the previous sentence

Vi Commands Mac

} - Takes you to the start of the next paragraph or block of text

{ - Takes you to the start of the previous paragraph or block of text

Ctrl + f - Takes you one page forward

Ctrl + b - Takes you one page back

gg - Places the cursor at the start of the file

G - Places the cursor at the end of the file

# - Where # is the number of a line, this command takes you to the line specified

3. Vim commands for editing

Those who use Vim tend to use the term 'yank' where most people would use the terms copy and paste. Therefore, the command for copying a word is yw, which stands for yank word, and the command for pasting whatever has been copied is p, meaning put. If you look up additional commands in the future, it can be confusing if you don't know what yank and put mean when using Vim.

You also have two options for how to select text. You can either use commands like dd, which deletes a single line, and yy, which copies a single line, or you can highlight text and then copy it to the unnamed register. The paste commands work the same whether you've highlighted text or used a command to automatically copy it.

As with movement commands, putting a number in front of the command can increase the number of times a task is completed. For instance, putting a number in front of yy will increase the number of lines copied, so 5yy will copy five lines.

yy - Copies a line

yw - Copies a word

y$ - Copies from where your cursor is to the end of a line

v - Highlight one character at a time using arrow buttons or the h, k, j, l buttons

V - Highlights one line, and movement keys can allow you to highlight additional lines

p - Paste whatever has been copied to the unnamed register

d - Deletes highlighted text

dd - Deletes a line of text

Vim Cheat Sheet

dw - Deletes a word

D - Deletes everything from where your cursor is to the end of the line

d0 - Deletes everything from where your cursor is to the beginning of the line

dgg - Deletes everything from where your cursor is to the beginning of the file

dG - Deletes everything from where your cursor is to the end of the file

x - Deletes a single character

u - Undo the last operation; u# allows you to undo multiple actions

Ctrl + r - Redo the last undo

. - Repeats the last action

4. Vim commands for searching text

Like many other text editors, Vim allows you to search your text and find and replace text within your document. If you opt to replace multiple instances of the same keyword or phrase, you can set Vim up to require or not require you to confirm each replacement depending on how you put in the command.

/[keyword] - Searches for text in the document where keyword is whatever keyword, phrase or string of characters you're looking for

?[keyword] - Searches previous text for your keyword, phrase or character string

n - Searches your text again in whatever direction your last search was

N - Searches your text again in the opposite direction

:%s/[pattern]/[replacement]/g - This replaces all occurrences of a pattern without confirming each one

:%s/[pattern]/[replacement]/gc - Replaces all occurrences of a pattern and confirms each one

5. Vim commands for working with multiple files

You can also edit more than one text file at a time. Vim gives you the ability to either split your screen to show more than one file at a time or you can switch back and forth between documents. As with other functions, commands make going between documents or buffers, as they're referred to with Vim, as simple as a few keystrokes.

:bn - Switch to next buffer

:bp - Switch to previous buffer

:bd - Close a buffer

:sp [filename] - Opens a new file and splits your screen horizontally to show more than one buffer

:vsp [filename] - Opens a new file and splits your screen vertically to show more than one buffer

:ls - Lists all open buffers

Ctrl + ws - Split windows horizontally

Ctrl + wv - Split windows vertically

Ctrl + ww - Switch between windows

Ctrl + wq - Quit a window

Ctrl + wh - Moves your cursor to the window to the left

Ctrl + wl - Moves your cursor to the window to the right

Ctrl + wj - Moves your cursor to the window below the one you're in

Ctrl + wk - Moves your cursor to the window above the one you're in

6. Marking text (visual mode)

Visual mode allows you to select a block of text in Vim. Once a block of text is selected you can use visual commands to perform actions on the selected text such as deleting it, copying it, etc.

v - starts visual mode, you can then select a range of text, and run a command

V - starts linewise visual mode (selects entire lines)

Ctrl + v - starts visual block mode (selects columns)

ab - a block with ()

aB - a block with {}

ib - inner block with ()

iB - inner block with {}

aw - mark a word

Esc - exit visual mode

Once you've selected a particular range of text, you can then run a command on that text such as the following:

d - delete marked text

y - yank (copy) marked text

> - shift text right

< - shift text left

~ - swap case (upper or lower)

7. Tab pages

Just like any browser, you can also use tabs within Vim. This makes it incredibly easy to switch between multiple files while you're making some code changes instead of working in one single file, closing it, and opening a new one. Below are some useful Vim commands for using tab pages:

:tabedit file - opens a new tab and will take you to edit 'file'

gt - move to the next tab

gT - move to the previous tab

#gt - move to a specific tab number (e.g. 2gt takes you to the second tab)

:tabs - list all open tabs

:tabclose - close a single tab

Simple Vim workflow example

If you haven't had a chance to play around with Vim much yet, you might be wondering what a simple workflow looks like when using it. It's relatively simple:

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

Of course, there is so much more you can do with Vim, however as a beginner, the above steps are what a simple Vim workflow looks like.

Summary

Vim is quite easy to use, it just involves memorizing Vim editor commands and remembering what mode you're in. If you're used to using keyboard shortcuts like Ctrl + C and Ctrl + S, you shouldn't have too much difficulty getting used to the way that Vim works. While there is a bit of a breaking in period with the editor, you don't have to worry too much about accidentally deleting large swathes of text without being able to recover them since you can use the undo command multiple times.

While not for everyone, functionality like being able to work on more than one document at a time in windowed screens and the ability to do major editing without a mouse is what makes Vim so popular. You can download the editor for free, and there are a variety of plugins and extensions that can improve its functionality and add additional Vim commands.

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.
:wqWrite 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].
ZZWrite current file, if modified, and exit.
ZQQuit 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.
gfEdit the file whose name is under or after the cursor. Mnemonic: 'goto file'.

Inserting Text

aAppend text after the cursor [count] times.
AAppend text at the end of the line [count] times.
iInsert text before the cursor [count] times.
IInsert text before the first non-blank in the line [count] times.
gIInsert text in column 1 [count] times.
oBegin a new line below the cursor and insert text, repeat [count] times.
OBegin 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
XDelete [count] characters before the cursor
d{motion}Delete text that {motion} moves over
ddDelete [count] lines
DDelete 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}.
REnter 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

Vim On Mac

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]yyYank [count] lines [into register x]
['x]Yyank [count] lines [into register x] (synonym for yy).
{Visual}['x]yYank the highlighted text [into register x] (for {Visual} see Selecting Text).
{Visual}['x]YYank 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]pPut the text [from register x] after the cursor [count] times.
['x]PPut the text [from register x] before the cursor [count] times.
['x]gpJust like 'p', but leave the cursor just after the new text.
['x]gPJust 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

uUndo [count] changes.
:u[ndo]Undo one change.
CTRL-RRedo [count] changes which were undone.
:red[o]Redo one change which was undone.
UUndo 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
or
[count] characters to the right (exclusive).
k or
or
CTRL-P
[count] lines upward
j or
or
CTRL-J or
or
CTRL-N
[count] lines downward (linewise).
0To 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
eForward to the end of word [count]
EForward to the end of WORD [count]
<S-Left> or
b
[count] words backward
<C-Left> or
B
[count] WORDS backward
geBackward to the end of word [count]
gEBackward to the end of WORD [count]

Mac Vi Commands Cheat Sheet

These commands move over words or WORDS.

A word consists of a sequence of letters, digits and underscores, or asequence of other non-blank characters, separated with white space (spaces,tabs, ). This can be changed with the 'iskeyword' option.

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
ztScroll the screen so the cursor is at the top
zbScroll the screen so the cursor is at the bottom

Marks

Vim Common Commands

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
:marksList all the current marks (not a motion command).
:marks {arg}List the marks that are mentioned in {arg} (not a motion command). For example:
Vim

Mac Vi Editor Commands

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.
nRepeat the latest '/' or '?' [count] times.
NRepeat the latest '/' or '?' [count] times in opposite direction.
Vim commands pdf

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.

vstart Visual mode per character.
Vstart Visual mode linewise.
<Esc>exit Visual mode without making any changes

How to Suspend

CTRL-ZSuspend 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!
Daniel Gryniewicz / dang@fprintf.net



Comments are closed.