I frequently use :ab to save typing time during coding. For e.g. :ab mat matrix to replace mat by matrix every time I type mat.

Is there any way of storing and loading the abbreviations I create for a given file? I want something to store my abbreviations as and when I declare them and also reload them when I open a file. I would prefer the abbreviations to be "local" to a file rather than global but I can work around this if necessary.

up vote 1 down vote accepted

If it's for a single specific file, you could add an autocommand (:help autocommand or :help 40.3) to your .vimrc:

au BufRead,BufNewFile /path/to/foobar call FoobarSettings()
function FoobarSettings()
  ab mat matrix
  " ... more setup commands
endfunction

Change "foobar" to something that makes more sense for you.

A less flexible clunkier shotgun-style approach is to use sessions (:help sessions or :help 21.4). It is unwieldy because sessions by default save a great deal of things including window sizes, open files, options, mappings, folds, etc. You can change this with the 'sessionoptions' option if you like.

  • After you've created opened the file and set up the abbreviations, :mksession! sessionfile.vim.
  • To restore the session, from the shell you can do vim -S sessionfile.vim or from inside vim you can do :source sessionfile.vim.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.