Blog

两个移动到行尾命令的区别

Content #

g_ 移动到当前行的最后一个非空字符上。 $ 移动到行尾,在visual mode下,会移动到最后一个字符的后面。

若光标在行首,使用"vg_“之后,再用"c"命令可修改当前行,但如果使用"v$“命令,再用"c"命令的话,当前行会被删除,下一行会顶上来,在输入结束后还得输入换行符。

execute与normal!

Content #

看下面的normal命令:

:normal! gg/a<cr>

执行后光标只会移到文件头部,并不会去搜索字符"a",原因是normal!命令无法识别像"<cr>“这样的特殊字符。

execute命令读取VimScript字符串后,会在执行前替换特殊字符。比如”\r"会被替换为"carriage return"。上面的命令就可以改写成:

:execute "normal! gg/a<cr>"

From #

Operator-Pending Mappings

Content #

An operator is a command that waits for you to enter a movement command, and then does something on the text between where you currently are and where the movement would take you. Some examples of operators are: d, y, c.

Movement Mappings #

:onoremap p i(

在代码的函数参数中按dp,可删除所有参数。按cp会删除后进入插入模式。

:onoremap b /return<cr>

在函数体代码中按db,可删除从当前位置开始直到return所在位置之间的所有代码。

定义新的operator-pending movement的步骤如下:

  1. Start at the cursor position.
  2. Enter visual mode(charwise).
  3. …mapping keys go here…
  4. All the text you want to include in the movement should now be selected.

Changing the Start #

:onoremap in( :<c-u>normal! f(vi(<cr>

“<c-u>”: remove the range that Vim may insert. “normal!”: simulate pressing keys in normal mode. “<cr>”: executes the :normal! command. 按下cin(,会删除当前行的小括号中的内容,并进入插入模式。

...

Autocommand Groups

Content #

Vim会为每个autocmd创建auto command,无法区分这些autocmd的功能是否为重复的。像下面的autocmd:

autocmd BufWrite * :sleep 200m

每次source后都会增加延迟。

不同的位置定义同名的autocommand group:

augroup testgroup
        autocmd BufWrite * :echom "Foo"
        autocmd BufWrite * :echom "Bar"
augroup END
...
augroup testgroup
        autocmd BufWrite * :echom "Baz"
augroup END

Vim会将多个autocmd自动组合在一起。如果要避免重复,可以在group中清除原先的group的内容。

augroup testgroup
    autocmd!
    autocmd BufWrite * :echom "Cats"
augroup END
:help autocmd-groups

From #

autocmd structure

Content #

autocmd一般结构如下:

:autocmd BufNewFile * :write

“BufNewFile” - the event to watch for “*” - a pattern to filter the event “:wrtie” - the command to run

结合buffer-local abbreviation和autocmd:

:autocmd FileType python :iabbrev <buffer> iff if:<left>
:autocmd FileType javascript :iabbrev <buffer> iff if ()<left>

查看所有autocmd事件:

:help autocmd-events

From #

Buffer-Local Mappings and Options

Content #

下面的映射只针对当前的buffer有效:

:nnoremap <buffer> <leader>x dd

有些option会对所有buffer有效,下面的option则可每个buffer单独设置。

:setlocal wrap
:setlocal nowrap
:setlocal number
:setlocal nonumber

有映射如下:

:nnoremap <buffer> Q x
:nnoremap          Q dd

按下Q时,若前一条规则有效,那么后一条规则会被掩盖掉。

From #

Keyword Characters

Content #

Vim will substitute an abbreviation when you type any “non-keyword character” after an abbreviation. “Non-keyword character” means any character not in the iskeyword option.

Run this command:

:set iskeyword?

You should see something like iskeyword=@,48-57,_,192-255. This format is very complicated, but in essence it means that all of the following are considered “keyword characters”:

• The underscore character (_). • All alphabetic ASCII characters, both upper and lower case, and their accented versions. • Any characters with an ASCII value between 48 and 57 (the digits zero through nine). • Any characters with an ASCII value between 192 and 255 (some special ASCII characters).

...

Abbreviations与Mappings的区别

Content #

考虑下面的映射:

:inoremap ssig -- <cr>Steve Losh<cr>steve@stevelosh.com

用户在输入:

Larry Lessig wrote the book ...

vim只要检测到ssig,就会做替换,而不管这四个这符出现的上下文环境。

修改如下:

:iunmap ssig
:iabbrev ssig -- <cr>Steve Losh<cr>steve@stevelosh.com

Vim就会去关注ssig前后的字符。

From #

sub:Vim

Content #

Nonrecursive Mapping Keyword Characters Abbreviations与Mappings的区别 Buffer-Local Mappings and Options autocmd structure Autocommand Groups Operator-Pending Mappings execute与normal! 两个移动到行尾命令的区别 gi与g; Variables and Options Coercion of Strings to Integers Explicit Case Sensitive or Insensitive Comparisons Function Varargs Always use normal! 脚本中使用<c-u>的含义 Magic and Literal Strings and Very Magic Paths in Vim Folding Theory visual模式下用gv命令恢复选择区域 Especially Useful Help Topics global命令

cook:Vim