Skip to content

Instantly share code, notes, and snippets.

@matthutchinson
Created February 26, 2020 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthutchinson/299512149c347e7cdd5f41265f651b58 to your computer and use it in GitHub Desktop.
Save matthutchinson/299512149c347e7cdd5f41265f651b58 to your computer and use it in GitHub Desktop.

Vim Help

Forgotten shortcuts and mappings

Insert Mode

Ctrl+w Delete word backwards Ctrl+n Ctrl+p Autocomplete word from next found forward or backward in file CTRL+xl Autocomplete entire line (must start with same chars) CTRL+xl Autocomplete to a local file path Ctrl+d Ctrl+t Tab and remove tab width Ctrl+r % Insert current filename " Insert yanked paste bufferr + Insert OSX paste buffer

Type Ctrl+v,Ctrl+a to insert the literal character Ctrl+a

Expression Register

:help i_CTRL-R=

For example to map Ctrl+jd (in insert mode) to run a script (by setting the expression register) use:

:imap d =system('/Users/matt/Desktop/ruby.rb')

Movement

; Repeat previous movement w, W Start of word forwards (consider w-space) b, B Start of word backwards (consider w-space) e, E End of word forwards ge, gE End of word backwards f /, F / First occurrences of / forwards t /, T / First occurrences of / (1 char before) forwards H, M, L Jump to head, middle, last 34G Jump to line 34 / Start search forward ? Start search backward Ctrl+g Switch to SELECT mode when in VISUAL mode

Editing

J Join all lines highlighted gv Highlight last thing that was highlighted i, I Insert mode, at position or start of line a, A Insert mode 1 char after position or end of line (append) x, X Delete under cursor, or 1 char before r, R, 3rh Replace char under cursor or enter Replace mode, or replace next 3 char with h

Marks

:marks List marks m{char} Mark position with a char '{char} Jump to mark (start of line) `{char} Jump to mark (actual col)

Buffers

:ls Show all loaded buffers :b {number or name} Jump to buffer by number or name :b#, Ctrl + ^ Jump toggle between buffers :bd, :3,4bd, :%bd, :bd {number} Delete a buffer, range, current, number :bufdo %s/the/bananas/gc Run same command in all buffers :args Show args list :n Next in args :wn Write next in args :rewind Move back to start in first file in args :argdo %s/the/bananas/gc Run command in all args (see below)

Windows

Ctrl + wo Close all windows except this one (zoom) Ctrl + wc Close a window Ctrl + wx Switch position of 2 windows (keep context) Ctrl + wp Jump toggle between 2 windows Ctrl + ws New window hoirzontal split Ctrl + wv New window vertical split Ctrl + ww Cycle windows

Help

:helpgrep Grep search help :cwin Show quickfix window :cn, :cp Next, previous in quickfix list :cclose Close quickfix window Ctrl + ] Jump to tag Ctrl + t Jump back to previous tag

Run same commands on multiple lines

Visually select the lines, the use e.g. to add ; at the end of each line do

:normal A;
# or to do this on the entire file run
:%normal A;.

Or to add 'xxx yyy' at the start of every line, visually select then run

:normal 0ixxx yyy

Or to do the last command/edit on a bunch on lines, do a command on a line, then visually select other lines and run

:normal .

Search and replace in multiple files

# get files to operate on
:args `ag -l ActiveRecord::Base`
# or load a bunch of files matching a name
:args **/.travis.yml
# check the list
:arg
:argdo %s/ActiveRecord::Base/ApplicationRecord/gec | update
# or to see if that had errors
:argdo %s/ActiveRecord::Base/ApplicationRecord/g | update

More here

Write file and a fire command into a Tmux window

:map ,tt :w\|:Tmux xcodebuild -scheme videosnap build && /u/code/videosnap/build/products/Debug/videosnap -v<cr>

error: multiple input filenames provided

Or just write file, compile and run it (no Tmux)

:map ,tt :w\|:!gcc % -o ./string_reverser; ./string_reverser<CR>

Snippets (Ultisnips)

  • U to open snippets for the FileType (UltiSnipEdit)
  • Create a snip with snip
  • Move through tabstops with (forward) and (back)
  • Snips are stored in ~/.vim/Ultisnips

More here

Spell Check

  • to start a spell check, move with ]s or [s
  • Use CTRL-N or CTRL-P (in insert mode) to autocomplete word
  • Add a new word to the dictionary quickly with zg

PNG diagrams with dot

Use dig and imgcat (iTerm only) - e.g

digraph {
  normal -> insert [label="i"]
  insert -> normal [label="ESC"]
  normal -> visual [label="V"]
  visual -> normal [label="ESC"]
}

:!dot -Tpng % | imgcat

Macros

Tips from here

qa start recording a macro to a @a play the macro 6@aa play back the macro 6 times "ap paste the a macro to the screen "aY yank the macro back to a buffer

  • use :wn before closing macro recording, to write and move to next file
  • to apply 'q' macro globally: :g[lobal]/{reqex to match on lines}/normal @q
  • paste the 'q' macro (e.g. in vimrc and then use to map to something): "qp let @i = '{"qp}' quote whatever was pasted from the macro noremap ri :g[lobal]/{regex to match}/normal @i:w
  • edit existing 'q' macro visually :let @q='q'
  • apply to all 100 open files: 100@q
  • apply to all open files, writing and moving to next: (end macro with :wn)
  • apply to all recursively: (end 'q' macro with @q)

Global

:g/itemnum/normal 20Ctl+v,Ctrl+a

global / look for lines with itemnum / normal mode / run the 20Ctrl+A command Type Ctrl+v,Ctrl+a to insert the literal character Ctrl+a

:v/itemmnum/normal {cmd}

:v is the inverse of the global command, matching lines that do not contain itemnum

Redir history and building commands from functions

redirect output from a command to register a

:redir @a

run commmand e.g. show last 20 history commands

:history : -20,

Stop further redirection

:redir END

Now in normal mode, paste the history from register a with:

"ap

Build a function around the history of commands like so:

function! MyFunction() silent! .. silent! .. endfunction

command! MyCommand call MyFunction()

Use silent! in front of history commands to silence things, and add normal in front of commands that come after switching to insert mode. Then source this file and try out the command!

:so % :MyCommand

Copy lines and manipulate

Copy all lines beginning with # (will paste below)

:g/^#/t.

Copy all lines beginning with # and substitute all chars on new line with =

:g/^#/t.|s/./=/g

Match all lines beginning with ** and do something with them (e.g. insert a new line above)

:g/**/normal O # insert a line above :g/**/normal yyp # copy lines and paste below :g/**/d # delete these lines

Regex capturing

Substitute ** (indented bullets) while maintaining spaces before them:

:%s/^(\s*)**/\1++/g

Surround regex with ( ) to capture and use \1, \2 etc. in substitution.

OR use \zs

:%s/^\s*\zs**/\1++/g

\zs tells regex to only start substituting from this point forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment