VIM FOR ADULTS
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
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
if you're typing i or a you're probably doin it rong
try :set rnu
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 ``
use them a lot--you get more than one. :reg shows them all. of special note is * which represents the system clipboard
use ^[
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
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
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
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
the premier plugin for completing everything; supports navigate to declaration (note that this is not the same as definitions outside translation unit in C++)
: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)
% to go to matching thing (even works for html!)
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
ag.vim
and <<. combines well with counts & visual blocks
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
sort these things
pears bananas apples starfruit montana
note that % represents the current filename, e.g.: .!echo %
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//
use tmux! good for getting notified when stuff happens, splitting vim sxs w compiler, async output, etc.
keep loop between screwing up and discovering you screwed up as tight as possible
or just :make (see makeprg)
:cope, :cn, :cp (bind these)
set backupdir=/.vimbackup
set directory=/.vimswap
use a dotfiles repo
- you complete me
- airline
- fugitive: git support
- dispatch
- syntastic
- ag
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