Hi all, I’ve been trying to learn vim and thought it would be a good idea to make a post with my notes. I can’t really give back to the community by answering questions and helping people with their technical issues so I was hoping that these notes may benefit someone. I made the notes gathering information from a few different places and the commands aren’t in the best order.
:help
(vim help menu. Within the help menu whatever is enclosed in brackets are optional things)
:help dd
(get help on a specific command, can use just do :h dd
for example)
Ctrl o
(within help go back to previous help section)
Ctrl i
(within help go forward to the next help section)
Ctrl ]
(on a word within help will take you to the help pg about that word if there is one)
Ctrl d
or tab
(auto-completion if your not sure of the full command)
vimtutor
(practice vim)
:wq
or :q!
or :w
or :q
(save and quit or quit without saving, save, quit)
shift ZZ
(saves and exits)
Shift ZQ
(force quit without saving)
:w [file name]
(save file)
:r /etc/fstab
(read the file and paste the content where the cursor is)
:[Line-n.o] (hit Enter)
(takes you to that line)
[Line-n.o] gg
(takes you to that line)
[Line-n.o] shift G
(takes you to that line n.o)
:earlier 5m
(takes you back in time 5mins to where you were in the file)
:later 5m
(takes you forward in time 5mins to where you were)
gg
(takes you to the top of the file)
shift G
(takes you to the bottom of the file)
Ctrl f
(move forward, to page down)
Ctrl b
(move backwards, to page up)
shift }
(go down a block of text)
Shift {
(go up a block of text)
[n.o]%
(takes you to that percent of the file eg 25% or 50%
)
$
(takes you to the end of the line)
:$
(takes you to the end of the file)
shift ^
(the carrot takes you to the beginning of the line)
hjkl
(moves around on lines with text, to move down to a line with no text gj
or up gk
)
[n.o]j
(go down that many lines)
Adding a n.o before most of these commands will do the command that many times. eg 10dd
would delete 10 lines. 5u
would undo x5)
w
(moves you to the beginning of the next word)
n.o w
(moves that many words forward)
e
(moves you to the end of the next word, n.o e
moves you that many words forward. web
are motions)
b
(moves you backwards to the beginning of the previous word, n.o b
moves you that many words backwards)
WEB
(the capitals do the same movements forwards and backwards but ignores commas etc)
z Enter
(keeps the cursor on the line it’s on but moves the line closer to the top)
zz
(keeps the cursor on the line it’s on but moves that line to the middle of the screen)
zt
(moves the line to the top)
zb
(moves the line to the bottom)
Ctrl d
(go down half a page)
Ctrl u
(go up half a page)
f[letter]
(searches forward to that letter, so the cursor will be on the letter)
F[letter]
(searches backwards to that letter)
;
(moves to the next search result)
t[letter]
(searches forward until that letter so the cursor will be on the letter before the letter you chose)
T[letter]
(searches backwards, cursor will be on the letter before the one you chose)
dt[letter]
(deletes up until that letter)
0
(takes you back to the first character on the line that you’re on)
ctrl g
(shows info, filename, n.o of lines and what percentage the cursor position is at within the file)
g Ctrl g
(shows even more information)
:set ruler
(shows line and character n.o)
:set noruler
(removes the option set ruler
)
:set ruler!
(toggles the option on or off depending on whether it was on or off)
Ctrl c
or Esc
(cancel an action depending on .vimrc config)
Esc
(command mode) (I remapped it to kj
)
i
(insert mode, under the cursor) tip: 80i*Esc
(create a line of 80 asterisks)
I
(moves the cursor to the beginning of the line and enters into insert mode.)
a
(places you in insert mode one space forward of the cursor)
shift A
(puts you at the end of the line in insert mode)
s
(deletes the character under the cursor and enters you into insert mode)
S
(deletes the entire line then enters you into insert mode)
o
(makes a new line beneath the cursor and places you in insert mode.
O
does the same but above the cursor) tip: 5o#Esc
(creates 5 lines beginning with #) 5o10.11.12.Esc
(creates 5 lines beginning with 10.11.12.
ce
(c
for change e
for end of the word, deletes the word and puts you in insert mode so you can quickly retype another word, the cursor should be at the beginning of the word)
(all the syntax for the d
command apply with c
)
cw
(change word)
c$
(deletes a whole line onward from the cursor and places you into insert mode)
C
(is the same as c$
)
cc
(change entire line)
ct[character]
(c
change t
until last character)
u
(undo last change)
U
(undo changes to the whole line)
Ctrl r
(redo)
~
(either changes a letter from uppercase to lower case or vice versa.)
g~w
(changes an entire word from uppercase to lower case or vice versa.)
g~$
(changes an entire line)
Shift J
(appends the line below to the line the cursor is on)
r[letter]
(replaces the letter under the cursor, could also enter vision mode highlight block of text then replace it)
R
(replaces multiple characters,
cw
is better though because you won’t end up writing over other words If your replacement word is longer)
x
(delete one character under cursor)
[n.o] x
(deletes that many characters)
X
(deletes one character before the cursor)
dd
(delete whole line. But it also copies the line so it could be pasted with p
)
4dd
(delete 4 lines)
dw
(deletes word but cursor should be at the beginning of the word)
dW
(deletes one word and other punctuation, it would delete all of this: i915.enable_guc=2
)
db
or dB
(deletes backwards)
dk
(delete the line your on and the line above because that is where the motion takes you)
dj
(delete the line your on and the line below because that is where the motion takes you)
d0
(delete from where you are to the beginning of the line)
d$
(deletes everything from the cursor to the end of the line)
D
(deletes from the cursor until the end of the line)
d3w
(deletes 3 words from the cursor onwards) (3dw
is the same as d3w
)
daw
(deletes a word and any space around it. The cursor doesn’t need to be at the beginning of the word)
diw
(deletes just the word and not the space around it. i
for inside.)
dip
(deletes only the whole paragraph, not the space around it)
dap
(deletes the paragraph and the space around it)
di(
(deletes text within brackets)
da(
(deletes brackets and enclosed text)
dt[character]
(delete everything until the character)
yw
(yank/copy word, from where the cursor starts onwards)
yy
(yanks an entire line, without having to highlight it.)
2yy
(yank 2 lines)
y$
(yanks from the cursor until the end of the line)
y2w
(yank/copy two words)
yi(
(copy whats inside these brackets)
ya(
(copy brackets and what’s enclosed within them)
yip
(copy the whole paragraph, not including space around it)
/[word] enter
(searches forward from the cursor, n
will then go to the next instance of that word and N
will go backwards to the previous instance of that word)
nohls
(temporarily turns the highlighted text off until the next search)
*
(searches for the word which is under the cursor, n and N stills moves to next search result)
ctrl z
(takes you out of vim back to command prompt, whilst vim is in the background)
jobs
(shows what jobs are in the background)
fg
(foreground, brings vim back)
fg [n.o]
(after looking at jobs you can choose the n.o to bring back to the foreground the file you want.)
:! [Linux command]
(whilst within vim run a command, once done hit enter to return to vim)
:! l[Ctrl d]
(will tell me every command that begins with the letter l
eg ls
)
:e /etc/pacman.d/mirrorlist
(opens a new buffer, basically opens the file in another vim editor, e for edit.) (This is to have multiple files open at once in vim.
:e [newfile])
:bn
(buffer next, to swap between the files)
:bp
(buffer previous)
:bd
(buffer delete, to get rid of that one file but not come out of vim)
badd [filename]
(buffer add, meaning open another file but stay in the file which your already in)
:enew
(new buffer, meaning new unnamed file.)
vim -o [file-name] [filename]
(opens in split mode)
vim -O [file-name] [filename]
(opens in vertical split mode)
:split [file-name]
(horizontal split)
:vsplit [file-name]
(vertical split)
:vs [file-name]
(same as above just short hand)
Ctrl ww
(in split mode to jump to the other window)
:vs [same file already in]
(if you need to reference something in the same file at the top and work at the bottom this makes it easy to do) Works with either two of the same or different files.
:set number
(to add line numbers to the file)
:set nonumber
(gets rid of the numbers)
vim +12 /etc/mkinitcpio.conf
(opens the file and places you on line n.o 12)
:set spell
]s
(next spelling)
[s
(previous spelling)
z=
(spelling suggestions)
zg
(spelling good, add)
zG
(spelling good, session)
zw
(spelling wrong, add)
zW
(spelling wrong, session)
:spellr
(spelling repeat all in file)
-
Registers in vim are like a clipboard
-
When using
d c s x y
commands the text gets put into registers -
Unamed registers
""
hold text fromd c s x y
commands -
Named registers (There are 26 named registers from
a-z
. Text can be copied or deleted into these named registers to be pasted later.) -
Numbered registers
"0 "1 "2 - "9
-
Register
"0
holds last text yanked -
Register
"1
holds last text deletedd
or changedc
-
Numbered registers shift the text along to the next register with each usage of the
c
ord
commands until they get to register "9 and drop off.
:reg [Enter]
(view the registers)
:reg b 9
(specific registers can be viewed)
"0 shift P
(paste the yanked text from register "0)
"1 shift P
(paste the last deleted or changed text from register "1
)
"_
(represents the black hole register) - "_dd
(using the black hole register to delete a line won’t show up in the registers. You could delete something using the black hole register but it wouldn’t change the unamed register so you could paste what was already in the unamed register.)
"ayy
(yanks a line of text and keeps it in the named "a register. If "ayy
is used again then the register gets replaced by new text but if "Ayy
is used then the old text would be appended with the new text. Applies to all capitals)
"byy
(same as above but placed into "b
register.)
"ap
or "bp
(to paste from the named registers.)
In vim a buffer is a file.
.
(the period repeats the last action that you did eg cw cat
. If you go to another word it’ll change it to cat if you type the period)
v
(enter visual mode.)
V
(highlights text line by line)
Ctrl v
(highlights with block selection)
- Highlight a block of text then
:norm
(repeats the previous command upon the highlighted text)
vip
(highlight paragraph not including space outside of it)
vap
(highlight entire paragraph and space around it)
p
(paste below the cursor if a whole line or after the cursor if a set number of words) or [n.o]p
(to paste multiple times)
P
(paste above the cursor if a whole line, behind the cursor if a set number of words)
:sort ui
(highlight text then this will sort the lines alphabetically.)
:s/cat/dog/
(without a g
at the end it will only change one instance of cat
on the line of the cursor, with the g
it will change every occurrence on that line.)
:%s/what you want to change/change to/g
(this will change every occurrence of the word to your chosen word in the whole file, s
stands for substitute g
for global)
:%s/cat/dog/gc
(same as above but gives a prompt on each occurrence asking whether to change it or not)
:3,9s/the/that/g
(this will change all words the
to that
from line 3 to 9)
~/.Vimrc have to use sudo -E vim [filename]
-
To be able to use sudo (the root account) with the users
~/.vimrc
configuration you have to runsudo -E vim [filename]
-
There are three alternatives to
sudo -E vim
-
- Create an alias for
sudo -E vim
in.bashrc
like so:alias svim='sudo -E vim'
then runsource .bashrc
(when running the command for sudo vim it then should be run like so:svim [filename]
- Create an alias for
-
- Copy mine to roots:
sudo cp ~/.vimrc /root/.vimrc
(but this will have to constantly be done to keep them both synced.)
- Copy mine to roots:
-
- Create a symbolic link so both the users and roots
.vimrc
files stay synchronised:sudo ln -sf ~/.vimrc /root/.vimrc
- Create a symbolic link so both the users and roots
-
View
.vimrc
options with:h option-list
and then:q
to get out the options - From within vim options can be turned on/off and queried whether they are on/off.
:set ruler
(turns the ruler option on)
:set noruler
(turns the ruler option off)
:set ruler?
(check if the option is on or off. If it returns ruler then it’s enabled, if it returns noruler then it’s disabled.)
:set ruler!
(toggles on and off)
My ~/.vimrc
" adds line numbers. (the speech quotes comments out the line)
set number
" esc in insert mode.
inoremap kj <esc>
" esc in command mode.
cnoremap kj <C-C>
" Note: In command mode mappings to esc run the command for some odd
" historical vi compatibility reason. We use the alternate method of
" exiting which is Ctrl-C
"Colour highlighting.
:filetype plugin on
:syntax on
"Spell checking.
"set spell
"Keep 1000 items in the history.
set history=1000
"Show the cursor position.
set ruler
"Show incomplete commands.
set showcmd
"Shows a menu when using tab completion.
set wildmenu
"Keeps lines above the cursor when moving with z.
set scrolloff=5
"Highlights search.
set hlsearch
"Incremental searching. (highlights as you type)
set incsearch
"Line wrapping doesn’t break mid-word.
set lbr
"Good indentation for coding.
set ai
set si
"Vim will use suitable colours for the background.
set bg=dark
"set bg=light
"Changes colour of the text.
color koehler