Skip to content

Learn Vimscript The Hard Way 读书笔记

前言

一直想要学习vimL,这样可以加深对Vim的理解, Learn Vimscript The Hard Way 这本书本质上是一本vimscript的教程.

你可以在 https://github.com/blogData/VIM/vimscripts 这里找到我写的学习中产生的vimscript

书本要求我们

  1. you need to TYPE ALL THE COMMANDS
  2. you need to DO ALL THE EXERICESES

Prerequisties

  1. kown buffer,window,normal mode,insert mode,text object
  2. create a vimrc file
  • to easily find the location and name of the file on any operating system,useecho $MYVIMRC

1 Echoing Message

1.2 how to print something

  • echo ,will not save messages
  • echom,will save messages
  • messages,show history messages

echomwill save the output and let you run :messages to view it later.

1.2 comments

plaintext
" this is comment
1

1.3 exercises

Details
nnoremap <space> za
echo ">^.^<"
1
2

2 setting options

2.1 boolean options

plaintext
set number
set nonumber
set number! " toggling boolean options
" how to know wath an option is currently set to by using a ?
set number?
1
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

plaintext
set number

" the numberwidth options changes how wide the column containing line numbers 
" will be.
set numberwidth=10
set numberwidth=4
set numberwidth?
1
2
3
4
5
6
7

2.5 setting multiple options at once

plaintext
set numberwidth=2
set nonumber
set number numberwidth=4
"both options were set and took effect in the last command
1
2
3
4
Details
plaintext
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.
1
2
3
4
5
6
7
8
9
10
11

3 Basic Mapping

plaintext
map - x  " - 映射成 x
map - dd
1
2

3.3 exercises

Details
plaintext
map - ddp
map _ ddkP
1
2
  • how to remove a mapping :unmap [key]

4 modal mapping

  • nmap,normal modal
  • vmap,visual modal
  • imap,insert modal
plaintext
imap <c-d> <esc>ddi
1

5 strict mapping

plaintext
nmap dd O<esc>jddk" recursive
1
  • Open a line
  • exit insert mode
  • move down a line
  • but dd is mapped,so perform the above mapping,never finish!
plaintext
nmap x dd
nnoremap \ x
1
2

Always use nonrecursive map

6 leaders

pick a key as a prefix key

plaintext
nnoremap -d dd
nnoremap -c ddO
let mapleader = "-"
nnoremap <leader>d dd
1
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 ?)

plaintext
let maplocalleader = "\\"
nnoremap <buffer> <localleader>d dd
1
2

7 Editing Your Vimrc

Find a way to make it easier to add new mappings to vimrc files.

viml
# split quickly to edit vimrc
nnoremap <leader>ev :vsplit $MYVIMRC<cr>
# source vimrc quickly
nnoremap <leader>sv :source $MYVIMRC<cr>
1
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

viml
nnoremap <leader>" viw<esc>a"<esc>hbi"<esc>lel

"disable the escape key in insert mode
1
2
3

10 Training Your Fingers

viml
inoremap <esc> <nop>
1

exercises

viml
inoremap <left> <nop>
nnoremap <left> <nop>
inoremap <right> <nop>
nnoremap <right> <nop>
1
2
3
4

11 Buffer-Local Options and Mapping

set map,Abbreviations,options in each buffer.

viml
nnoremap          <leader>d dd
nnoremap <buffer> <leader>x dd
1
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

vimL
call somefile#Hello()
1

vim will look for a called autoload/somefile.vim