Skip to content

Instantly share code, notes, and snippets.

@marcusshepp marcusshepp/vim.md
Last active Dec 6, 2017

Embed
What would you like to do?
My Vim Cheat Sheet

Edit

  • replace current file with new file: :e path/to/file

  • undo: u

  • redo: ctrl r

  • increment/decrement number: CTRL A, CTRL X

  • change case: ~

  • change case of full word: viw~

  • join two lines: J

  • fix line indent: ==

  • indent/unindent block (vis m): >/</5<

  • open new line after/before current: o/O

  • replace the char that the cursor is over: r

    • ie rb will change the current char to a b
  • cut:

    • cut the rest of the line and enter insert m: C
    • cut until end of word: ce
    • cut current line: cc || S
    • cut current char and switch to insert m: s
  • copy:

    • from curr cursor position to line ending: y$
    • whole line: yy
    • current word: yw
    • current word: bye
    • from cursor to beginning of line (norm m): v0
    • copy to Mac OS X clipboard: visually select text(type v or V in normal mode) and type :w !pbcopy
      • copy the whole file :%w !pbcopy
      • paste from the clipboard :r !pbpaste
  • paste

    • creating a new line under cursor: p
    • creating a new line above the cursor: P
    • when pasting large amount of text from mac clipboard to linux vim: :set paste will help with special characters
  • deleting

    • everything after the cursor (normal m): D or $d
    • everything before cursor until end of line (normal m): d0
    • delete line (normal m): dd
    • delete a word:
      • backword (insert m): ctrl+w
      • word under the cursor: daw
      • word under the cursor, then enter insert mode: caw
      • next word after cursor (normal m): dw
      • diw will delete the entire word without touching whitespace around it
    • delete 4 lines: 4dd
    • delete 3 chars: 3x
    • cut everything between:
      • {}: ci{
      • []: ci[
      • (): ci(
  • commenting

    • ctrl+v (visual block mode)
    • select lines you want
    • Shift+i (capital I)
    • insert the text you want, i.e. #
    • esc
    • (might take a couple seconds)

Visual

  • select by moving cursor: v
  • select whole lines: V
  • select blocks: ctrl+v
  • move cursor to other side after selection (can toggle): o

SHORTCUTS

Some operator-motion commands are used so often that they have been given a single letter command:

x  stands for  dl  (delete character under the cursor)
X  stands for  dh  (delete character left of the cursor)
D  stands for  d$  (delete to end of the line)
C  stands for  c$  (change to end of the line)
s  stands for  cl  (change one character)
S  stands for  cc  (change a whole line)

Search

  • search for string (normal m): /foo
    • next: n
    • prev: N
    • next 5th result: 5n
  • search and replace (normal m):
    • all matches: :%s/find this str/replacewiththis/g
      • confirm all matches: :%s/find this str/replacewiththis/gc
      • ignore case: :%s/find this str/replacewiththis/gi
    • on specific lines: :56,59s/foo/bar/g
  • search for word under cursor: * || #
  • to ignore case place \c anywhere in pattern
  • to force case matching place \C anywhere in pattern
  • http://vimregex.com/
  • to turn off highlighting until next search: :noh

Move

  • end of line (normal m): $
  • end of line then enter insert mode: A
  • beginning of line (normal m): 0
  • beginning of line where text begins (normal m): ^
  • beginning of line then enter insert mode: I
  • page down (norm m): ctrl-f
  • page up (norm m): ctrl-b
  • go to line: :222
  • current screen:
    • top: H
    • middle: M
    • bottom: L
  • jump to next paragraph: { && }
  • to next word: w
  • to next space-separated word: W
  • beginning/end of current word: b/e
  • beginning/end of space-separated word: B/E
  • beginning/end of file: gg/G
  • last edited line: '.
  • last edited position: g;
  • move to char first instance of "x": fx
    • works for any char
  • move to next matching {, [ or (: %

Tree View

  • netrw
    • to enter netrw: vim, :E or :Explore
    • toggle diff tree views: i
    • to be able to toggle whats inside a folder without entering: echo "let g:netrw_liststyle=3" >> ~/.vimrc
    • list o commands:
      • create new dir: d
      • create and open new file: %
      • delete dir or file: D
      • rename a file: R
      • open a file in horizontal split: o
      • open a file in vertical split: v

Tabs

  • new tab (normal m): :tabe path/to/file
  • next tab (normal m): :tabn
  • prev tab (normal m): :tabp
  • first tab: :tabfir || :tabfirst
  • last tab: :tablast
  • save and close a tab (normal m): ZZ || :wq (they do the same)
  • open multiple file in tabs: vim -p path/to/first/file path/to/another
  • list all current buffers: :ls
  • open multiple files from within vim: :arga foo.txt bar.txt, :arga /foo/bar/*.txt

Panes

  • split pane horizontally: :sp path/to/file
  • split pane vertically: :vs path/to/file
  • switch between panes: ctrl-ww

Misc.

  • auto completion

    • list of suggested words (insert m): ctrl+n
  • line numbers

    • :set number, :set nu
    • :set nonumber, set nonu
    • echo "set number" >> ~/.vimrc
    • echo "set relativenumber" >> ~/.vimrc
  • tab length: set tabstop=4

  • see/view the path of the current open file:

    • (normal m): :f or ctrl+g
    • % will print the current file name: :!echo "current file: %"
    • echo "set statusline += %F" >> ~/.vimrc
  • back to previous buffer: ctrl+o

  • chaining commands together: use | as a seperator ie. to write a file then go into tree view you'd use: :w|E

  • defining functions in .vimrc:

function func()
    :command1
    :command2
endfunction

to use this command: exec func()

  • open a url: vim http://google.com/
  • bring file back to how it was 15 min ago: :earlier 15m
  • to see all whitespace as characters: set list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.