Skip to content

Instantly share code, notes, and snippets.

@herrera-ignacio
Last active May 17, 2020 23:43
Show Gist options
  • Save herrera-ignacio/4a0aa25d829d9ac19882f43eaf1076fb to your computer and use it in GitHub Desktop.
Save herrera-ignacio/4a0aa25d829d9ac19882f43eaf1076fb to your computer and use it in GitHub Desktop.
Vim Cheat Sheet

Cheat sheet

Splits

  • :split <file>
  • :<lines_height>sp <file>
  • :vs <file>
  • Ctrl+w+<direction> to move between splits

Remember you can use Ctrl+^(6) to switch between buffers.

Modes

  • Normal - Command
  • : Line (from normal)
  • i/a Insert
  • R Replace

Leave mode with ESC

Common patterns

  • [count]operation{motion}
  • operation operation: does operation on all the line (for example g~~ for caps), this happens as repeating operation gets interpreted as a line motion ($)
  • ./u/redo repeat/undo/redo
  • `:help

Navigation

  • j/k Up and down

  • h/l Left and right

  • Ctrl f page down

  • Ctrl b page up

  • w/b move right/left one word

  • W/B move right/left one word ignoring puntuaction

  • 0 beginning of line

  • $ end of line

  • gg first line of the file

  • G last line of the file

  • <LINE_NUM>gg or :<LINE_NUM><Enter> specific line

  • CTRL G Get information about lines

Deleting

  • x/X deletes from cursor / del previous
  • dw delete word
  • dd deletes current line
  • d + motion (j/k) deletes line + line you would have navigated to
  • d + motion (0/$) deletes till the beginning/end of the line

Cut Copy Paste

  • d and x don't delete, they cut
  • y yank (copy), yy to fast copy line
  • p/P put below/above actual line
  • <REG>p put from desired register

Registers :reg <ENTER> You can use a specific register for example, black hole register "_dd. You can also use specific registers, for example "ayy to copy line to "a register, and then put from it with "ap.

You can append to a register instead of insert, using its name in caps. For example: "Ayy will append instead of replace all content.

  • Unnammed:
    • "" holds text from d,c,s,x and y operations
    • "0 holds last text yanked (y)
    • "1 holds last text deleted (d) or changed (c)
  • Numbered shift with each d or c

Transforming and Substituting Text

  • I shift from beginning of line and go into Insert mode
  • a insert after current position
  • A insert after end of the line
  • o/O insert on new line below/above current one
  • cw change word, this can be used with a specific register
  • c$ replace till the end of the line
  • cc repalce line
  • ~[motion] invert caps, g~~/g~$
  • gU[motion]/ gu[motion] mayus or minus
  • shift [direction] join lines

Search, Find, Replace

  • f/F {char} Search forward/backward in line (case sensitive)
  • t/T {char} same as f but places cursor before/after result
  • ;/, next/previous ocurrence

For example, you can delete everything up until next ocurrence of a character with d t {char}

Search across file

  • /{word} <ENTER> forward search (in command line mode)

  • ?{word} <ENTER> reverse search

  • n / N next/previous ocurrence

  • c w to replace word, you can basicaly use d c y

  • /\<is\> regexp allowed, with this example you can find only exact word

  • * / # search for newxt/previous occurence of current word

  • :[range]s/<word1>/<word2>/[flags] replaces occurences of word1 with word2 in current line.

  • g flag to replace every ocurrence

  • range 1 or more line lumbers separeted by comma

  • range can also be:

  • . current line

  • .,$ current line till last line

  • % all lines

  • /PATTERN-1/,/PATTERN-2/ you could only run operations between two search results for example :/<word1>/,/<word2>/s/<replace_this_word>/<replacement_word>/g, another example would be :/word1/,$s/

Text Objects

  • {operator}{a/i}{object}
  • d c y
  • d a w delete a word
  • d/c i w delete/change inner word
  • d i p delete a inner paragraph
  • c i [/(/< deletes everythin inside object, you can use a instead of i to delete object too
  • [ = b
  • ( = p
  • < = t

A usefull example is, for manipulating everything inside brackets:

  • d|v|y i <bracket>, where bracket can be (, [, {, etc...
  • d|v|y a <bracket> if you also want to delete the bracket

Macros

Normalize data, cleanup, change format, basically any repetable operation. Nothing more than a recorded sequence of keystrokes.

  • q <nammed register: record a macro, for example q a, to stop press q.
  • @ <nammed register>: apply macro
  • <lineNum>,<lineNum>normal @ <nammedregister> to apply to multiple lines

Best practices:

  • Noralize the cursor position with 0
  • Perform edits and operations
  • Position your cursor in next line enable easy replays, for example with j

Visual Mode

  • v characterwise visual mode

  • V linewise visual mode

  • Ctrl v blockwise visual mode

  • g v rapidly select previous selection

  • > >> indent

  • {motions} to highlight

  • {operations} affect highlighted text

  • If you want to highlight to both sides of the cursor, first move to the beginning of your selection with o. You can do the opposite with O.

  • You can use text objects to operate with highlights, for example v a p to highlight a paragraph

  • You can use insert yank change replace xdelete join u/U upper/lower case, </> shift left/right.

  • You can perform commands over selected area, for example, if you highlight an area in visual mode and press :, you can perform a substitution with s/word1/word2/g

  • Center text with :ce, you can also do :right and :left

Vim Customization/Settings

  • ~/.vimrc, you can make comments with ".
  • :h option-list check all options

Helpful options:

  • set history=1000: keep 1000 items in history
  • set ruler: show cursor position
  • set showcmd: show incomplete commands
  • set wildmenu: shows menu when using tab completion
  • set scrolloff=5: how many lines to keep above the cursor
  • set hlsearch: highligh search results
  • set incsearch: highlight incremental searchs
  • set ignorecase and set smartcase: ignore case if uppercase not in search term
  • set number: set column number
  • set ai: set auto indent
  • set si: smart indent
  • set bg=light/dark
  • color <schema>, you can install custom ones in ~/.vim/colors
  • map <F2> i<your_name>: create a macro for f2, you can do it with multiple keys
  • let mapleader="<key>": by default \
  • map <leader>w :w!: now you can use \ w

Buffers

  • Editing multiple files: vim file1 file2 or vim file-*
  • :buffers / :ls to see opened files
  • :buffer/b <num> to change between files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment