Update vimscript
This commit is contained in:
parent
24a84aa7ed
commit
e5d364cc7e
|
@ -0,0 +1,88 @@
|
||||||
|
---
|
||||||
|
title: Python
|
||||||
|
---
|
||||||
|
|
||||||
|
### Lists
|
||||||
|
|
||||||
|
list = []
|
||||||
|
|
||||||
|
list[i] = val
|
||||||
|
list[i:j] = otherlist
|
||||||
|
del list[i:j]
|
||||||
|
|
||||||
|
list.append(item)
|
||||||
|
list.extend(list)
|
||||||
|
list.insert(0, item)
|
||||||
|
list.pop()
|
||||||
|
list.remove(i)
|
||||||
|
|
||||||
|
list.reverse()
|
||||||
|
list.count(item)
|
||||||
|
|
||||||
|
list.sort()
|
||||||
|
|
||||||
|
zip(list1, list2)
|
||||||
|
sorted(list)
|
||||||
|
",".join(list)
|
||||||
|
|
||||||
|
### Dict
|
||||||
|
|
||||||
|
dict.keys()
|
||||||
|
dict.values()
|
||||||
|
"key" in dict
|
||||||
|
dict["key"] # throws KeyError
|
||||||
|
dict.get("key")
|
||||||
|
dict.setdefault("key", 1)
|
||||||
|
|
||||||
|
### Iteration
|
||||||
|
|
||||||
|
for item in ["a", "b", "c"]:
|
||||||
|
for i in range(4): # 0 to 3
|
||||||
|
for i in range(4, 8): # 4 to 7
|
||||||
|
for key, val in dict.items():
|
||||||
|
|
||||||
|
### [String](https://docs.python.org/2/library/stdtypes.html#string-methods)
|
||||||
|
|
||||||
|
str[0:4]
|
||||||
|
len(str)
|
||||||
|
|
||||||
|
string.replace("-", " ")
|
||||||
|
",".join(list)
|
||||||
|
"hi {0}".format('j')
|
||||||
|
str.find(",")
|
||||||
|
str.index(",") # same, but raises IndexError
|
||||||
|
str.count(",")
|
||||||
|
str.split(",")
|
||||||
|
|
||||||
|
str.lower()
|
||||||
|
str.upper()
|
||||||
|
str.title()
|
||||||
|
|
||||||
|
str.lstrip()
|
||||||
|
str.rstrip()
|
||||||
|
str.strip()
|
||||||
|
|
||||||
|
str.islower()
|
||||||
|
|
||||||
|
### Casting
|
||||||
|
|
||||||
|
int(str)
|
||||||
|
float(str)
|
||||||
|
|
||||||
|
### Comprehensions
|
||||||
|
|
||||||
|
[fn(i) for i in list] # .map
|
||||||
|
[fn(i) for i in list if i > 0] # .filter.map
|
||||||
|
|
||||||
|
### Regex
|
||||||
|
|
||||||
|
import regex
|
||||||
|
|
||||||
|
re.match(r'^[aeiou]', str)
|
||||||
|
re.sub(r'^[aeiou]', '?', str)
|
||||||
|
re.sub(r'(xyz)', r'\1', str)
|
||||||
|
|
||||||
|
expr = re.compile(r'^...$')
|
||||||
|
expr.match(...)
|
||||||
|
expr.sub(...)
|
||||||
|
|
|
@ -0,0 +1,176 @@
|
||||||
|
---
|
||||||
|
title: Vimscript functions
|
||||||
|
---
|
||||||
|
|
||||||
|
Dictionaries
|
||||||
|
------------
|
||||||
|
|
||||||
|
```vim
|
||||||
|
let colors = {
|
||||||
|
\ "apple": "red",
|
||||||
|
\ "banana": "yellow"
|
||||||
|
}
|
||||||
|
|
||||||
|
echo colors["a"]
|
||||||
|
echo get(colors, "apple") " supress error
|
||||||
|
|
||||||
|
remove(colors, "apple")
|
||||||
|
|
||||||
|
" :help E715
|
||||||
|
if has_key(dict, 'foo')
|
||||||
|
if empty(dict)
|
||||||
|
keys(dict)
|
||||||
|
len(dict)
|
||||||
|
|
||||||
|
max(dict)
|
||||||
|
min(dict)
|
||||||
|
|
||||||
|
count(dict, 'x')
|
||||||
|
string(dict)
|
||||||
|
|
||||||
|
map(dict, '<>> " . v:val')
|
||||||
|
extend(s:fruits, { ... })
|
||||||
|
```
|
||||||
|
|
||||||
|
```vim
|
||||||
|
for key in keys(mydict)
|
||||||
|
echo key . ': ' . mydict(key)
|
||||||
|
endfor
|
||||||
|
```
|
||||||
|
|
||||||
|
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")
|
||||||
|
```
|
||||||
|
|
||||||
|
Functions
|
||||||
|
---------
|
||||||
|
|
||||||
|
### Buffer
|
||||||
|
|
||||||
|
line('.') " current line number
|
||||||
|
col('.')
|
||||||
|
col('$')
|
||||||
|
|
||||||
|
getline('.') " current line as a string
|
||||||
|
getline(1) " get line 1
|
||||||
|
getline(1, 5) " get lines 1-5
|
||||||
|
search('^$') " next blank line
|
||||||
|
|
||||||
|
getcurpos() " [bufnum, lnum, col, off, curswant]
|
||||||
|
getpos('.') " [bufnum, lnum, col, off]
|
||||||
|
|
||||||
|
nextnonblank(1) " next non-blank line after line1
|
||||||
|
|
||||||
|
### Expand
|
||||||
|
|
||||||
|
expand('<cword>') " word under cursor
|
||||||
|
expand('%') " current file
|
||||||
|
|
||||||
|
" <cword> current word on cursor
|
||||||
|
" :p full path
|
||||||
|
" :h head
|
||||||
|
" :p:h dirname (/Users/rsc/project)
|
||||||
|
" :t tail (file.txt)
|
||||||
|
" :r root (file)
|
||||||
|
" :e extension (.txt)
|
||||||
|
" see :h cmdline-special
|
||||||
|
|
||||||
|
### Files
|
||||||
|
|
||||||
|
fnameescape('string')
|
||||||
|
fnamemodify('main.c', ':p:h')
|
||||||
|
fnamemodify(fname, ':e') " current file extension - see expand()
|
||||||
|
filereadable(fname)
|
||||||
|
getfsize('file.txt')
|
||||||
|
getcwd()
|
||||||
|
|
||||||
|
globpath(&rtp, "plugin/commentary.vim")
|
||||||
|
|
||||||
|
### Math
|
||||||
|
|
||||||
|
fmod(9, 2) " modulus
|
||||||
|
abs(-0.5)
|
||||||
|
sqrt(9)
|
||||||
|
|
||||||
|
trunc(1.84)
|
||||||
|
floor(1.84)
|
||||||
|
ceil(1.84)
|
||||||
|
float2nr(3.14)
|
||||||
|
|
||||||
|
### Casting
|
||||||
|
|
||||||
|
str2float('0.2')
|
||||||
|
str2nr('240')
|
||||||
|
str2nr('ff', '16')
|
||||||
|
|
||||||
|
string(0.3)
|
||||||
|
|
||||||
|
### Type checking
|
||||||
|
|
||||||
|
type(var) == type(0)
|
||||||
|
type(var) == type("")
|
||||||
|
type(var) == type(function("tr"))
|
||||||
|
type(var) == type([])
|
||||||
|
type(var) == type({})
|
||||||
|
type(var) == type(0.0)
|
||||||
|
|
||||||
|
### Date/time
|
||||||
|
|
||||||
|
strftime('%c')
|
||||||
|
strftime('%c',getftime('file.c'))
|
||||||
|
|
||||||
|
### Strings
|
||||||
|
|
||||||
|
if a =~ '\s*'
|
||||||
|
subst(str, '.', 'x', 'g')
|
||||||
|
strpart("abcdef", 3, 2) " == "de" (substring)
|
||||||
|
strpart("abcdef", 3) " == "def"
|
||||||
|
stridx("abcdef", "e") " == "e"
|
||||||
|
strridx() " reverse
|
||||||
|
|
||||||
|
matchstr('testing','test') " == 'test' (or '')
|
||||||
|
match('testing','test') " == 0
|
||||||
|
matchend('testing','test') " == 4
|
||||||
|
match('testing','\ctest') " ignore case
|
||||||
|
|
||||||
|
split(str, '\zs') " split into characters
|
||||||
|
|
||||||
|
strlen(str)
|
||||||
|
strchars() " accounts for composing chars
|
||||||
|
strwidth() " accounts for ambig characters
|
||||||
|
strdisplaywidth() " accounts for tab stops
|
||||||
|
|
||||||
|
### Syntax
|
||||||
|
|
||||||
|
synstack(line('.'),col('.')) " returns many
|
||||||
|
synID(line('.'),col('.'),1) " only one
|
||||||
|
|
||||||
|
synIDattr(id,"bg")
|
||||||
|
synIDattr(id,"name")
|
||||||
|
synIDtrans()
|
||||||
|
|
||||||
|
" syntax stack
|
||||||
|
map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
|
||||||
|
|
||||||
|
### Shell
|
||||||
|
|
||||||
|
system('ls '.shellescape(expand('%:h')))
|
||||||
|
|
||||||
|
Executing
|
||||||
|
---------
|
||||||
|
|
||||||
|
### Running commands
|
||||||
|
|
||||||
|
normal 'ddahello'
|
||||||
|
exe 'normal ^C' " with expansions
|
||||||
|
wincmd J
|
|
@ -2,49 +2,6 @@
|
||||||
title: Vimscript snippets
|
title: Vimscript snippets
|
||||||
---
|
---
|
||||||
|
|
||||||
## Functions
|
|
||||||
|
|
||||||
### Inspecting the buffer
|
|
||||||
|
|
||||||
line('.') " current line number
|
|
||||||
col('.')
|
|
||||||
|
|
||||||
getline('.') " current line as a string
|
|
||||||
getline(1)
|
|
||||||
getline(1, 0)
|
|
||||||
search("^$") " next blank line
|
|
||||||
|
|
||||||
getcurpos() " [bufnum, lnum, col, off, curswant]
|
|
||||||
getpos('.') " [bufnum, lnum, col, off]
|
|
||||||
|
|
||||||
expand('<cword>') " word under cursor
|
|
||||||
expand('%') " current file
|
|
||||||
|
|
||||||
" syntax stack
|
|
||||||
map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
|
|
||||||
|
|
||||||
### Files
|
|
||||||
|
|
||||||
fnameescape("string")
|
|
||||||
fnamemodify("main.c", ":p:h")
|
|
||||||
filereadable(fname)
|
|
||||||
fnamemodify(fname, ':e') " current file extension - see expand()
|
|
||||||
getfsize('file.txt')
|
|
||||||
getcwd()
|
|
||||||
|
|
||||||
### Math
|
|
||||||
|
|
||||||
fmod(9, 2) " modulus
|
|
||||||
floor(1.84)
|
|
||||||
ceil(1.84)
|
|
||||||
abs(-0.5)
|
|
||||||
|
|
||||||
### Strings
|
|
||||||
|
|
||||||
if a =~ '\s*'
|
|
||||||
|
|
||||||
## Binding
|
|
||||||
|
|
||||||
### Bind function to key and command
|
### Bind function to key and command
|
||||||
|
|
||||||
command! YoFunctionHere call s:YoFunctionHere()
|
command! YoFunctionHere call s:YoFunctionHere()
|
||||||
|
@ -52,16 +9,6 @@ title: Vimscript snippets
|
||||||
function! s:FunctionHere()
|
function! s:FunctionHere()
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
## Executing
|
|
||||||
|
|
||||||
### Execute normal keystrokes
|
|
||||||
|
|
||||||
### Running commands
|
|
||||||
|
|
||||||
normal 'ddahello'
|
|
||||||
exe 'normal ^C' " with expansions
|
|
||||||
wincmd J
|
|
||||||
|
|
||||||
### Call a function in insert mode
|
### Call a function in insert mode
|
||||||
|
|
||||||
inoremap X <CR>=script#myfunction()<CR>
|
inoremap X <CR>=script#myfunction()<CR>
|
||||||
|
|
Loading…
Reference in New Issue