VIM Script /VIML 脚本语言入门

变量

"设置变量
let myVariable = 1
let myString = 'Hello'

逻辑控制

参考:Comparisons - Learn Vimscript the Hard Wayhtml

if-else:python

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.vim

Functions 函数

参考:Functions - Learn Vimscript the Hard Wayruby

函数名必须大写开头。架构

function MyFunction()
    :wq
endfunction

可是若是重载当前vimrc的话,会遇到function already exists警告。 因此最好在将函数定义为可重写的函数,即变为function!函数

```vim
function! MyFunction()
    :wq
endfunction

内置函数

has(..)

  • has('程序语言')
  • has('硬件架构')
  • has('xxx')

system(...)

  • system('rm /tmp/*')

buffer

  • bufnr('%'): 返回当前VIM中的buffer数量

autocmd 事件触发器

参考很是棒的VIM官方文档(中文翻译):http://vimcdoc.sourceforge.net/doc/autocmd.html.net

格式为::au[tocmd] [group] {event} {pat} [nested] {cmd} 中文的话就是::au[tocmd] [组] {事件} {文件名规则} [nested] {命令}翻译

若是命令比较复杂的话,建议建立function,而后在autocmd中call func()code

经常使用技巧

获取当前buffer的文件名、路径、扩展名

参考:How can I see the full path of the current file? 参考:How do I get the name and extension of the current file?htm

都知道,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 |
            \ ...
相关文章
相关标签/搜索