# 《learn Vimscript The Hard Way》学习笔记

Posted by wsxq2 on 2019-02-01
TAGS:  VimVimscript

## 1 echo

• echo: 显示文本（在最下面）
• echom: 显示文本，并保存到:messages
• messages: 显示保存的信息
• ": 注释

### Exercises

Add a line to your ~/.vimrc file that displays a friendly ASCII-art cat (>^.^<) whenever you open Vim.

## 2 Options

### 2.1 Boolean Options

set <name>, set no<name>: 设置 Boolean 选项如set numberset nonumber set <name>!: 开启/关闭（Toggle） Boolean 选项。如set number! set <name>?: 检查选项值。如set number?

### 2.2 Options with Values

set <name>=<value>: 设置带值的选项。如set numberwidth=10

### 2.3 Setting Multiple Options at Once

1
:set number numberwidth=6


### Exercises

Read :help ‘number’ (notice the quotes).

Add a few lines to your ~/.vimrc file to set these options however you like.

## 3 map

map - dd: 将-映射为dd（即按下-会删除当前行） map <keyname> <keys>: 使用特殊键。如map <space> viwmap <c-d> dd map <space> viw " Select word: 这里的注释无效

### Exercises

Map the - key to “delete the current line, then paste it below the one we’re on now”. This will let you move lines downward in your file with one keystroke.

Add that mapping command to your ~/.vimrc file so you can use it any time you start Vim.

Figure out how to map the _ key to move the line up instead of down.

Try to guess how you might remove a mapping and reset a key to its normal function.

## 4 Modal Mapping

nmap(Normal MAP): 正常模式下的按键映射 vmap(Visual MAP): Visual模式下的按键映射。如vmap \ U imap(Insert MAP): 插入模式下的按键映射。如imap <c-d> <esc>ddi

### Exercises

Set up a mapping so that you can press to convert the current word to uppercase when you're in insert mode. Remember that U in visual mode will uppercase the selection. I find this mapping extremely useful when I'm writing out the name of a long constant like MAX_CONNECTIONS_ALLOWED. I type out the constant in lower case and then uppercase it with the mapping instead of holding shift the entire time.

Set up a mapping so that you can uppercase the current word with when in normal mode. This will be slightly different than the previous mapping because you don't need to enter normal mode. You should end up back in normal mode at the end instead of in insert mode as well.

## 5 Strict Mapping

### 5.1 Side Effects

*map的副作用：

• 存在递归风险
• 安装插件时插件按键映射时存在引用的风险。如：
1
2
:nmap - dd
:nmap \ -


### Exercises

Convert all the mappings you added to your ~/.vimrc file in the previous chapters to their nonrecursive counterparts.

### 6.1 <leader>

1
2


### 6.2 <localleader>

1
2


### Exercises

Convert all the mappings you added to your ~/.vimrc file in the previous chapters to be prefixed with so they don't shadow existing commands.

1
2
3
4
" Edit my Vimrc file
nnoremap <leader>ev :vsplit $MYVIMRC<cr> " Source my Vimrc file nnoremap <leader>sv :source$MYVIMRC<cr>


### Exercises

Add mappings to “edit my ~/.vimrc” and “source my ~/.vimrc” to your ~/.vimrc file.

Try them out a few times, adding dummy mappings each time.

## 8 Abbreviations

1
2
3
iabbrev waht what
iabbrev tehn then


### 8.1 Keyword Characters

1
set iskeyword?


### 8.2 More Abbreviations

1
2
iabbrev @@    wsxq2@qq.com


### Exercises

Add abbreviations for some common typos you know you personally make to your ~/.vimrc file. Be sure to use the mappings you created in the last chapter to open and source the file!