Vim
Quick Overview
Movement
Some movement keys
Key | Description |
---|---|
j
|
down |
k
|
up |
l
|
right |
h
|
left |
w
|
go forward 1 word |
W
|
go forward 1 word, ignoring symbols and punctuations |
b
|
go backward 1 word |
B
|
go backward 1 word, ignoring symbols and punctuations |
f $char
|
Go to next instance of '$char' |
;
|
Repeat the previous f command |
0
|
Move to beginning of the line |
^
|
Move to first non-blank character of the line |
$
|
Move to end of the line |
gg
|
Move to top of file |
G
|
Move to end of the file |
-
|
Go to start of previous line |
+
|
Go to start of next line |
%
|
Jump to start/end of highlighted bracket/parenthesis |
Splits
Splits let you split the VIM window and allow you to edit multiple files side by side. These splits aren't quite like tabs in a conventional editor since they share the same list of buffers. You can change a particular pane's 'view' by focusing on it and then changing buffers.
Key | Description |
---|---|
Ctrl + w , v
|
Create a new split horizontally |
Ctrl + w , s
|
Create a new split vertically |
Ctrl + w , q
|
Closes the focused split |
:on
|
Close all other splits |
Ctrl + w , l
|
Move to/focus on the split to the right |
Ctrl + w , h
|
Move to/focus on the split to the left |
Ctrl + w , j
|
Move to/focus on the split below |
Ctrl + w , k
|
Move to/focus on the split above |
Ctrl + w , >
|
Increase width of the focused split |
Ctrl + w , <
|
Decrease width of the focused split |
Ctrl + w , -
|
Decrease height of the focused split |
Ctrl + w , +
|
Increase height of the focused split |
Actions
Key | Description |
---|---|
u
|
Undo |
Ctrl+r
|
Redo |
/
|
find |
>>
|
Indent by shiftwidth amount |
<<
|
Un-indent by shiftwidth amount |
~
|
Toggle selected character case |
.
|
Repeat last command |
x
|
Delete character |
d<movement>
|
Delete in the given movement. Eg. dw deletes a word.
|
dd
|
Delete current line |
D
|
Delete till the end of the line |
p
|
Pastes what was last deleted |
You can repeat actions by specifying the number prior to the command.
- Eg. To delete 10 characters:
10x
- Eg. To delete 10 lines:
10dd
Enter visual line mode with shift+v
starting at the current location. Use movement keys to change what is selected. Actions can then be done to the selected portion of the text.
Enter visual block mode with ctrl+v
starting at the current cursor location. This mode is useful if you wish to change multiple lines at a particular column.
Registers
Registers hold some value and are named a
through z
. You may use registers as a type of clipboard or temporary scratch space. Type :register
to list all registers with values.
Key | Description |
---|---|
"<register>p | Paste whatever is in <register> |
"xdd | Put deleted line into register x |
Macros repeat a set of commands stored within a register.
Key | Description |
---|---|
q<register> | Begin recording macro into register. Eg. qq records into register q.
|
q | End the recording by pressing q again |
@<register> | Repeat macro stored in register <register>. Eg. @q replays macro at q.
|
Tips
Execute Current File using Preset Key
To make vi execute a script that is being edited using a function key (in this case, F5), add the following to your .vimrc
" Mapping F5 to run hashbang scripts
" Will save, then run the interpreter.
au BufEnter * if match( getline(1) , '^\#!') == 0
Toggle Line Numbers
To make vi toggle line numbers using a key (in this case, F4), add the following to your .vimrc
" Map F4 to toggle line numbers
map <F4> :set nonumber!<CR>
Multi-line Edit
Indent lines 1-33 with 2 spaces.
1,33s/^/ /g
Vi Visual Bell Flash
Out of the box vim
flashes to the point where I feel like I'll get a seizure. To turn it off:
:set noflash
Force Tabs
Force tabs instead of soft tabs with tab stops and width set to 4.
:set autoindent noexpandtab tabstop=4 shiftwidth=4
Force Soft Tabs
The reverse of the above:
:set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab
Mouse Scroll Only
To enable mouse wheel scrolling but not mouse interactions:
" Enable mouse support, except for visual mode which will cause right clicks to enter visual mode
:set mouse=nicr
" Disable left mouse
:nmap <LeftMouse> <nop>
:imap <LeftMouse> <nop>
:vmap <LeftMouse> <nop>
" Disable double click
:nmap <2-LeftMouse> <nop>
Search and Replace
You can search and replace with :%s/search/replace/
. To search and replace within the selected range in visual mode, use: :'<,'>s/regex/replacement/options
. Searching within the selected text is enabled by default with my vimrc; just select the text in visual mode and then invoke a search.
To replace with a newline, you cannot use \n
since that maps to a NULL character (^@
). Instead, hit ctrl+v
ctrl+m
in place of the newline.