Skip to content

Latest commit

 

History

History
243 lines (193 loc) · 8.11 KB

File metadata and controls

243 lines (193 loc) · 8.11 KB

move

hjkl: left, down, up, right

  • word

    • w: beginning of next
    • b: beginning
    • e: end
  • line

    • O or ^: beginning (I to insert)
    • $: end (A to insert)
    • :n or nG: jump to line n
  • document

    • gg: beginning
    • G: end
  • page

    • CTRL-F: forward
    • CTRL-B: backward

delete

  • x: character (nx to delete n characters)

  • dw: word

  • dd: line (ndd to delete n lines from current position)

  • delete n line(s)

    • dnk: above
    • dnj: below
  • delete current line to

    • D or d$: end
    • d0 or d^: beginning
  • delete from document to

    • dgg: start
    • dG: end
    • dnG: line number n
  • vim-surround

    • csw" or viwS": surround word with quotes (repeat on next word: W.)
    • csw<strong> or vS<strong>: add html tags around word
    • cs'": change surrounding quote to single quote
    • ds": delete surrounding quotation
    • dst: delete surrounding tag
  • other

    • di": delete inside double quotation marks
    • d/c: delete until next character c on any line
    • :g/pattern/d: delete lines matching pattern
    • :g/pattern then :v//d: delete lines not matching pattern

search

  • /pattern and ?pattern: forward/backward for pattern

  • n and N: move to next/previous occurrence of search pattern

  • *: if already on word on screen, hit the * key to search for next occurrence of whole word

  • pattern1\|pattern2\|pattern3: find any of the patterns

  • after searching for word, use :%s//new/g to change all occurrences to 'new', or :g/ to list all lines containing the word.

  • :set hlsearch and :noh: enable highlighting of all searches and turn off highlighting

  • search & replace

    • :s/, /\r&/g: replace all , on current line with a new line
    • :%s/ \+/,/g: replace all whitespace with single comma
    • :%s/cat/dog/ig: replace everything, case insensitive
    • :%s/$/foo/g: add foo to the end of each line
    • :5,12s/foo/bar/g: replace foo with bar globally between lines 5 and 12
    • :%s/\s*\w\+\s*$//: delete last word of each line
    • /\v<[A-Z]+> & :%s//\r&/g: find all capital words and create a newline after match
    • :g/\w$/ s/$/,/: find all lines that end with \w and append a comma

search directory (silver searcher with ag.vim)

  • ,a [options] {pattern} [{directory}]: search code across files in directory
  • ,a flask -l -i -G py: list matching filenames for flask, case insensitive, in filenames that contain py

jumping

  • fc: search forward on current line to character c (dfc: delete)
  • Fc: search backward on current line to character c (dFc: delete)
  • tc: search forward on current line to character before character c (dtc: delete)
  • Tc: search backward on current line to character after character c (dTc: delete)

visual mode

  • v - select by characters

  • V - select by lines

  • preprend multiple lines

    • CTRL-v, highlight lines, Shift-I to insert at beginning of block, press Esc
  • visual select

    • >>: indent the text right by shiftwidth
    • <<: indent the text left by shiftwidth
    • y: "yank" the text into the paste buffer
  • visual insert

    • CTRL V (select column), Shift i (type text to insert), Esc: insert text in multi-line selection
  • paste output from stdout into vim

    • type valid shell/python/R command in vim, visually select all lines, and enter :!python, or :!bash, or :!R --vanilla
  • maximum awesome features

    • vii/vai visually select in or around the cursor's indent
    • Vp/vp replaces visual selection with default register without yanking selected text (works with any visual selection)

new line

  • o: start a new line below
  • O: start a new line above

clipboard

# in .vimrc.local (http://evertpot.com/osx-tmux-vim-copy-paste-clipboard/)
set clipboard=unnamed
  • y: copy/yank
  • p: paste

or

  • :w !pbcopy: copy (after visual selection)
  • :r !pbpaste: paste

yank

  • yy: yank (cut) the current line and put it in buffer

  • Nyy: yank (cut) N lines and put it in buffer

  • :g/pattern/y A: yank all matching lines in one buffer

  • after searching for a pattern, copy only the text that matches, where a is any register (see CopyMatches)

    :let @a = ''
    :CopyMatches a
    

paste

  • p: paste at the current position the yanked line or lines from the buffer

switching case

  • gU: uppercase "HellO" to "HELLO"
  • gu: lowercase "HellO" to "hello"

undo/redo

  • u: undo the previous operation
  • CTRL-r: redo changes which were undone (undo the undos)

commenting

  • gcc toggles current line comment
  • gc toggles visual selection comment lines

sorting

  • :1,10sort: sort lines 1 through 10
  • visual select + :sort: sort selected lines

tabs

  • :tabe {file}; edit file in new tab
  • :tabfind {file}: search path to find and open file in new tab
  • :args *.py | :tab all: open multiple files in new tabs
  • gt: go to next tab
  • gT: go to previous tab
  • CTRL-w SHIFT-t: move pane to tab

run external commands

  • :!ls: shows file list in directory
  • r:!ls: copies the above command into vim window

recording

  • q <letter>: records everything you type (q again ends recording)
  • @ <letter>: replays recording
  • q a + <your edit> + ESC + j h q, use with @ a: repeat command on multiple lines
    • explanation: q a starts recording macro to register a, perform edit, exit insert mode, j moves down one line, q ends recording)

add vertical line at 80 characters

  • :set colorcolumn=80

windows

  • CTRL-w R: rotate windows up/left
  • CTRL-w r: rotate windows down/right
  • CTRL-w L: move current window to the far right
  • CTRL-w H: move current window to the far left
  • CTRL-w J: move current window to the very bottom
  • CTRL-w K: move current window to the very top

exit

  • :qa: close all tabs/windows
  • :wqa: close and save all tabs/windows

multiple files

  • :sp open new file in horizontal split

  • :vsp open new file in vertical split

  • vim -p *: open multiple files in tabs

  • vim -o *: open multiple files stacked

  • vim -O *: open multiple files side by side

  • move between multiple windows

    • CTRL-w + arrow / CTRL-w + w: move between windows
    • CTRL-w t + CTRL-w K: change vertically split windows to horizonally split
    • CTRL-w t + CTRL-w H: horizonally split to vertically split

filesystem

  • Explore filesystem with NERDTree

    • ,d: toggle the drawer
    • ,f: open NERDTree to the current file
    • i: open selected file in a horizontal split window
    • s: open selected file in a vertical split window
    • t: open selected file in a new tab
    • m: delete file in menu
  • Change working directory

    • :pwd: present working directory
    • :cd %:p:h: change directory of currently open file (sets current directory for all windows in Vim)
    • :lcd %:p:h: change directory only for current window
  • full path fuzzy file, buffer finder with ctrlp.vim

    • ,t brings up a project file filter for easily opening specific files
    • ,b restricts ctrlp.vim to open buffers

Easily jump to code definitions

  1. Create a cron job to update tags daily
0 12 * * * ctags --languages=python -R -o ~/.tags ~/path/to/code
  1. add --python-kinds=-i to ~/.ctags to ignore imports
  2. add set tags=~/.tags;/ to vimrc
  3. use CTRL-w ] to jump to code definition (or :TagbarOpen to open viewer)

Vundle

install plugins by adding following to vimrc.bundle.local

Plugin '<user>/<plugin>'

and run

:PluginInstall

sourcing

  • :so filename: source vim code in filename

other

  • ,[space] strips trailing whitespace
  • ,l begins aligning lines on a string, usually used as ,l= to align assignments