Vimscript
Vimscript notesVimscript
- Can escape single quotes in a single quote string by doubling it: ’ this is double " this is escaped single ’’ ’
- Run command line commands using the
system('command string')
function
Scope
Vim allows you to restrict the scope function and variable definitions with simple prefixes:
s
: variables and functions only needed within the script itselfg
: globalb
: local to current bufferl
: local to current functiona
: argument of current functionw
: local to current window
Vim options can be retrieved and set using &
as a prefix. For example:
let &l:comments = 'fb:-'.&comments
This line gets the current comments setting with &comments
, and sets the local comment setting with a prepended string.
Saving files
You can use write
to programmatically write the current buffer. Sometimes update
may be preferable, where the buffer will only be written when there are unsaved changes that have been made.
Restoring position
setpos
will restore a cursor position retrieved fromgetpos
winsaveview
andwinrestview
save and restore actual window views. A bit more fully features and less jumpy thansetpos
can be at times.