Learn Vimscript The Hard Way 读书笔记
前言
一直想要学习vimL,这样可以加深对Vim的理解, Learn Vimscript The Hard Way 这本书本质上是一本vimscript的教程.
你可以在 https://github.com/blogData/VIM/vimscripts 这里找到我写的学习中产生的vimscript
书本要求我们
- you need to TYPE ALL THE COMMANDS
- you need to DO ALL THE EXERICESES
Prerequisties
- kown
buffer
,window
,normal mode
,insert mode
,text object
- create a vimrc file
- to easily find the location and name of the file on any operating system,use
echo $MYVIMRC
1 Echoing Message
1.2 how to print something
echo
,will not save messagesechom
,will save messagesmessages
,show history messages
echom
will save the output and let you run :messages
to view it later.
1.2 comments
" this is comment
1.3 exercises
Details
nnoremap <space> za
echo ">^.^<"
2
2 setting options
2.1 boolean options
set number
set nonumber
set number! " toggling boolean options
" how to know wath an option is currently set to by using a ?
set number?
2
3
4
5
all boolean options work this way :set <name>
turns the options on and :set no<name>
turns it off.
2.4 options with values
set number
" the numberwidth options changes how wide the column containing line numbers
" will be.
set numberwidth=10
set numberwidth=4
set numberwidth?
2
3
4
5
6
7
2.5 setting multiple options at once
set numberwidth=2
set nonumber
set number numberwidth=4
"both options were set and took effect in the last command
2
3
4
Details
nnoremap <space> za
"echo ">^.^<"
set number relativenumber numberwidth=2
set wrap
set shiftround "round indent to multiple of 'shiftwidth'
"set showmatch matchtime=10
"when bracket is inserted,briefly jump to the matchone.
"the jump is only done if the match can be seen on the screen.
2
3
4
5
6
7
8
9
10
11
3 Basic Mapping
map - x " - 映射成 x
map - dd
2
3.3 exercises
Details
map - ddp
map _ ddkP
2
- how to remove a mapping :
unmap [key]
4 modal mapping
- nmap,normal modal
- vmap,visual modal
- imap,insert modal
imap <c-d> <esc>ddi
5 strict mapping
nmap dd O<esc>jddk" recursive
- Open a line
- exit insert mode
- move down a line
- but
dd
is mapped,so perform the above mapping,never finish!
nmap x dd
nnoremap \ x
2
Always use nonrecursive map
6 leaders
pick a key as a prefix key
nnoremap -d dd
nnoremap -c ddO
let mapleader = "-"
nnoremap <leader>d dd
2
3
4
you can use <leader>
to mean "whatever i have my leader key set to"
local leader
only take effect for certain types of files.(for this buffer ?)
let maplocalleader = "\\"
nnoremap <buffer> <localleader>d dd
2
7 Editing Your Vimrc
Find a way to make it easier to add new mappings to vimrc files.
# split quickly to edit vimrc
nnoremap <leader>ev :vsplit $MYVIMRC<cr>
# source vimrc quickly
nnoremap <leader>sv :source $MYVIMRC<cr>
2
3
4
quickly write and quit in normal mode: ZZ
8 Abbreviations
Abbreviations are useful in day-to-day text editing.
I think abbrev are useful for code snippets.
9 More Mapping
nnoremap <leader>" viw<esc>a"<esc>hbi"<esc>lel
"disable the escape key in insert mode
2
3
10 Training Your Fingers
inoremap <esc> <nop>
exercises
inoremap <left> <nop>
nnoremap <left> <nop>
inoremap <right> <nop>
nnoremap <right> <nop>
2
3
4
11 Buffer-Local Options and Mapping
set map,Abbreviations,options in each buffer.
nnoremap <leader>d dd
nnoremap <buffer> <leader>x dd
2
THE <buffer>
in the second nnoremap command told vim to only consider that mapping when we're in the buffer where defined it.
we should use <localleader>
instead of <leader>
53 Autoloading
autoload/path/some.vim
e,autoload
-> delay loading
call somefile#Hello()
vim will look for a called autoload/somefile.vim