vim

copy contents to/from files

to copy the contents into a file

:r <filename>
or
:"qY # yank out of first file
:"qP # put into second file
or read range of lines
:r! sed -n <n>,<m>p /path/to/file.md

to copy the contents to a new file

:<n>,<m> w <filename>
where <n> and <m> are numbers or symbols that designate range of lines

text wrap

(wrap to column) wrap current line

gqq

wrap entire file

gqG

wrap paragraph using visual selection

V}gq

also use visual or visual block with gq

spell

using_spellcheck check spelling

Spell

move to word

]s [s

and see results

z=

turn off highlighting

set nospell

buffers

incrementing numbers

post on reddit

select several lines containing '0' and type g<C-a>

split

  • movement <Ctrl>-w [h,j,k,l] <Ctrl>-w w
  • rotate <Ctrl>-w r
  • orientate <Ctrl>-w J <Ctrl>-w L
  • sizing <Ctrl>-w - # decrease height by 1 line <Ctrl>-w + # increase height by 1 line <Ctrl>-w < # change width by 1 line to the left <Ctrl>-w > # change width by 1 line to the right <Ctrl>-w 10 - # decrease height by 10 lines, etc...

diff

:windo diffthis
:windo diffoff

list recent doc

:ol[dfiles]

replace

  • change inner word ciw

delete

  • until but not dt <char>
  • until and df <char>

visual

  • visual select to end of line excluding whitespace
    vg_

using the shell

vim and the shell

  • run current buffer as script

    :!./%
  • in script, highlight line and switch to command mode by pressing :

    :'<,'>w !bash -
:'<,'>w !python -

directory of current buffer

open new file in dir of current buffer

:e %:h/filename

save file as new file in dir of current buffer

:sav %:h/filename

substitute on multiple lines

stackoverflow 19996145

:6,10s/<search_string>/<replace_string>/g | 14,18&&

reverse substitute

  • adds a tab to the beginning of all lines that don't start with Pattern
    :%v/^Pattern/s/^/\t/

write regex match lines to new file

:g/^Pattern/ .w >> <filename>
  • use .w! if file doesn't exist

sort visual block

vim fandom

:'<,'>sort /\ze\%V/

move current line (scroll-cursor)

z<enter> or zt - move to top of buffer z. or zz - move to centre of buffer z- or zb - move to bottom of buffer

(zEnter, z., and z- puts the cursor in the first non blank column. zt, zz, and zb leaves the cursor in the current column)

:help scroll-cursor

open a terminal

:terminal
:vert term

ctrl-w shift-n puts terminal in normal mode a or i returns to shell

yanking into a register

"qp paste the contents of the register to the current cursor position i enter insert mode at the begging of the pasted line ^ add the missing motion to return to the front of the line <escape> return to visual mode "qyy yank this new modified macro back into the q register dd delete the pasted register from the file your editing

editing the register visually

:let @q=' open the q register <cntl-r><cntl-r>q paste the contents of the q register into the buffer ^ add the missing motion to return to the front of the line ' add a closing quote <enter> finish editing the macro

insert full path of current file

"%p
:put=expand('%:p')

netrw

help

  • jump to topic - Ctrl-]
  • return - Ctrl-o

folding

| key | desc | | -- | -- | | zf#j | creates a fold from the cursor down # lines | | zf/ | string creates a fold from the cursor to string | | zj | moves the cursor to the next fold | | zk | moves the cursor to the previous fold | | za | toggle a fold at the cursor | | zo | opens a fold at the cursor | | zO | opens all folds at the cursor | | zc | closes a fold under cursor | | zm | increases the foldlevel by one | | zM | closes all open folds | | zr | decreases the foldlevel by one | | zR | decreases the foldlevel to zero -- all folds will be open | | zd | deletes the fold at the cursor | | zE | deletes all folds | | [z | move to start of open fold | | ]z | move to end of open fold |