Vimscript

Vimscript notes

Vimscript

  • 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 itself
  • g: global
  • b: local to current buffer
  • l: local to current function
  • a: argument of current function
  • w: 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 from getpos
  • winsaveview and winrestview save and restore actual window views. A bit more fully features and less jumpy than setpos can be at times.