Vim Advanced cheatsheet

Commands

Edit

T,c, Delete between coma
:b#<bar>bd# delete buffer
C = c$: Change to end of line
S = ^C: Change from first non blank to EOL
gqmotion Filter format
g=motion Filter indent
!<motion><cmd> Filter by

Move

'. Last edit location
g- Last non blank character (Vs _ or ^)
gm Middle of line
n<bar> Column n of current line
ab aB Select a block ( ), a block { }
- + line up, down on first non-blank character

Move cursor

zl, zh, zL, zH Scrool horizontally, one char or screen
zt, zz, zb Cursor top middle, bottom
<C-W>Z :pc[lose]: Close previw window
1gt Goto tab 1

Ex tips 1/2

:w !sudo tee % Save as root
:set nrformat+=alpha <C-A> works with alphabet chars
:sort [u] [n] Sort [Unique] [Numerals (so 100 after 20)
:echo 5e9x20 Simple Math
:q:, q/ Ex, Search Windows
normal @a Execute register a in this line
g/{/_.+1,/-1 sort Sort between { and }
:sort n/\(\S\+\s\+\)\{2}/ Sort by 2nd column

Ex tips 2/2

:%s/^/\=printf('%-4d', line('.')) Prefix line by line number
:set fo+=t Autowrap (h formatoption)
:bro[wse] ol[dfiles][!] Prompt which file to choose
:earlier 12h Change file to how it was 12h ago
:%! column -t align columns
:syntax sync fromstart Reload syntax
set cul! if scrooling slow for a long line
set lazyredraw can be usefull too
h ttyfast

Ex tips 3/2

:v/./,/./-j Compress multiple blank lines into on

Ex tips 4/2

Some usefull function * Function: sort(list), put, getline(int), setlone(int, list) join(list), split(string), len(list), get, getpid()

Sort line accroding to line length

:%s/.*/\=printf("%03d", len(submatch(0)) . "|" . submatch(0)/ | sor n | %s/..../  

Reverse line order

:%!tac
:%!tail -r
:g/^/m0

Man

runtime! ftplugin/man.vim
:Man 3 printf

Auto cd %

set autochdir  " auto chdir to current file
:tabdo lcd /dir/

Substitute without escaping the replcement

:%sno/search_string/replace_string/g
:s/</\='&lt;'/g

Delete everything except text

Remove duplicate lines

:g/\v^([^ -=]+)\n\_.*%(^\1$)/d

Binary editing

222go Goto byte 222

Xxd

  • xxd -r (reverse, from hex to string) -p (output in ps := raw) -c 8 (bytes per line) -g 1 (bytes per column)

No newline at end of file

  • vim -b file or set binary
  • :set noeol or set nofixendofline

File encoding

Ctags

Vimscript

Get|Set (ou|in)tput from ex commands

redir @a
echo toto
redir END
"Examples: 
":call Exec('buffers')
"This will include the output of :buffers into the current buffer.
"Also try: ":call Exec('ls')
":call Exec('autocmd')
funct! Exec(command)
    redir =>output
    silent exec a:command
    redir END
    let @o = output
    execute "put o"
endfunct! 

Inspect, Debug

TODO ### Inspect syntax highlight (link)

map K :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<' . synIDattr(synID(line("."),col("."),0),"name") . "> lo<" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">"<CR>

Display environment

To find the script file defining a map : :verbose map <c-z>. But map can ber replaced by:

Get filetype of a buffer

* `:let bufFiletype = getbufvar(bufNr, '&filetype')`

Show highlight syntax type

  • :echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')

Get filetype of a buffer

  • :let bufFiletype = getbufvar(bufNr, '&filetype')

Vimrc tips

Vimrc: GVim Remove useless bars

:set guioptions-=m " remove menu bar
:set guioptions-=T " remove toobar
:set guioptions-=r " remove right hand scrool bar 
:set guioptions-=L " remove lefthand scrooll bar

Ex one char

Ex 1 char (1/3)

ab abbrev
a append
ar args
c change
co copy
t Copy line to
d delete
e edit
f file
g global
i insert
j join
l list
maa map
ma mark

Ex 1 char (2/3)

m move
n next
nu number
o open
pre preserve
p print
pu put
q quit
re read
rec recover
rew rewind
se set
sh shell
so source
st stop

Ex 1 char (3/3)

s substitute
una unabbrev
u undo
unm unmap
ve version
vi visual
w write
x xit
ya yank
z window
! escape
CR print next
& resubst
> rshift
^D scroll

Command line

Pipe

Remote

vim --servername DEMO
vim --servername DEMO --remote file.txt
vim --servername DEMO  --remote-send ":3d<CR>"
:!vim --servername DEMO --remote-tab "%"   " Send current buffer to remote vim

Dos2Unix: for file in * ; do vi +':w ++ff=unix' +':q' $file

C-s