脚本宝典收集整理的这篇文章主要介绍了VIM Script /VIML 脚本语言入门,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
参考:Learn Vimscript the Hard Way
变量
"设置变量
let myVARiable = 1
let myString = 'Hello'
逻辑控制
参考:Comparisons - Learn Vimscript the Hard Way
if-else:
if 3 >= 1
echo 'True'
elseif 3 < 4
echo 'True again'
elseif 3 == 3
echo 'True true'
else
echo 'False'
endif
VIM中的字符串和数字是可以直接比较的,如:echo '3' >= 3
,返回1.
Functions 函数
参考:Functions - Learn Vimscript the Hard Way
函数名必须大写开头。
function MyFunction()
:wq
enDFunction
但是如果重载当前vimrc的话,会遇到function already exists
警告。
所以最好在将函数定义为可重写的函数,即变为function!
:
function! MyFunction()
:wq
endfunction
内置函数
has(..)
- has('程序语言')
- has('硬件架构')
- has('xxx')
System(...)
- system('rm /tmp/*')
buffer
- bufnr('%'): 返回当前VIM中的buffer数量
autocmd 事件触发器
参考非常棒的VIM官方文档(中文翻译):http://vimcdoc.sourceforge.ne...
格式为::au[tocmd] [group] {event} {pat} [nested] {cmd}
中文的话就是::au[tocmd] [组] {事件} {文件名规则} [nested] {命令}
如果命令比较复杂的话,建议创建function,然后在autocmd中call func()
。
常用技巧
获取当前buffer的文件名、路径、扩展名
参考:How can I see the full path of the current file?
参考:How do I get the name and extension of the current file?
都知道,VIM中%
代表当前buffer,我们可以增加filename-modifiers
来操作%
得到buffer关联的文件的相关信息。
Register %
返回当前文件的名字。所以我们才可以用!python %
这样的命令来运行当前脚本。
VIM中,%
还能做到很多的扩展:
:echo @% |" directory/name of file
:echo expand('%:p') |" full path "PATH"
:echo expand('%:p:h') |" directory containing file "HEAD"
:echo expand('%:t') |" full name of file "TAIL"
:echo expand('%:t:r') |" Only name of file "ROOT"
:echo expand('%:e') |" Only extension of file "EXTENSION"
我们在vimrc中使用的时候,可以省略echo和expand。比如:
nnoremap <C-g> :!CMD %:p:h<CR>
,这样可以在按Ctrl-g
时候,在当前文件所在的目录执行CMD
命令
“获取路径
echo expand('%:p') "/home/mool/vim/src/version.c
"获取文件全名
echo expand('%:t') "version.c
"获取文件名,不包括扩展名
echo expand('%:t:r') "version
"获取扩展名
echo expand('%:e') "c
Multiple lines 换行
以
开头,|
结尾,即可连接多行为一行。
au FiletyPE ruby
setlocal ts=2 |
setlocal sts=2 |
...
以上是脚本宝典为你收集整理的VIM Script /VIML 脚本语言入门全部内容,希望文章能够帮你解决VIM Script /VIML 脚本语言入门所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。