Skip to content

Instantly share code, notes, and snippets.

@jmcphers
Created May 12, 2016 16:26
Show Gist options
  • Save jmcphers/e17dd2762a2536df9533321129d60b50 to your computer and use it in GitHub Desktop.
Save jmcphers/e17dd2762a2536df9533321129d60b50 to your computer and use it in GitHub Desktop.

VIM FOR ADULTS

CHAPTER ONE: STOP TYPING

intro/stuff you should already know

what is vim?

  • based on very old editor (vi) designed in the days when connections were slow
  • installed on almost every *nix system, incl. os x
  • organic growth since to include hundreds of modern features
  • two big ideas?
    • modality: insert vs. command (contrast with emacs)
    • composibility: combine operators (e.g. delete, copy, change) with text objects (characters, lines, paragraphs, etc)
  • unix philosophy: it just edits text, it doesn't do anything else

will focus mainly on native vim stuff, not plugins--be productive on any system and in vi emulators

in-line navigation: wB, ^$, fF, ;, ct/cf

use set showcmd so you can see what the hell you're doing

let's change this if statement

if (var1 == 20.302) {

}

gj/gk etc are useful for very long lines

in-page navigation: H, M, L

in-file navigation: gg, G, ^f, ^b, :N, NG

common insertion patterns: I, A, o, O

if you're typing i or a you're probably doin it rong

common deletion patterns: cc, dd (esp. with counts)

try :set rnu

common selection patterns: v, V, ^V

more on this later

marks

drop them anywhere; I like "ma" before I go meandering about, then `a to go back try :marks to see them all don't forget about the amazing ``

registers

use them a lot--you get more than one. :reg shows them all. of special note is * which represents the system clipboard

don't reach for escape

use ^[

the dot operator: don't repeat yourself

The quick brown fox jumps over the lazy dog The quick blue hare jumps over the lazy ogre The quick orange hare jumps over the lazy liger

macros: record them carefully, play back with counts

var a = 1 var b = 2 var c = 3 var d = 4 var e = 5 var f = 6 var g = 7 var h = 8 var i = 9 var j = 10

nb. neovim appears to have implemented increment for characters

visual block mode

let's add a semicolon to the end of all these lines

var first = 1 var second = 2 var third = 3 var xy = 2

and then let's comment them out--this is about as close as we get to multiple cursors

gq: just as cool as it sounds

reformat/reflow text

  • this bulleted list is pretty messy
  • it's a problem in source code, especially if you've been changing text inside the comment and then you have to realign the comment markers
  • if this were a c++ source file you'd get comment wrappers

if you're really anal you can shell out to par which is not mode aware but is non-greedy and suffix-preserving

CHAPTER TWO: NAVIGATE YOUR CODE

ycm

the premier plugin for completing everything; supports navigate to declaration (note that this is not the same as definitions outside translation unit in C++)

ctags

:ta foo to navigate to tag foo ctrl + ] to jump, ctrl+t to go back g] to see all the possibilities, not just the best match this is a general mechanism (see the tag stack)

blocks

% to go to matching thing (even works for html!)

files

ctrl-p.vim--ctrl+p, leader+b

splits, c-w h, c-w l, c-j, c-k, etc.

c-w =

if in vanilla vim, can use :buf

some people like :tabs, but I don't--more than a few becomes cumbersome

matches

ag.vim

indent/unindent

and <<. combines well with counts & visual blocks

indentation/file type specific settings

set these by filetype, e.g.

au FileType javascript setlocal shiftwidth=2 tabstop=2 au FileType cpp setlocal shiftwidth=3 tabstop=3 au FileType r setlocal shiftwidth=2 tabstop=2 et au FileType ruby setlocal shiftwidth=2 tabstop=2 et

you can enable spell checking too

au FileType markdown setlocal spell spelllang=en_us au FileType gitcommit setlocal spell spelllang=en_us

project specific vimrc files are possible, as is autodetection! (I don't use this)

https://github.com/tpope/vim-sleuth

you can also use a mode line at the top of your file to tell vim to use specific indentation settings

CHAPTER THREE: BATCH OPERATIONS

filter stuff through a shell command

sort these things

pears bananas apples starfruit montana

note that % represents the current filename, e.g.: .!echo %

operate on matching lines

g/PATTERN/d - delete matching lines

g/PATTERN/m0 - move matching lines to top (good for inverse of above)

and of course there's :{range}s//

CHAPTER FOUR: WORKFLOW

use tmux! good for getting notified when stuff happens, splitting vim sxs w compiler, async output, etc.

syntastic/ycm

keep loop between screwing up and discovering you screwed up as tight as possible

dispatch.vim

or just :make (see makeprg)

then:

:cope, :cn, :cp (bind these)

swapfiles

set backupdir=/.vimbackup set directory=/.vimswap

config

use a dotfiles repo

CHAPTER FIVE: PLUGINS YOU NEED

  • you complete me
  • airline
  • fugitive: git support
  • dispatch
  • syntastic
  • ag

APPENDIX: STUPID TRICKS

center cursor on screen zz

increment/decrement numbers ctrl+a, ctrl+x

yank/paste from command line history q:

show man page for word under cursor: K setuid

toggle capitalization v, then ~ (also works with counts)

edit a remote file--see :help netrw :e scp://[email protected]//path/to/document

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