vimscript 自查手册

摘自参考


变量命名空间

                     (无) 函数内: 局部于函数;
                     (无) 老式脚本里: 全局;
                     (无)   Vim9  脚本里: 局部于脚本
 buffer-variable     b:   局部于当前缓冲区。
 window-variable     w:   局部于当前窗口。
 tabpage-variable    t:   局部于当前标签页。
 global-variable     g:   全局。
 local-variable      l:   局部于函数 (只限于老式函数内使用)。
 script-variable     s:   局部于  :source  的 Vim 脚本。
 function-argument   a:   函数参数 (只限于老式函数内使用)。
 vim-variable        v:   Vim 预定义的全局变量。

map的三种模式

  • normal
  • visual
  • insert

所有的map命令都有对应的noremap命令来忽视递归的映射,它们分别是:nnoremap,vnoremap和inoremap。

AutoCommand

:autocmd BufNewFile * :write
         ^          ^ ^
         |          | |
         |          | The command to run.
         |          |
         |          A "pattern" to filter the event.
         |
         The "event" to watch for.

The first piece of the command is the type of event we want to watch for. Vim offers many events to watch. Some of them include:

Starting to edit a file that doesn't already exist.
Reading a file, whether it exists or not.
Switching a buffer's filetype setting.
Not pressing a key on your keyboard for a certain amount of time.
Entering insert mode.
Exiting insert mode.

augroup

autocmd 的组形式
autocmd! 表示清除组

<buffer>

把作用域限制在本文件

setlocal

把作用域限制在本文件

Operator-Pending Mappings

:onoremap p i(

这个章节我们继续来探索vim映射的神奇功能:“操作符-区间 映射”,让我们先来弄懂这个名词的意义,然后在使用他。

一个操作符是一个等待你来输入一个移动命令的命令,然后对你当前位置到移动后的位置之间的文本做操作。

常见的操作符有d,y,c。

General Rules

A good way to keep the multiple ways of creating operator-pending mappings straight is to remember the following two rules:

If your operator-pending mapping ends with some text visually selected, Vim will operate on that text.
Otherwise, Vim will operate on the text between the original cursor position and the new position.

:normal!

表示在normal模式下执行按键,normal! 取消映射,并且不分析特殊字符序列。

Special Characters

If you play around with normal! long enough you'll probably notice a problem. Try the following command:

:normal! /foo<cr>

乍一看,这似乎应该执行foo搜索,但你会发现它不起作用。问题很正常!不解析特殊字符序列,如 .
In this case Vim thinks you wanted to search for the character sequence "f, o, o, left angle bracket, c, r, right angle bracket", and doesn't realize that you even pressed return to perform the search!
我们需要配合execute来修复这个问题。

:execute "normal! /foo\<cr>"

编写Vim脚本时,应始终使用normal!,不要使用普通的normal.

Options as Variables

We can also set options as variables using the let command. Run the following commands:

:let &textwidth = 100
:set textwidth?

Vim will display textwidth=100.

Registers as Variables

You can also read and set registers as variables. Run the following command:

:let @a = "hello!"

分割符号 |

:echom "foo" | echom "bar"

vim会把上面的命令看做是两行。用:mesages来看看日志里是否有两行信息。

在本书后面的章节里,如果你想手动输入命令,但是不想输入换行和分号,那么你可以把它们放在同一行里,只需要用|分割就可以了。

比较符号

==? 字符串大小写不敏感比较
==# 字符串大小写敏感比较

函数

  • 函数名要以大写字母开头(潜规则)
  • 定义函数
     :funciont Meow()
     :     echom "Meow!"
     :endfunction
  • 调用函数
:call Meow()
  • 隐式返回值
    函数没有返回值则返回0

  • 可变参数

:function Varg(...)
:  echom a:0
:  echom a:1
:  echo a:000
:endfunction

:call Varg("a", "b")

a:0 表示可变输入的参数数量
a:1 为第一个可变输入的参数
a:000 表示可变输入参数的列表

  • 赋值
:function Assign(foo)
"  let a:foo = "Nope"   " 错误,不能给形参赋值
:  echom a:foo
:  let foo_tmp = a:foo  " 正确
:  let foo_tmp = "Yep"
:endfunction

:call Assign("test")

浮点格式

:echo 5.45e+3

Vim displays 5450.0.

字符串拼接

Here's the issue: Vim's + operator is only for Numbers. When you pass a string to + Vim will try to coerce it to a Number before performing the addition. Run the following command:

:echom "3 mice" + "2 cats"

This time Vim displays 5, because the strings are coerced to the numbers 3 and 2 respectively.

+只能用来拼接整数,会把字符串传换成整数进行拼接。Vim允许你在进行加法操作时,把一个字符串当作浮点数来操作,但是在进行字符串拼接时却不允许把浮点数当作字符串来使用。拼接字符串需要使用.符号。例如:

:echom "Hello, " .  "world"

这次Vim会输出“Hello, world”。

不转义的字符串

:echom '\n\\'

Vim会输出“\n\”。用单引号表示不对字符串进行转移,除了两个单引号会表示一个单引号这个例外。

:echom 'That''s enough.'

Vim会输出“That's enough”,在不转义字符串里,单引号是惟一一个例外的字符。

正则表达式

  • Very Magic

您可能会想知道Vimscript的四种不同的正则表达式解析模式,以及它们与您使用的Python、Perl或Ruby等语言的正则表达式有何不同。如果你真的想看,你可以阅读他们的文档,但如果你想要理智、简单的解决方案,那就继续阅读。
Run the following command:

:execute "normal! gg" . '/\vfor .+ in .+:' . "\<cr>"

我们把匹配模式从命令的其余部分分离出来,并用单引号让其不进行转义。在匹配模式中我们以\v开头,这告诉Vim使用其“very magic”的正则表达式解析模式,这与您在任何其他编程语言中所使用的几乎相同。
如果您只需使用\v启动所有正则表达式,就不必担心Vimscript的其他三种疯狂正则表达式模式。

<cword>

<cword> is a special bit of text you can use in Vim's command-line mode, and Vim will replace it with "the word under the cursor" before running the command.

参考

https://kenvifire.gitbooks.io/vimscript/content/1.html
https://learnvimscriptthehardway.stevelosh.com/chapters/1.html

posted @   3yude  阅读(42)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示