vim script programming
DESCRIPTION
TRANSCRIPT
Vim Script ProgrammingVim Script 程式設計
林佑安 (c9s)
Fork Me on Githubhttp://github.com/c9s
• Programmable interface
• vim script
• Perl / Python / Ruby / Mzscheme ...
Outline - Vim Script• Comment
• Variable
• Variable scope
• Special variable
• Function
• function scope
• built-in function
• Condition
• Looping
• Command
• Mapping
• Completion
• Autocommand
• Autogroup
• Utility
• vim-makefile
Prepare
Region Eval
http://gist.github.com/444528
* Press ‘V’ then select region* Press ‘e’ to evaluate region
To run script
:source [filename]
:source % # current file
Debugging
:messages
:set verbose=10 “ verbose mode
:verbose *command*
Help
section :help *section*
option :help ‘option_name’
mode :help [icv]_.....
command :help :command_name
Alias
• source => so :so
• function => func :func Foo(bar)
• endfunction => endf :endf
Variable
let variable = 123let string = “string”let list = [1,2,3]let strlist = [ “bar” , “foo” ]
let dict = { ‘phone’: ‘01234567’ }let dict.name = “Chopin”echo dict
Variable Scope
• non-prefix = global
• g: global
• s: script
• b: buffer
• v: vim built-in
• a: function arguments
Variable Scope
• let var = “string”
• let g:var_global = “global”
• let s:var_script = “script”
• let b:var_buffer = “buffer”
Condition
if boo > 2
elseif str == “string”
elseif str =~ ‘[a-z]+’
endif
Looping
while running == 1 echo “Run...”endwhile
for i in [1,2,3,4]
endfor
Looping
for i in range(1,10) “ i = 1 ~ 10
endfor
Function
fun! Test() .. blahendf
Uppercase
Override
Function
fun! Test() .. blahendf
Uppercase
Override
call Test() “ no return value
Function
fun! Test() .. blah return 3endf
Uppercase
Override
let ret = Test()
Return 3
Function arguments
fun! Foo(bar) echo a:barendf
fun! Foo(...) echo a:000 echo a:001endf
Function arguments
fun! Foo(bar) echo a:barendf
fun! Foo(...) echo a:000 echo a:001endf
non-modifiable
Function arguments
fun! Foo(bar) echo a:barendf
fun! Foo(...) echo a:000 echo a:001endf
count of args
non-modifiable
Function arguments
fun! Foo(bar) echo a:barendf
fun! Foo(...) echo a:000 echo a:001endf
count of args
non-modifiable
arg1
Function Scope
fun! s:Foo() endf
fun! g:Foo()endf
fun! b:Foo()endf
Script Scope
Global Scope
Buffer Scope
Built-in Functions
• List related: add() , remove() , filter() , map(), len() , sort(), empty(), join(), range() ...
• Dictionary related: keys() , values() , has_key() , has_value(), extend() ...
• String related: stridx(), strridx(), strlen(), substitute() , submatch(), strpart(), expand() ...
Built-in Functions
• List related: add() , remove() , filter() , map(), len() , sort(), empty(), join(), range() ...
• Dictionary related: keys() , values() , has_key() , has_value(), extend() ...
• String related: stridx(), strridx(), strlen(), substitute() , submatch(), strpart(), expand() ...
*function-list*
Built-in Function
• Buffer related: bufnr() , bufexists() , buflisted() , bufname() ... etc
• Window related: winbufnr(), bufwinnr(), winnr()
• Misc: getreg(), setreg(), system(), eventhandler() , getpid() ...
Autoload function
~/.vim/autoload/foo.vimfun! foo#blah()
endf
call foo#blah()In your script:
Define your library function:
Command
com! -range DoWrite :write
flags command name execute
Command
-range-range=%-range=N-count=N
Define line range for command:
Command
-bang-bar-register-buffer-complete=[type]
Other options:
Command
<line1>,<line2><count><bang><reg><args><f-args><q-args><lt>
Template arguments:
Syntax
• syn list
• syn keyword [ID] ....
• syn match [ID] ....
• syn region [ID] ...
• hi [ID] [Attributes]
Runtime Directory/Users/c9s/.vim/|-- after|-- autoload|-- colors|-- doc|-- ftdetect|-- ftplugin| |-- c| |-- cabal| |-- cg ....|-- indent|-- plugin|-- syntax| |-- c| |-- javascript| `-- perl
Utilities
• vim-makefile
• vim-uploader
• Vimana
Vim Makefile
• make install
• make uninstall
• make link
• make dist