Update vimscript
This commit is contained in:
parent
e21f26a19c
commit
fc9a2f6748
364
vimscript.md
364
vimscript.md
|
@ -3,20 +3,276 @@ title: vimscript
|
|||
layout: default
|
||||
---
|
||||
|
||||
Variables
|
||||
---------
|
||||
|
||||
```vim
|
||||
let var = "hello"
|
||||
```
|
||||
|
||||
### Variable prefixes
|
||||
|
||||
```vim
|
||||
let g:ack_options = '-s -H' " g: global
|
||||
let s:ack_program = "ack" " s: local to script
|
||||
let l:foo = 'bar' " l: local to function
|
||||
let w:foo = 'bar' " w: window
|
||||
let b:state = 'on' " b: buffer
|
||||
let t:state = 'off' " t: tab
|
||||
echo v:var " v: vim special
|
||||
|
||||
let @/ = "" " @ register (this clears last search patterN)
|
||||
echo $PATH " $ env
|
||||
|
||||
" they're actually a dictionary
|
||||
keys(s:)
|
||||
```
|
||||
|
||||
### Vim options
|
||||
Prefix with `&`
|
||||
|
||||
```vim
|
||||
echo "tabstop is " . &tabstop
|
||||
if &insertmode
|
||||
echo &g:option
|
||||
echo &l:option
|
||||
```
|
||||
|
||||
### Operation assignment
|
||||
|
||||
```vim
|
||||
let var -= 2
|
||||
let var += 5
|
||||
let var .= "string" " concat
|
||||
```
|
||||
|
||||
### Strings
|
||||
|
||||
```vim
|
||||
let str = "String"
|
||||
let str = "String with \n newline"
|
||||
|
||||
let literal = 'literal, no \ escaping'
|
||||
|
||||
echo "result = " . re " concatenation
|
||||
```
|
||||
|
||||
```vim
|
||||
strlen(str) " length
|
||||
strchars(str) " character length
|
||||
```
|
||||
|
||||
Functions
|
||||
---------
|
||||
|
||||
```vim
|
||||
" prefix with s: for local script-only functions
|
||||
function! s:Initialize(cmd, args)
|
||||
" a: prefix for arguments
|
||||
echo "Command: " . a:cmd
|
||||
|
||||
return true
|
||||
endfunction
|
||||
```
|
||||
|
||||
### Calling functions
|
||||
|
||||
```vim
|
||||
call s:Initialize()
|
||||
call s:Initialize("hello")
|
||||
```
|
||||
|
||||
### Consuming arguments
|
||||
|
||||
```vim
|
||||
echo "Result: " . s:Initialize()
|
||||
```
|
||||
|
||||
Commands
|
||||
--------
|
||||
|
||||
```vim
|
||||
command! Save set fo=want tw=80 nowrap
|
||||
command! Save call s:Foo()
|
||||
```
|
||||
|
||||
Flow
|
||||
----
|
||||
|
||||
### Conditionals
|
||||
|
||||
```vim
|
||||
let char = getchar()
|
||||
if char == "\<LeftMouse>"
|
||||
" ...
|
||||
elseif char == "\<RightMouse>"
|
||||
" ...
|
||||
else
|
||||
" ...
|
||||
endif
|
||||
```
|
||||
|
||||
### Boolean logic
|
||||
|
||||
```vim
|
||||
if g:use_dispatch && s:has_dispatch
|
||||
endif
|
||||
```
|
||||
|
||||
Lists
|
||||
-----
|
||||
|
||||
```vim
|
||||
let mylist = [1, two, 3, "four"]
|
||||
|
||||
let first = mylist[0]
|
||||
let last = mylist[-1]
|
||||
|
||||
" Supresses errors
|
||||
let second = get(mylist, 1)
|
||||
let second = get(mylist, 1, "NONE")
|
||||
|
||||
len(mylist)
|
||||
empty(mylist)
|
||||
|
||||
sort(list)
|
||||
let sortedlist = sort(copy(list))
|
||||
|
||||
split('hello there world', ' ')
|
||||
```
|
||||
|
||||
### Concatenation
|
||||
|
||||
```vim
|
||||
let longlist = mylist + [5, 6]
|
||||
let mylist += [7, 8]
|
||||
```
|
||||
|
||||
### Sublists
|
||||
|
||||
```vim
|
||||
let shortlist = mylist[2:-1]
|
||||
let shortlist = mylist[2:] " same
|
||||
|
||||
let shortlist = mylist[2:2] " one item
|
||||
```
|
||||
|
||||
### Push
|
||||
|
||||
```vim
|
||||
let alist = [1, 2, 3]
|
||||
let alist = add(alist, 4)
|
||||
```
|
||||
|
||||
### Map
|
||||
|
||||
```vim
|
||||
call map(files, "bufname(v:val)") " use v:val for value
|
||||
call filter(files, 'v:val != ""')
|
||||
```
|
||||
|
||||
Dictionaries
|
||||
------------
|
||||
|
||||
```
|
||||
let colors = {
|
||||
\ "apple": "red",
|
||||
\ "banana": "yellow"
|
||||
}
|
||||
|
||||
echo colors["a"]
|
||||
echo get(colors, "apple") " supress error
|
||||
|
||||
remove(colors, "apple")
|
||||
|
||||
keys(dict)
|
||||
len(dict)
|
||||
```
|
||||
|
||||
### Extending
|
||||
|
||||
```
|
||||
" Extending with more
|
||||
let extend(s:fruits, { ... })
|
||||
```
|
||||
|
||||
Casting
|
||||
-------
|
||||
|
||||
```vim
|
||||
str2float("2.3")
|
||||
str2nr("3")
|
||||
float2nr("3.14")
|
||||
```
|
||||
|
||||
Math
|
||||
----
|
||||
|
||||
```vim
|
||||
sqrt(100)
|
||||
floor(3.5)
|
||||
ceil(3.3)
|
||||
abs(-3.4)
|
||||
|
||||
sin() cos() tan()
|
||||
sinh() cosh() tanh()
|
||||
asin() acos() atan()
|
||||
```
|
||||
|
||||
Vim-isms
|
||||
--------
|
||||
|
||||
### Execute a command
|
||||
Runs an ex command you typically run with `:`
|
||||
|
||||
execute "vsplit"
|
||||
execute "e " . fnameescape(filename)
|
||||
|
||||
### Echo
|
||||
|
||||
```
|
||||
echohl WarningMsg | echomsg "=> " . a:msg | echohl None
|
||||
```
|
||||
|
||||
### Propmts
|
||||
|
||||
```
|
||||
let result = confirm("Sure?")
|
||||
execute "confirm q"
|
||||
```
|
||||
|
||||
### Built-ins
|
||||
|
||||
```
|
||||
has("feature") " :h feature-list
|
||||
executable("python")
|
||||
globpath(&rtp, "syntax/c.vim")
|
||||
|
||||
exists("$ENV")
|
||||
exists(":command")
|
||||
exists("variable")
|
||||
exists("+option")
|
||||
exists("g:...")
|
||||
```
|
||||
|
||||
Mapping
|
||||
-------
|
||||
|
||||
nnoremap
|
||||
vmap
|
||||
...
|
||||
```
|
||||
nnoremap
|
||||
vmap
|
||||
...
|
||||
```
|
||||
|
||||
Components:
|
||||
|
||||
[nvixso](nore)map
|
||||
^ ^
|
||||
| don't recurse
|
||||
|
|
||||
normal, visual, insert, eX mode, select, operator-pending
|
||||
```
|
||||
[nvixso](nore)map
|
||||
^ ^
|
||||
| don't recurse
|
||||
|
|
||||
normal, visual, insert, eX mode, select, operator-pending
|
||||
```
|
||||
|
||||
Arguments:
|
||||
|
||||
|
@ -24,69 +280,73 @@ Arguments:
|
|||
- `<silent>` - no echo
|
||||
- `<nowait>`
|
||||
|
||||
Stuff
|
||||
-----
|
||||
|
||||
let var = "hello"
|
||||
echo "var = " . var
|
||||
|
||||
Functions
|
||||
---------
|
||||
|
||||
has("feature") " :h feature-list
|
||||
executable("python")
|
||||
globpath(&rtp, "syntax/c.vim")
|
||||
|
||||
if getchar() == "\<LeftMouse>"
|
||||
endif
|
||||
|
||||
exe "vsplit"
|
||||
|
||||
Syntax
|
||||
------
|
||||
|
||||
### Highlights
|
||||
|
||||
hi Comment
|
||||
term=bold,underline
|
||||
gui=bold
|
||||
ctermfg=4
|
||||
guifg=#80a0ff
|
||||
```
|
||||
hi Comment
|
||||
term=bold,underline
|
||||
gui=bold
|
||||
ctermfg=4
|
||||
guifg=#80a0ff
|
||||
```
|
||||
|
||||
### Filetype detection
|
||||
|
||||
augroup filetypedetect
|
||||
au! BufNewFile,BufRead *.json setf javascript
|
||||
augroup END
|
||||
```
|
||||
augroup filetypedetect
|
||||
au! BufNewFile,BufRead *.json setf javascript
|
||||
augroup END
|
||||
|
||||
au Filetype markdown setlocal spell
|
||||
au Filetype markdown setlocal spell
|
||||
```
|
||||
|
||||
### Conceal
|
||||
|
||||
set conceallevel=2
|
||||
syn match newLine "<br>" conceal cchar=}
|
||||
hi newLine guifg=green
|
||||
```
|
||||
set conceallevel=2
|
||||
syn match newLine "<br>" conceal cchar=}
|
||||
hi newLine guifg=green
|
||||
```
|
||||
|
||||
### Region conceal
|
||||
|
||||
syn region inBold concealends matchgroup=bTag start="<b>" end="</b>"
|
||||
hi inBold gui=bold
|
||||
hi bTag guifg=blue
|
||||
```
|
||||
syn region inBold concealends matchgroup=bTag start="<b>" end="</b>"
|
||||
hi inBold gui=bold
|
||||
hi bTag guifg=blue
|
||||
```
|
||||
|
||||
### Syntax
|
||||
|
||||
syn match :name ":regex" :flags
|
||||
```
|
||||
syn match :name ":regex" :flags
|
||||
|
||||
syn region Comment start="/\*" end="\*/"
|
||||
syn region String start=+"+ end=+"+ skip=+\\"+
|
||||
syn region Comment start="/\*" end="\*/"
|
||||
syn region String start=+"+ end=+"+ skip=+\\"+
|
||||
|
||||
syn cluster :name contains=:n1,:n2,:n3...
|
||||
syn cluster :name contains=:n1,:n2,:n3...
|
||||
|
||||
flags:
|
||||
keepend
|
||||
oneline
|
||||
nextgroup=
|
||||
contains=
|
||||
contained
|
||||
flags:
|
||||
keepend
|
||||
oneline
|
||||
nextgroup=
|
||||
contains=
|
||||
contained
|
||||
|
||||
hi def link markdownH1 htmlH1
|
||||
hi def link markdownH1 htmlH1
|
||||
```
|
||||
|
||||
### Include guards
|
||||
|
||||
```
|
||||
if exists('g:loaded_myplugin')
|
||||
finish
|
||||
endif
|
||||
|
||||
" ...
|
||||
|
||||
let g:loaded_myplugin = 1
|
||||
```
|
||||
|
|
Loading…
Reference in New Issue