brew install vim
.
Alternatively, install MacVim by browsing
https://macvim-dev.github.io/macvim/.
vim
.
This can be followed by a file path to open a
specific file, but as we'll see it is very easy
to locate and open files from within Vim.
Key | Action |
---|---|
cw |
changes word starting at cursor; removes word and enters insert mode so replacement can be entered |
c$ |
changes everything from cursor to end of line; removes rest of line and enters insert mode so replacement can be entered |
dd |
deletes current line, placing it in the clipboard |
dw |
deletes word starting at cursor, placing it in the clipboard |
d$ |
deletes from cursor to end of current line, placing it in the clipboard |
dt followedby a character |
deletes from cursor "to" first occurrence of specified character, placing it in the clipboard |
J |
joins next line to current line |
p |
pastes clipboard contents after character under cursor |
P |
pastes clipboard contents before character under cursor |
r |
replaces character under cursor with next character typed |
u |
undoes last change; press repeatedly to undo multiple changes |
ctrl-r |
reverses last undo; press repeatedly to redo multiple undo's |
y |
yanks (copies) character under cursor into the clipboard |
yw |
yanks (copies) word starting at cursor into the clipboard |
yy |
yanks (copies) current line into the clipboard |
y$ |
yanks (copies) characters from cursor to end of current line into the clipboard |
x |
cuts character under cursor, placing it in the clipboard |
z enter |
moves current line to top; especially useful when cursor is on the first line of a long function definition and you want to see as much code as possible on the screen at once |
~ |
toggles case of character under cursor |
. |
repeats last command; It cannot be overemphasized how useful this is! |
/text or/regex |
finds the next occurrence of the specified text; press n to repeat the search forwardor N to repeat the search backward |
%s/old/new/g or%s/regex/new/g |
replaces all occurrences of matching text in the entire file
with the specified text; add c to end of command to confirm each replacement
|
:set number or :set nu |
shows line numbers |
:set nonumber or :set nonu |
hides line numbers |
3dd
deletes three lines
starting with the current one.
Key | Enters Insert Mode ... |
---|---|
i |
before character under cursor |
a |
after character under cursor |
I |
before first non-whitespace character on current line |
A |
after last character on current line |
o |
opening new line below current line |
O |
opening new line above current line |
esc or ctrl-[ |
exits insert mode and returns to command mode |
Key | Enters Insert Mode ... |
---|---|
v |
enters character-based visual mode |
V |
enters line-based visual mode |
x |
cuts selected text and exits visual mode |
y |
yanks (copies) selected text into the clipboard and exits visual mode |
~ |
toggles case of all selected characters and exits visual mode |
esc or ctrl-[ |
exits visual mode and returns to command mode |
Key | Moves ... |
---|---|
j |
down |
k |
up |
h |
left |
l |
right |
0 |
beginning of line |
$ |
end of line |
^ |
first non-whitespace character on line |
w |
next word |
b |
previous word |
gg |
top of file |
G |
bottom of file |
line number followed by G
|
to line number |
ctrl-f |
forward one page |
ctrl-b |
backward one page |
% |
to matching delimiter when cursor is on one of the following delimiters { } [ ] ( ) /* */
|
Command | Action |
---|---|
:w |
writes current buffer to associated file |
:w file-path |
writes current buffer to specified file |
:q |
quits Vim, but not if there are unsaved changes |
:q! |
quits Vim even if there are unsaved changes |
:wq |
writes and quits |
:E
.
To open one on the left side of a new vertical split,
enter :Ve
.
To open one on the bottom side of a new horizontal split,
enter :He
.
To open one when starting Vim, start it with
vim +E
or vim .
.
Command | Action |
---|---|
enter | opens file for editing or directory for further navigating |
R |
renames file or directory; will prompt for new name |
D |
deletes file or directory (if empty) |
Command | Action |
---|---|
:ls |
lists all current buffers |
b followed by abuffer number |
opens specified buffer |
bd
followed by aspace-separated list of buffer numbers |
deletes specified buffers,
but not their associated files; Deleted buffers will no longer appear in :ls output. |
phones.txt
buffer, enter b4
.
Command | Action |
---|---|
ctrl-ws or:split orjust :sp |
splits current window horizontally |
ctrl-wv or:vsplit orjust :vs |
splits current window vertically |
ctrl-wc or ctrl-wq or :close orjust :clo |
closes current split |
ctrl-wo or:only orjust :on |
closes all splits except current one |
ctrl-ww | moves cursor to next split; press repeatedly to cycle between them |
Command | Action |
---|---|
q followed bya register letter |
begins recording a macro in the specified register |
q |
when already recording a macro, ends the recording |
@ followed bya register letter |
executes the macro in the specified register |
@@ |
executes the last macro that was executed; faster than typing @ and a register letter
for repeated execution |
esc
is the escape key):qpi(escllllr)q
qp
begins recording a macro in the p register.i(esc
enters insert mode, inserts a left paren,
and exits insert mode.llll
moves right four characters.
This could also be done with 4l
.r)
replaces the character under the cursor,
a dash in this case, with a right paren.q
ends the macro recording.
@p
to format them.
After the macro has been run once,
it can also be run again by pressing @@
.
/\d\{3}-\d\{3}-\d\{4}enter
.
This finds a phone number using a regular expression
where \d
represents a digit and
{n}
represents a number of consecutive occurrences.
The opening braces must be escaped with backslashes.
Once this is in place, the macro can be run a specified number
of times by entering a number followed by @p
.
For example, to fix the next ten phone numbers
beginning with the one starting at the cursor position
enter 10@p
.
.vimrc
file
or adding the use of plugins.
But those are more advanced topics that you can learn about elsewhere.