Starting vim
Insert vs command mode Saving and quitting |
Insert commands
Other editing commands Moving around the file |
Search and replace
Screen appearance and vim options Entering special characters Macros: creating your own commands |
Core commands for getting started with vim
sample configuration file: .vimrc to be placed in your home directory |
If the system crashes or you lose connection while you're editing, you can usually
recover what you were doing by typing vim -r filename
Save the changes in the recovered file, quit, then remove the file named .filename.swp (so vim knows you've finished recovering). |
Commands that take you into insert mode: (again, hit commands to alter existing text (also treated as insert mode):
corrections you can perform while in insert mode:
First, press q (to begin recording) then the letter you want to use for your command,
e.g. x
Then type your command
Finally, press q again to end the recording
To run your macro you use @x (or whatever your chosen letter was)
You can also store these in your .vimrc using
When you type your command (in the appropriate mode) it runs the specified key sequence as if you
had typed it in.
These are often used to create easier key sequences for commands you commonly use,
or to give access to commands that are difficult to enter on some keyboards (e.g. an escape key
on some phone apps).
Example: create a command Example: create a command As with the options settings, you can place your macro definitions in your .vimrc file
so they are automatically loaded every time you start vim.
commands to start inserting new text:
a
append (insert) after the cursor position
i
insert before the cursor position
A
append (insert) at the end of line
o
open (insert) on the line below the current line
O
open (insert) above the current line
I
insert before the first non-blank character
cw
replace the rest of the current word with new text
C
replace the rest of the current line with new text
R
allows you to type over characters until you hit
the escape key
r
replace the single character under cursor with a new
character (once you type the single new character,
this command automatically takes you back to command
mode)
^H
(control-h) erases the last character typed
(like the backspace key)
^W
(control-w) erases the last word typed
ESC
(escape key) ends insert mode,
and returns you to command mode
^C
(control-c) this is the interrupt command for most
linux programs, and in vim suspends insert mode
^D
(control-d) backtabs one character
^^D
caret (^) followed by control-d (^D);
backtabs to beginning of line;
^V
(control-v) quotes a non-printable character
Other editing commands available in command mode
When not inserting text, there are many commands available
for manipulating the file contents:
J
automatically joins the next line to the current one
x
deletes the current character (by cursor position)
dw
deletes the rest of one word (from the current
cursor position )
D
deletes the rest of the current line (d$)
dd
deletes the current line
ma
d'a
yw
"yanks" a copy of the current word
yy
"yanks" a copy of the current line
ma
y'a
p
:r filename
.
u
^r
(hold down control and press r)
:earlier Nm
e.g. :earlier 15m
would undo all the changes from the last 15 minutes
U
"dp
e.g. to delete 17 lines type 17dd
or to delete 12 characters type 12x
q:
allows you to browse through and select recently used
vim commands
:r filename
:.! commandname
For instance :.! date
will get the current date and time
and put it in the file.
Moving around in the file and repositioning things on the screen
This changes the portion of text shown on the screen, but does
NOT change the cursor position
(using CR
to indicate hitting the return or enter key
and ^ to indicate holding down the control key)
^L
clears and redraws the window
zCR
redraws the screen with
the current line at the top of the window
z-CR
redraws the screen with
the current line at the bottom of the window
z.CR
redraws the screen with
the current line at the center of the window
^E
scrolls the text up 1 line
^Y
scrolls the text down 1 line
^U ^D
scrolls up or down one half screen
^F ^B
scroll up or down an entire screen
zz
centres the screen on the line the cursor is on
^G
displays which line number you're on now
nG
jump to the start of line number n
G
jump to the start of the very last line of the file
h
(left),
j
(down), k
(up), and l
(right)
%
move to the matching ( ) { or }
THIS IS A WONDERFUL COMMAND FOR EDITING YOUR C++ CODE:
to make sure you have your brackets lining up correctly,
position the cursor over one bracket, hit the %
key,
and the cursor jumps to the bracket that currently matches up
with the one you're on(
move to the beginning of the current sentence
)
move to the end of the current sentence
{
move to the beginning of the current paragraph
}
move to the end of the current paragraph
H
move to the top line on the screen
L
move to the last line on the screen
M
move to the middle line on the screen
CR
move to the next line, at the first non-whitespace
-
move to the previous line, at the first
non-whitespace
^
move to the next non white-space character
0
move to the beginning of the line
$
move to the end of the line
w
move forward a word
b
move back a word
e
move to the end of the current word
U
restore the current line
.
repeat the last change
"dp
retrieve the d'th last delete
Search and replace
/pattern
search forward (from the cursor) for the pattern
?pattern
search backward (from the cursor) for the pattern
n
repeat the last search (in the same direction as before,
but starting from your new position)
q/
and q?
allows you to browse through
and rerun your recent search commands
xxxx
with the new pattern yyyy
, from the current
cursor position to the end of a file, use the command:
:.,$s/xxxx/yyyy/g
Just by way of explanation:
:
is for command mode
.
is to start from the current position
,$s
is (I think) to indicate string replacement
/xxxx/yyyy/
is to indicate the old/new patterns
g
handles multiple xxxx
matches per line (if you only want to change the first
xxxx
on each line, then leave off the g
)
Screen appearance options
:set nu
:set nonu
:set ruler
:set noruler
:set cul
:set nocul
:set list
:set nolist
:set listchars=tab:>-,trail:@
:syntax on
or :syntax off
:color schemename
,
providing the name of the desired colour scheme.
Some of the colour schemes on our system
include: blue, darkblue, default, delek, desert, elflord, evening,
industry, koehler, morning, murphy, pablo, peachpuff, ron, shine,
slate, torte, zellner.
:sp filename
splits into a top/bottom window (new one on time),
:vsp filename
splits into left/right windows
^w
and ^W
let you switch between windows (forward/back)
:b#
toggles between the two most recently used windows
:resize N
adjusts the number of lines allocated to the current window
:vertical resize N
does the same for the number of columns
:set tabstop N
sets the default tab width
Entering special characters
While in insert mode,
the control-v sequence is useful to enter special characters in your text.
Macros: creating your own vim commands
let @x='your actual command stuff goes here'
map yourcommandname keysequence
,
and your own commands for insert mode using imap yourcommandname keysequence
v
to swap two characters using commands x and p
(so if you accidentally type "taht" instead of "that" you can fix it with a single keystroke)
:map v xp
jj
to allow you to escape from insert mode without needing
an escape key (the assumption being you rarely need to put "jj" in your text)
:imap jj <ctrl>-v <esc>