vim script programming

36

Click here to load reader

Upload: yo-an-lin

Post on 15-Jan-2015

12.302 views

Category:

Technology


5 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Vim Script Programming

Vim Script ProgrammingVim Script 程式設計

林佑安 (c9s)

Fork Me on Githubhttp://github.com/c9s

Page 2: Vim Script Programming

• Programmable interface

• vim script

• Perl / Python / Ruby / Mzscheme ...

Page 3: Vim Script Programming

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

Page 4: Vim Script Programming

Prepare

Page 5: Vim Script Programming

Region Eval

http://gist.github.com/444528

* Press ‘V’ then select region* Press ‘e’ to evaluate region

Page 6: Vim Script Programming

To run script

:source [filename]

:source % # current file

Page 7: Vim Script Programming

Debugging

:messages

:set verbose=10 “ verbose mode

:verbose *command*

Page 8: Vim Script Programming

Help

section :help *section*

option :help ‘option_name’

mode :help [icv]_.....

command :help :command_name

Page 9: Vim Script Programming

Alias

• source => so :so

• function => func :func Foo(bar)

• endfunction => endf :endf

Page 10: Vim Script Programming

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

Page 11: Vim Script Programming

Variable Scope

• non-prefix = global

• g: global

• s: script

• b: buffer

• v: vim built-in

• a: function arguments

Page 12: Vim Script Programming

Variable Scope

• let var = “string”

• let g:var_global = “global”

• let s:var_script = “script”

• let b:var_buffer = “buffer”

Page 13: Vim Script Programming

Condition

if boo > 2

elseif str == “string”

elseif str =~ ‘[a-z]+’

endif

Page 14: Vim Script Programming

Looping

while running == 1 echo “Run...”endwhile

for i in [1,2,3,4]

endfor

Page 15: Vim Script Programming

Looping

for i in range(1,10) “ i = 1 ~ 10

endfor

Page 16: Vim Script Programming

Function

fun! Test() .. blahendf

Uppercase

Override

Page 17: Vim Script Programming

Function

fun! Test() .. blahendf

Uppercase

Override

call Test() “ no return value

Page 18: Vim Script Programming

Function

fun! Test() .. blah return 3endf

Uppercase

Override

let ret = Test()

Return 3

Page 19: Vim Script Programming

Function arguments

fun! Foo(bar) echo a:barendf

fun! Foo(...) echo a:000 echo a:001endf

Page 20: Vim Script Programming

Function arguments

fun! Foo(bar) echo a:barendf

fun! Foo(...) echo a:000 echo a:001endf

non-modifiable

Page 21: Vim Script Programming

Function arguments

fun! Foo(bar) echo a:barendf

fun! Foo(...) echo a:000 echo a:001endf

count of args

non-modifiable

Page 22: Vim Script Programming

Function arguments

fun! Foo(bar) echo a:barendf

fun! Foo(...) echo a:000 echo a:001endf

count of args

non-modifiable

arg1

Page 23: Vim Script Programming

Function Scope

fun! s:Foo() endf

fun! g:Foo()endf

fun! b:Foo()endf

Script Scope

Global Scope

Buffer Scope

Page 24: Vim Script Programming

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() ...

Page 25: Vim Script Programming

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*

Page 26: Vim Script Programming

Built-in Function

• Buffer related: bufnr() , bufexists() , buflisted() , bufname() ... etc

• Window related: winbufnr(), bufwinnr(), winnr()

• Misc: getreg(), setreg(), system(), eventhandler() , getpid() ...

Page 27: Vim Script Programming

Autoload function

~/.vim/autoload/foo.vimfun! foo#blah()

endf

call foo#blah()In your script:

Define your library function:

Page 28: Vim Script Programming

Command

com! -range DoWrite :write

flags command name execute

Page 29: Vim Script Programming

Command

-range-range=%-range=N-count=N

Define line range for command:

Page 30: Vim Script Programming

Command

-bang-bar-register-buffer-complete=[type]

Other options:

Page 31: Vim Script Programming

Command

<line1>,<line2><count><bang><reg><args><f-args><q-args><lt>

Template arguments:

Page 32: Vim Script Programming

Syntax

• syn list

• syn keyword [ID] ....

• syn match [ID] ....

• syn region [ID] ...

• hi [ID] [Attributes]

Page 33: Vim Script Programming

Runtime Directory/Users/c9s/.vim/|-- after|-- autoload|-- colors|-- doc|-- ftdetect|-- ftplugin| |-- c| |-- cabal| |-- cg ....|-- indent|-- plugin|-- syntax| |-- c| |-- javascript| `-- perl

Page 34: Vim Script Programming

Utilities

• vim-makefile

• vim-uploader

• Vimana

Page 35: Vim Script Programming

Vim Makefile

• make install

• make uninstall

• make link

• make dist

Page 36: Vim Script Programming

Thankshttp://github.com/c9s