# vim9script reminder
# use `var` to define variable, instead of `let`
# boolean
true
false
# option value &foo
echo &number
# assigns value to option
&number = 1
# contents of register `q`
echo @q
# remove or set contents of register `q`
@q = ""
# set register q in linewise mode
setreg('q', '', 'V')
# string concatenation
var foo = "abc" .. "def"
# string interpolation
var name = "tiger"
echo $"hello {name}!"
# setting variable name to register
# @0 is last yanked text
var name = @0
# list
var foo_list = [1, 2, 3]
var first_item = foo_list[0]
var first_two_items = foo_list[0 : 2]
var last_two_items = foo_list[-2 : ]
# list concatenation
var foo_list_extended = foo_list + [4, 5]
# builtin list methods
mylist->add(val1)->add(val2)
mylist->copy()
mylist->count(val)
mylist->empty()
mylist->extend(otherlist)
mylist->filter(expr2)
mylist->flatten()
mylist->foreach(expr2)
mylist->indexof(expr)
mylist->insert(item)
mylist->join()
mylist->len()
mylist->map(expr2)
mylist->max()
mylist->min()
mylist->remove(idx)
mylist->repeat(count)
mylist->reverse()
mylist->sort()
mylist->string()
mylist->type()
mylist->uniq()
# dictionary
var foo = {"a": 1, "b": 2, "c": 3}
echomsg foo["a"]
for [key, value] in foo->items()
echomsg key value
endfor
# dictionary methods
mydict->has_key(key)
mydict->items()
mydict->keys()
mydict->values()
# if
var a = 1.5
if a > 2
echo "a > 2" a
elseif a > 1
echo "a > 1" a
else
echo "a < 1" a
endif
# for loop
var foo = [1, 2, 3]
for i in foo
echo i
endfor
# regex match, right hand side string is used as a pattern
# echo true
echo "foo" =~ '^f.*'
# echo false
echo "foo" =~ 'b.*'
# powerful commands
# execute the string from the evaluation of {expr1} as an cli # command
execute "normal @q"
# calling vim builtin function, see `:help builtin.txt`
# see help for builtin function `substitute` at `:help substitude()`
var remove_prefix = substitute(posix_file_path, '^saltus/', '', 'g')
# Types
float, string, bool, number, float, string, blob, list<type>, dict<type>, job,
channel, func
Example function
implementation
def GetWordAfterPrefix(prefix_string: string): string
# search for the line number and column number for the prefix_string
# e.g. the line and column of character 'f' in `def \zsfoo`
# flag `b` - search backward
# flag `n` - do not move the cursor
# see also `:help search()`
var [match_line_number, match_col_number] = (prefix_string .. '\zs')->searchpos('bn')
var line = getline(match_line_number)
# get the word with matching column position - 1, `-1` is needed to include
# the first character of the word, e.g. word would be `foo`
var word = line->matchstr('\w*', match_col_number - 1)
return word
enddef
def g:YankWordAfterPrefix(prefix_string: string)
var word = GetWordAfterPrefix(prefix_string)
echom 'yanked' word
setreg('+', word)
enddef
nnoremap <leader>yf :call YankWordAfterPrefix("def ")<cr>
nnoremap <leader>yc :call YankWordAfterPrefix("class ")<cr>
def GetPythonFileImportPath(): string
var posix_file_path = expand("%")
var python_import_path = posix_file_path
->substitute('^saltus/', '', 'g')
->substitute('.py$', '', 'g')
->substitute('/', '.', 'g')
return python_import_path
enddef
Using python in vim
def g:JumpToTestFile()
py3 << EOF
from vim_python import get_or_create_alternative_file
# vim.eval("@%") gets the filepath in current buffer
test_filepath = get_or_create_alternative_file(filepath=vim.eval("@%"))
# open test_filepath in current window
vim.command(f"tabnew {test_filepath}")
EOF
enddef
running a vimscript file
foo.vim
> cat foo.vimvim9scriptvar a = [1, 2, 3]echomsg a> vim -S foo.vim