Vim Scripting Cheatsheet
Vim Scripting Cheatsheet
cheatsheet
Start hacking Learn by example
# Variables
Defining Variable prefixes
Other prefixes
The s: prefix is also available in function names. See :help local-variables
let var -= 2
let var += 5
let var .= 'string' " concat
# Strings
Strings String functions
1/6
# Functions
Functions Namespacing
infect('jake', 'bella')
# Loops
for s in list
echo s
continue " jump to start of loop
break " breaks out of a loop
endfor
while x < 5
endwhile
# Custom commands
Custom commands Commands calling functions
command! Save :set fo=want tw=80 nowrap command! Save call <SID>foo()
Custom commands start with uppercase letters. The ! redefines a command if it already exists. function! s:foo()
...
endfunction
2/6
-nargs=1 1 argument, includes spaces
-nargs=? 0 or 1 argument
# Flow
Conditionals Truthiness
if 3 > 2
if a && b Strings
if (a && b) || (c && d)
if !c
if name ==# 'John' " case-sensitive
if name ==? 'John' " case-insensitive
See :help expression-syntax. See: Operators if name == 'John' " depends on :set ignorecase
Identity operators
Regexp matches
a is b
a isnot b
"hello" =~ 'xx*'
"hello" !~ 'xx*'
Checks if it’s the same instance object. "hello" =~ '\v<\d+>'
\v enables “extended” regex mode which allows word boundary (<>), +, and
Single line
# Lists
Lists Functions
Concatenation
Sublists
3/6
let shortlist = mylist[2:-1] let longlist = mylist + [5, 6]
let shortlist = mylist[2:] " same let mylist += [7, 8]
Push
# Dictionaries
Dictionaries Using dictionaries
See :help dict
max(dict)
min(dict)
Extending Prefixes
keys(s:)
" Extending with more
let extend(s:fruits, { ... })
# Casting
str2float("2.3")
str2nr("3")
float2nr("3.14")
# Numbers
Numbers Floats
See :help Float
See :help Number. See: Numbers
Arithmetic
Math functions
3 / 2 "=> 1, integer division
3 / 2.0 "=> 1.5
4/6
3 * 2.0 "=> 6.0
sqrt(100)
floor(3.5)
ceil(3.3)
abs(-3.4)
# Vim-isms
Execute a command Running keystrokes
Getting filenames
See :help expand
Echo
Settings
echoerr 'oh it failed'
echomsg 'hello there'
set number echo 'hello'
set nonumber
set number! " toggle echohl WarningMsg | echomsg "=> " . a:msg | echohl None
set numberwidth=5
set guioptions+=e
Prompts
Built-ins
let result = confirm("Sure?")
execute "confirm q"
has("feature") " :h feature-list
executable("python")
globpath(&rtp, "syntax/c.vim")
exists("$ENV")
exists(":command")
exists("variable")
exists("+option")
exists("g:...")
# Mapping
Mapping commands Explanation Arguments
<buffer>
nmap [nvixso](nore)map
vmap <silent>
imap
xmap │ └ don't recurse <nowait>
nnoremap │
vnoremap └ normal, visual, insert,
inoremap eX mode, select, operator-pending
xnoremap
...
5/6
# Syntax
Highlights Filetype detection
6/6