Learn Vimscript the Hard Way
Echoing Message
- instead of
echo,echomwill save the output and let you run:messagesto view it later:echom "hello world":messages
fold example
Mapping
excersize
imap <c-u> <esc>gUiwA
nmap <c-u> gUiwStrict mapping
:nmap - dd
:nmap \ -- Vim will take other mapping : Pure evil
Recurlsion
:nmap dd O<esc>jddk
- it looks like clear the line but...
- It calls the new
dd(mapping to clear previous line) again and again and again...
Side effect
- Danger of recursing
- Mapping key of plugin
Nonrecursive mapping
- Each
*maphas*noremapnmap x dd nnoremap / x
When should you use these nonrecursive variants instead of their normal counterparts?
Always.
No, seriously, always.
Leaders
- What if we need the mapping key later?
- Vim doesn't use
H,L,-,<space>,<bs>,<cr>: Only six
Mapping key sequences
- Easy to map more then just single key
nnoremap -d dd nnoremap -c ddO - Vim had mechanism for this "prefix" key
- Vim called this prefix key the "leader"
Leader
:let mapleader = "-"- You can replace
-with any key you like - I personally like
, - Then you can use leader like below
nnoremap <leader>d dd- Why don't use
-directly instead of<leader>?- Can change leader later easily
- Share easily
- Many plugins create mappings based on leader
Local leader
is to be used for mappings which are local to a buffer
:let maplocalleader = "\\"Editing your VIMRC
The idea of this chapter is that you want to make it easier to make it easier to edit text.
:nnoremap <leader>ev :vsplit $MYVIMRC<cr>$MYVIMRC: special variable that points your vimrc file- Personally, I use global mark
Vso that I can open vimrc file by\V`
Sourcing mappings
- Once add config in
~/.vimrc, it doesn't immediately take effect - like
so %orsource ~/.vimrc - Let's make it easier
nnoremap <leader>sv :source $MYVIMRC<cr>Abbreviations
:abbrev adn and- Will automatically chagne adn to and
Keyword Characters
- Vim will substitute an abbreviation when you type any "non-keyword character" after an abbreviation
- "non-keyword character" means any character not in the
iskeywordoption.:set iskeyword? iskeyword=@,48-57,_,192-255 - it means all ASCII characters, digits, underscore, and so on
More Abbreviations
- Abbreviations are useful for more than just correcting typos
:iabbrev @@ steve@stevelosh.com
:iabbrev ccopy Copyright 2013 Steve Losh, all rights reserved.Why Not Use Mappings?
:inoremap ssig -- <cr>Steve Losh<cr>steve@stevelosh.com- if type
Larry Lessig wrote the book "Remix".after mapping, vim expandssiginto name - Mapping don't take into account what characters come before or after the map
:iunmap ssig
:iabbrev ssig -- <cr>Steve Losh<cr>steve@stevelosh.comMore Mappings
- Can mapping in sequence of multiple keys
:nnoremap jk dd
A more complicated mapping
:nnoremap <leader>" viw<esc>a"<esc>bi"<esc>lelExercises
Use
vnoremapto quote whatever inside visually selected:vnoremap <leader>" <esc>`>a"<esc>`<i"<esc>Map
Hin normal mode to go to the end of the current line.Originally,
His mapping to line from top of window.:help H:nnoremap H 0Map
Lin normal mode to go to the end of the current line.Originally,
His mapping to line from bottom of window.:help LMis mapping to middle of window.:nnoremap L $
Training your fingers
:inoremap jk <esc>- Many ways to exit insert mode by default
<esc>
<c-c>
<c-[>- Using
jkis great becasue the keys are right under two of your strongest fingers - What about if you write in a language where
jk? Change to different mapping (...)
Learning the Map
- To excersize new mapping above, you can force yourself to use it by disabling(!) the old key (lol)
:inoremap <esc> <nop><nop>means no operations