Update vim
This commit is contained in:
parent
9909f5dda0
commit
553ec5208b
48
vimscript.md
48
vimscript.md
|
@ -5,13 +5,13 @@ title: Vim script
|
||||||
Variables
|
Variables
|
||||||
---------
|
---------
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
let var = "hello"
|
let var = "hello"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Variable prefixes
|
### Variable prefixes
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
let g:ack_options = '-s -H' " g: global
|
let g:ack_options = '-s -H' " g: global
|
||||||
let s:ack_program = 'ack' " s: local to script
|
let s:ack_program = 'ack' " s: local to script
|
||||||
let l:foo = 'bar' " l: local to function
|
let l:foo = 'bar' " l: local to function
|
||||||
|
@ -30,7 +30,7 @@ keys(s:)
|
||||||
### Vim options
|
### Vim options
|
||||||
Prefix with `&`
|
Prefix with `&`
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
echo 'tabstop is ' . &tabstop
|
echo 'tabstop is ' . &tabstop
|
||||||
if &insertmode
|
if &insertmode
|
||||||
echo &g:option
|
echo &g:option
|
||||||
|
@ -39,7 +39,7 @@ echo &l:option
|
||||||
|
|
||||||
### Operation assignment
|
### Operation assignment
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
let var -= 2
|
let var -= 2
|
||||||
let var += 5
|
let var += 5
|
||||||
let var .= 'string' " concat
|
let var .= 'string' " concat
|
||||||
|
@ -47,7 +47,7 @@ let var .= 'string' " concat
|
||||||
|
|
||||||
### Strings
|
### Strings
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
let str = "String"
|
let str = "String"
|
||||||
let str = "String with \n newline"
|
let str = "String with \n newline"
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ let literal = 'literal, no \ escaping'
|
||||||
echo "result = " . re " concatenation
|
echo "result = " . re " concatenation
|
||||||
```
|
```
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
strlen(str) " length
|
strlen(str) " length
|
||||||
strchars(str) " character length
|
strchars(str) " character length
|
||||||
```
|
```
|
||||||
|
@ -64,7 +64,7 @@ strchars(str) " character length
|
||||||
Functions
|
Functions
|
||||||
---------
|
---------
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
" prefix with s: for local script-only functions
|
" prefix with s: for local script-only functions
|
||||||
function! s:Initialize(cmd, args)
|
function! s:Initialize(cmd, args)
|
||||||
" a: prefix for arguments
|
" a: prefix for arguments
|
||||||
|
@ -76,34 +76,34 @@ endfunction
|
||||||
|
|
||||||
### Namespacing
|
### Namespacing
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
function! myplugin#hello()
|
function! myplugin#hello()
|
||||||
```
|
```
|
||||||
|
|
||||||
### Calling functions
|
### Calling functions
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
call s:Initialize()
|
call s:Initialize()
|
||||||
call s:Initialize("hello")
|
call s:Initialize("hello")
|
||||||
```
|
```
|
||||||
|
|
||||||
### Consuming arguments
|
### Consuming arguments
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
echo "Result: " . s:Initialize()
|
echo "Result: " . s:Initialize()
|
||||||
```
|
```
|
||||||
|
|
||||||
### Abortable
|
### Abortable
|
||||||
Aborts when error is detected
|
Aborts when error is detected
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
function! myfunction() abort
|
function! myfunction() abort
|
||||||
endfunction
|
endfunction
|
||||||
```
|
```
|
||||||
|
|
||||||
### Dynamic arguments
|
### Dynamic arguments
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
function! infect(...)
|
function! infect(...)
|
||||||
echo a:0
|
echo a:0
|
||||||
echo a:1
|
echo a:1
|
||||||
|
@ -113,7 +113,7 @@ endfunction
|
||||||
Commands
|
Commands
|
||||||
--------
|
--------
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
command! Save set fo=want tw=80 nowrap
|
command! Save set fo=want tw=80 nowrap
|
||||||
command! Save call s:Foo()
|
command! Save call s:Foo()
|
||||||
```
|
```
|
||||||
|
@ -123,7 +123,7 @@ Flow
|
||||||
|
|
||||||
### Conditionals
|
### Conditionals
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
let char = getchar()
|
let char = getchar()
|
||||||
if char == "\<LeftMouse>"
|
if char == "\<LeftMouse>"
|
||||||
" ...
|
" ...
|
||||||
|
@ -136,13 +136,13 @@ endif
|
||||||
|
|
||||||
### Single line
|
### Single line
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
if empty(a:path) | return [] | endif
|
if empty(a:path) | return [] | endif
|
||||||
```
|
```
|
||||||
|
|
||||||
### Boolean logic
|
### Boolean logic
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
if g:use_dispatch && s:has_dispatch
|
if g:use_dispatch && s:has_dispatch
|
||||||
endif
|
endif
|
||||||
```
|
```
|
||||||
|
@ -150,7 +150,7 @@ endif
|
||||||
Lists
|
Lists
|
||||||
-----
|
-----
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
let mylist = [1, two, 3, "four"]
|
let mylist = [1, two, 3, "four"]
|
||||||
|
|
||||||
let first = mylist[0]
|
let first = mylist[0]
|
||||||
|
@ -163,7 +163,7 @@ let second = get(mylist, 1, "NONE")
|
||||||
|
|
||||||
### Functions
|
### Functions
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
len(mylist)
|
len(mylist)
|
||||||
empty(mylist)
|
empty(mylist)
|
||||||
|
|
||||||
|
@ -175,14 +175,14 @@ split('hello there world', ' ')
|
||||||
|
|
||||||
### Concatenation
|
### Concatenation
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
let longlist = mylist + [5, 6]
|
let longlist = mylist + [5, 6]
|
||||||
let mylist += [7, 8]
|
let mylist += [7, 8]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Sublists
|
### Sublists
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
let shortlist = mylist[2:-1]
|
let shortlist = mylist[2:-1]
|
||||||
let shortlist = mylist[2:] " same
|
let shortlist = mylist[2:] " same
|
||||||
|
|
||||||
|
@ -191,14 +191,14 @@ let shortlist = mylist[2:2] " one item
|
||||||
|
|
||||||
### Push
|
### Push
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
let alist = [1, 2, 3]
|
let alist = [1, 2, 3]
|
||||||
let alist = add(alist, 4)
|
let alist = add(alist, 4)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Map
|
### Map
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
call map(files, "bufname(v:val)") " use v:val for value
|
call map(files, "bufname(v:val)") " use v:val for value
|
||||||
call filter(files, 'v:val != ""')
|
call filter(files, 'v:val != ""')
|
||||||
```
|
```
|
||||||
|
@ -231,7 +231,7 @@ let extend(s:fruits, { ... })
|
||||||
Casting
|
Casting
|
||||||
-------
|
-------
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
str2float("2.3")
|
str2float("2.3")
|
||||||
str2nr("3")
|
str2nr("3")
|
||||||
float2nr("3.14")
|
float2nr("3.14")
|
||||||
|
@ -240,7 +240,7 @@ float2nr("3.14")
|
||||||
Math
|
Math
|
||||||
----
|
----
|
||||||
|
|
||||||
```vimscript
|
```vim
|
||||||
sqrt(100)
|
sqrt(100)
|
||||||
floor(3.5)
|
floor(3.5)
|
||||||
ceil(3.3)
|
ceil(3.3)
|
||||||
|
|
Loading…
Reference in New Issue