vim

Vim commands and shortcuts

About

My .vimrc and other files: https://gitlab.com/tuckerw/vimrc/

It’s also a submodule in my dotfiles repo: https://gitlab.com/tuckerw/dotfiles/

Clone repo:

mv ~/.vim ~/.vim.bak
git clone https://gitlab.com/tuckerw/vimrc.git ~/.vim
cat ~/.vim/README.md

Or just get the .vimrc file:

cd
wget https://gitlab.com/tuckerw/vimrc/raw/master/.vimrc # OR
curl -O https://gitlab.com/tuckerw/vimrc/raw/master/.vimrc

View and change options interactively

:options
:options | resize

Editable command and search history

q: view commands

q/ view searches

Enter Executes

<C-c> copies to command line

Registers

:registers - show contents of registers

a-z A-Z - normal available registers

+ - clipboard register

0-9 - number registers (more details later)

Marks

:marks show contents of marks

m <lowercase-letter> mark current line/column with lowercase letter

m <uppercase-letter> mark current line/column/file with uppercase letter

' <lowercase-letter> jump to mark line in current file

' <uppercase-letter> jump to mark line in any file

` <lowercase-letter> jump to mark line and column in current file

` <uppercase-letter> jump to mark line and column in any file

Movements

h go back/left a character

l go forward/right a character

j go up a line

k go down a line

gj go up a visual line (on wrapped lines)

gk go down a visual line (on wrapped lines)

w go forward a word

W go forward a Word (space-delimited)

e go forward to end of word

E go forward to end of Word (space-delimited)

b go backward a word

B go backward a Word (space-delimited)

ge go backward to end of word

gE go backward to end of Word (space-delimited)

gg go to beginning of file

G go to end of file

} go to next empty line (i.e. line after end of paragraph)

{ go to previous empty line (i.e. line before start of paragraph)

]] go to next function/heading (if vim settings and filetype support it)

[[ go to previous function/heading (if vim settings and filetype support it)

<integer>gg, <integer>G go to line <integer>

:<integer> go to line <integer>

<integer>% go to <integer> percent of file

/search-pattern go to next occurrence of search-pattern

?search-pattern go to previous occurrence of search-pattern

f<character> go to next occurrence of character on current line

F<character> go to previous occurrence of character on current line

t<character> go to character before next occurrence of character on current line

T<character> go to character after previous occurrence of character on current line

; equivalent to last f / F / t / T command

, equivalent to the reverse of last f / F / t / T command

'<char> go to line of mark <char>

`<char> go to line and column of mark <char>

Only work with inner / outer ( i / a )

p paragraph

s sentence

t tag

' single-quoted region e.g. 'this is a string'

" double-quoted region e.g. "this is a string"

(, ) parentheses block e.g. (index, value)

[, ] square-brace block e.g. ['one', 'two']

{, } curly-brace block e.g. { i++; closeSession(); }

<, > ankle-brace block e.g. <input type="hidden" name="test">

Scrolling / Cursor Movements

zt Scroll page so current line is at top of page

zz Scroll page so current line is at middle of page

zb Scroll page so current line is at bottom of page

H Move cursor to top of page

M Move cursor to middle of page

L Move cursor to bottom of page

zh Scroll page one column left (requires :set nowrap)

zl Scroll page one column right (requires :set nowrap)

:set sidescroll=n Set page to scroll horizontally by n columns automatically (requires :set nowrap)

<C-e> Scroll page one line down

<C-y> Scroll page one line up

<C-u> Scroll half a page up

<C-d> Scroll half a page down

<C-b> Scroll one page up

<C-f> Scroll one page down

Descriptors

i inner

a around

Actions

Single-character

r replace character under curser or every character in selection with next character entered

x delete character under curser or in selection

s delete character under curser or in selection and enter insert mode (equivalent of cl)

o add newline below current line and enter insert mode

O add newline above current line and enter insert mode

J join current line to next line with a space between

Formatting

= autoformat line(s)

>> indent line(s)

<< de-indent line(s)

Multi-character (require a motion)

y yank

d delete

c change

v visual select

V visual select by line

<C-v> visual-block select

gu make lowercase

gU make uppercase

:sort sort alphabetically

:sort i sort case-insensitively

Examples:

viw select inner word

daw delete around word

c3aw change around 3 words

gUi" make everything in the quoted region uppercase

vip:sort sort the lines in a paragraph

== auto-indent line

=5ap auto-indent current paragraph plus the next 4

>i} indent all lines inside curly braces ({ ... })

Yanking and Pasting

* - Search for word under cursor

<C-r> 0 - paste contents of register 0 when in insert mode (any register can be substituted)

<C-r> " - paste contents of register " when in insert mode (any register can be substituted)

Tabs and buffers

gt go forward a tab

gT go backward a tab

:bn go forward a buffer

:bp go backward a buffer

In my .vimrc only:

gb go forward a buffer

gB go backward a buffer

With these mappings:

" move among buffers
map gB :bprev<CR>
map gb :bnext<CR>

Vimdiff commands

]c               - advance to the next block with differences
[c               - reverse search for the previous block with differences
do (diff obtain) - bring changes from the other file to the current file
dp (diff put)    - send changes from the current file to the other file
zo               - unfold/unhide text
zc               - refold/rehide text
zr               - unfold both files completely
zm               - fold both files completely
:'<,'>diffget    - equiv. of do in visual mode
:'<,'>diffput    - equiv. of dp in visual mode
:diffupdate      - rescan files for changes
:h copy-diffs    - view help for vimdiff

Ed/Sed Commands

:[range]g/pattern/cmd

Where cmd is an ed command! (Will be listed below)

Learn ed:

$ info --vi-keys Ed

Search and replace all array variables unquoted with quotes

In place with sed (saves a .bak file):

sed -i.bak "s/\[\([a-zA-Z]\w*\)\]/\[\"\1\"\]/g" test.php

Search pattern in vim:

\[[a-zA-z]\w*\]

Search-and-replace in vim:

%s#\[[a-zA-z]\w*\]#\[\'\0\'\]#c

Misc

Set tab settings:

:set expandtab
:set tabstop=4
:set shiftwidth=4
:set ft={php,js,html,c,...}
:set ci

Highlight line and column

:set cursorline
:set cursorcolumn

Edit files on remote server

vim scp://user@server//path/to/folder/
vim scp://user@server//path/to/document

To do

Insert-mappings

" Remap if_ to if
:inoremap if_ if () {<CR>}<esc>k<esc>4li

" Remap ie_ to if/else
:inoremap ie_ if () {<cr>} else {<cr>}<esc>kk<esc>4li

" Remap iee_ to if/elseif/else
:inoremap iee_ if () {<cr>} elseif () {<cr>} else {<cr>}<esc>2k<esc>4li

" Remap fn_ to function
:inoremap fn_ function () {<cr>}<esc>kf(i

" Remap pr_ to print "<pre>" . print_r(, true) . "</pre>";
:inoremap pr_ print "<pre>" . print_r(, true) . "</pre>";<esc>F,i

Search for visual selection text

maps // to search for visually selected text

vnoremap // y/\V<C-R>=escape(@",'/\')<CR><CR>

Count matches for pattern

maps / to count matches for pattern

nnoremap <leader>/ :%s///gn<CR>

Indent lines

let g:indentLine_color_term ='darkblue' | e

Colours

Show relevant help

:h highl

Show all highlight groups

:so $VIMRUNTIME/syntax/hitest.vim

Show highlight settings for given group

:hi LineNr

Set highlight settings for given group

:hi LineNr term=underline ctermfg=103 ctermbg=236 guifg=#878787 guibg=#303030