Vim Script Basics¶
Table of Contents¶
- Type Conversion
- Types
- Type Checking
- Variable Namespaces
- Strings
- String Length
- String Concatenation
- String Comparison
- String Slicing
- Counting Occurrences of a Substring
- Find Position of a Substring
- Check if a String Starts With or Ends With a Substring
- Join Strings in a List with a Separator
- Replace a Substring
- Split a String
- Trimming/Stripping Strings of Whitespace
- Fuzzy Matching Strings
- String Methods
- Appending to a List
- Join Two Lists / Extend a List
- Insert an Item Into a List
- Remove an Item from a List
- Remove the Last Item from a List
- Find the Index of an Item in a List
- Find the index of an item in a List of Dictionaries by the item value
- List Slicing
- Adding multiple items to a list using repetition
- Count number of occurrences of an item in a list
- Get the length of a list
- Dictionaries
- The -> syntax
- Heredoc
- Resources
Type Conversion¶
Conversion | VimScript |
---|---|
Number to Float | floor(n) |
Float to Number | float2nr(f) |
Number to String | string(n) |
String to Number | str2nr(str) |
Float to String | string(f) |
String to Float | str2float(str) |
List to String | string(l) |
String to List | eval(str) |
Dict to String | string(d) |
String to Dict | eval(str) |
Types¶
Type | VimScript |
---|---|
Number | let num = 10 |
Float | let f = 3.4 |
Booelan | let done = v:true |
String | let str = "green" |
List | let l = [1, 2, 3] |
Dictionary | let d = #{a : 5, b : 6} |
Type Checking¶
Type | VimScript |
---|---|
Number | type(x) is v:t_number |
String | type(x) is v:t_string |
List | type(x) is v:t_list |
Dictionary | type(x) is v:t_dict |
Float | type(x) is v:t_float |
Boolean | type(x) is v:t_bool |
Variable Namespaces¶
All the variables in VimScript have a scope.
The default namespace is global (g:
), unless defined inside a function.
Inside a function, the default namespace is local (l:
)
Scope Prefix | Description |
---|---|
g: |
global |
l: |
function-local |
s: |
script-local |
a: |
function argument |
v: |
internal |
b: |
buffer local |
w: |
window local |
t: |
tab local |
Strings¶
Single quoted strings are string literals.
Double quoted strings are expanded (\n
adds a newline).
String Length¶
To find the actual length of a string in chars, use either:
len("string")
(This counts bytes)strwidth("string")
strcharlen("string")
All the functions available:
" number of bytes in a string
let n = len("Hello World")
" number of bytes in a string
let n = strlen("Hello World")
" number of characters in a string
let n = strcharlen("Hello World")
" number of characters in a string
let n = strwidth("Hello World")
String Concatenation¶
Strings can be concatenated with two dots: ..
String Comparison¶
" compare strings matching case
let str1 = "blue"
let str2 = "blue"
let str3 = "sky"
if str1 ==# str2
echo "str1 and str2 are same"
endif
if str1 is# str2
echo "str1 and str2 are same"
endif
if str1 !=# str3
echo "str1 and str3 are not same"
endif
if str1 isnot# str3
echo "str1 and str3 are not same"
endif
" compare strings ignoring case
let str1 = "Blue"
let str2 = "BLUE"
let str3 = "sky"
if str1 ==? str2
echo "str1 and str2 are same"
endif
if str1 is? str2
echo "str1 and str2 are same"
endif
if str1 !=? str3
echo "str1 and str3 are not same"
endif
if str1 isnot? str3
echo "str1 and str3 are not same"
endif
String Slicing¶
let str = "HelloWorld"
" use byte index range
echo str[2:4]
" use a negative byte range
echo str[-3:]
echo str[2:-3]
" use byte index and length
echo strpart(str, 2, 3)
" use character index and length
echo strcharpart(str, 2, 3)
" use the start and end character indexes
echo slice(str, 2, 3)
" exclude the last character
echo slice(str, 6, -1)
Counting Occurrences of a Substring¶
To count the occurrences of a substring, you can use the count()
method
of the string
type.
Methods are invoked with the method (->
) operator.
:h count()
Find Position of a Substring¶
There are two string
methods for finding positions of substrings.
string->strindex("substr")
- Find substring starting from the left.
string->strrindex("substr")
- Find substring starting from the right.
- Find substring starting from the right.
Check if a String Starts With or Ends With a Substring¶
Check if a string starts with or ends with a substring with
the =~#
comparison operator, or with a slice and the is#
operator.
let str = "running"
if str =~# '^run'
echo "starts with run"
endif
if str[:len('run') - 1] is# 'run'
echo "starts with run"
endif
if str =~# 'ing$'
echo "ends with ing"
endif
Join Strings in a List with a Separator¶
Use the join()
function on a list, with the separator as the second argument.
Replace a Substring¶
Use the string
method string->substitute(old, new, flags)
.
Split a String¶
Use the string
method string->split(separator)
to split a string.
Trimming/Stripping Strings of Whitespace¶
Use the string
method string->trim(char, direction)
Direction is:
0
: Remove from beginning and end (default)1
: Remove from beginning2
: Remove from end
Fuzzy Matching Strings¶
Use the matchfuzzy()
function to fuzzily-search for a substring.
String Methods¶
Method | VimScript |
---|---|
capitalize() |
'one two'->substitute('.', '\u&', '') |
count() |
"abbc"->count('b') |
endswith() |
'running' =~# 'ing$' |
expandtabs() |
"a\tb"->substitute("\t", repeat(' ', 8), 'g') |
find() |
"running"->stridx('nn') |
index() |
'hello'->stridx('e') |
isalnum() |
str =~ '^[[:alnum:]]\+' |
isalpha() |
str =~ '^\a\+$' |
isdigit() |
str =~ '^\d\+$' |
islower() |
str =~ '^\l\+$' |
isspace() |
str =~ '^\s\+$' |
istitle() |
str =~ '\(\<\u\l\+\>\)\s\?' |
isupper() |
str =~ '^\u\+$' |
join() |
join(['a', 'b', 'c'], ':') |
lower() |
'Hello'->tolower() |
lstrip() |
' vim '->trim(' ', 1) |
partition() |
'ab-cd-ef'->matchlist('\(.\{-}\)\(-\)\(.*\)')[1:3] |
replace() |
'abc'->substitute('abc', 'xyz', 'g') |
rfind() |
'running'->strridx('ing') |
rindex() |
'running'->strridx('ing') |
rpartition() |
'ab-cd-ef'->matchlist('\(.*\)\(-\)\(.*\)')[1:3] |
rstrip() |
' vim '->trim(' ', 2) |
split() |
'a-b-c'->split('-') |
splitlines() |
"one\ntwo"->split("\n") |
startswith() |
'running' =~# '^run' |
strip() |
' vim '->trim() |
title() |
'onE twO'->substitute('\<\(.\)\(\S\+\)\>', '\u\1\L\2', 'g') |
upper() |
'Hello'->toupper() |
translate() |
'abcd'->tr('bd', '12') |
Appending to a List¶
Call the list->add()
method to append to a list.
Join Two Lists / Extend a List¶
Join or extend a list with another list with the list->extend()
method.
Or, to concatenate a list, use the +
operator.
let l = []
call extend(l, [1, 2])
let l = l->extend([3, 4])
let l = extend(l, [5, 6])
let l += [7, 8]
echo(l)
" [1, 2, 3, 4, 5, 6, 7, 8]
let l = [1, 2] + [3, 4]
echo(l)
" [1, 2, 3, 4]
Insert an Item Into a List¶
Insert an item into a list with the list->insert(obj, idx)
method.
let l = [2, 4]
" Insert before index 1
eval l->insert(3, 1)
" Insert just before last item
eval l->insert(5, -1)
" Insert at the beginning
eval l->insert(1)
echo(l)
" [1, 2, 3, 4, 5]
Remove an Item from a List¶
Use the list->remove(idx)
method, or unlet
to remove an item from a list.
Remove the Last Item from a List¶
Use the list->remove(-1)
method to remove (and return) the last item from a list.
let l = [1, 2, 3, 4, 5]
let last_item = l->remove(-1)
eval l->remove(-1)
echo(last_item)
" 5
echo(l)
" [1, 2, 3]
Find the Index of an Item in a List¶
Use the list->index()
method to get the index of an item in a list.
Find the index of an item in a List of Dictionaries by the item value¶
Use the list->indexof()
method to get the index of a dictionary value.
This should be passed
let colors = [{'color': 'red'}, {'color': 'green'}, {'color': 'blue'}]
let idx = indexof(colors, {i, v -> v.color == 'blue'})
echo(idx)
" 2
let idxx = colors->indexof({i, v -> v.color == 'blue'})
echo(idxx)
" 2
List Slicing¶
To slice a list, you can use the square bracket slice notation (l[:-1]
) or
the list->slice()
method.
slice()
does not include theend
index.- This can also be used on strings.
- This can also be used on strings.
Adding multiple items to a list using repetition¶
Use the list->repeat()
method to add multiples of an item to a list.
Count number of occurrences of an item in a list¶
Use the list->count()
method to find all occurrences of an item in a list.
Get the length of a list¶
Use the list->len()
method to get the length of a list.
Dictionaries¶
Dictionaries can be defined just like in Python.
#{}
syntax can be used.The -> syntax¶
The ->
syntax can be used variables to call methods availale for the type
of variable that calls it.
Essentially it passes in self
as the first argument.
This also allows for chaining, passing the value that one method returns to the
next method:
Example of using a lambda:
When using ->
the expr7
operators will be applied first, so:
->name(
can not contain white space.There can be white space before the
->
and after the (
,
so you can split the lines like this:
When using the lambda form there can't be white space between the }
and the (
.
Heredoc¶
Assigning multi-line values to a variable.
Resources¶
- Vim Script for Python Developers
:h usr_41.txt