Part 1
" comments start with "
:echo "hello"
:echom "world"
" shows "world" but not "hello" -- echom = echo message
:messages
" get help
:help echo
:help echom
:help :messages
" boolean options
:set number
:set nonumber
" toggle with
:set number!
" query with
:set number?
" more numbers
:set numberwidth=10
:set numberwidth=6
:set numberwidth?
" more set
:set wrap?
:set shiftround?
:set matchtime?
" multiple
:set numberwidth=2 number
" more help
:help 'number'
:help relativenumber
:help numberwidth
:help wrap
:help shiftround
:help matchtime
" shiftround when set causes shifts to round to inter mults of shiftwidth
" in insert mode, Ct and Cd always round
Mappings
" pressing - does the same as pressing x
:map - x
" and similarly
:map - dd
:map <space> viw
:map <c-d> dd
:map <space> viw " Select word
:map - ddp
" there are also nmap vmap and imap
" for normal mode, visual mode and insert mode
" so
:imap <c-d> <esc>ddi " switch to normal mode, delete line and then switch back to insert mode
" then the fun begins
:imap <c-e> <c-d>
" map just turns keypresses into keypresses
" to unmap
:nunmap /
:vunmap -
:iunmap <c-d>
" if you don't want the results of a mapping to be mapped
:nmap x dd
:nnoremap \ x
" this doesn't get turned into x
" sequences
:noremap -c ahello<esc>
" this uses the 'leader', which is - by default
:let mapleader = "-"
:noremap <leader>d dd
" <leader> is whatever mapleader is set to
:let maplocalleader = "\\"
:help mapleader
:help maplocalleader
" example
:nnoremap <leader>ev :vsplit $MYVIMRC<cr>
" also we learn that
:vsplit filename " splits vertically and opens 'filename' in the new pane
:nnoreamp <leader>sv :source $MYVIMRC<cr>
Abbreviations
:iabbrev mr flibble
" then imr<esc> will result in flibble being input
" the abbrev takes affect when you press <space>
" or <cr> but not when you press a non-whitespace char.
:iabbrev teh the " poor man's spelling autocorrect
" vim abbrevs when you type a non-keyword char
;set iskeyword?
More mapping
:let mapleader = ","
:nnoremap <leader>x imrflibble<esc>
then
:let maplocalleader = "."
" changes the leader char on a per-buffer basis
" and on the subject of local
:setlocal wrap
:setlocal nowrap
:setlocal number
" and
:nnoremap <buffer> Q x
" will override
:nnoremap Q dd
" as it is more specific
Autocommands
:autocmd BufNewFile * :write
" Event pattern command
" pattern is a glob pattern like *.py, not a regex
:autocmd BufWritePre *.html :normal gg=G
:autocmd BufWritePre,BufRead *.html :normal gg=G
:autocmd BufNewFile,BufRead *.html setlocal nowrap
" insert comments
:autocmd FileType javascript nnoremap <buffer> <localleader>c I//<esc>
:autocmd FileType python nnoremap <buffer> <localleader>c I#<esc>
" I inserts at the start of the line, whereas i inserts
" at the cursor position.
:help autocmd-events
Buffer Local Abbrevs
:iabbrev <buffer> --- —
:autocmd FileType python :iabbrev <buffer> iff if:<left>
Autocommand groups
:augroup testgroup
: autocmd BufWrite * :echom "Baz"
:augroup END
" to clear
:augroup testgroup
: autocmd!
:augroup END
Operator-Pending Mappings
An operator command waits for you to enter a movement command, then does something with the text between where you are and where the movement would take you. e.g.
dw -- delete to next word
ci( -- change inside paren
yt, -- yank until comma
Also note ct, is how you do this, so you don't need c/re/,<enter>
To make mappings
:onoremap p i(
so then dp is equivalent to di(