tags:

views:

2229

answers:

5

Is there any way to save the state of vim settings with a document?

To clarify: I'm editing a document and I change a bunch of settings. I don't necessarily recall which; and I don't want to use these settings again, except for the current document. I don't want to manually try to remember what I've changed; or what the magic abbreviations are for the settings I've changed. I just want to have, say, for "mydoc.txt", a "mydoc.vim" file that puts me back where I left off, and the settings file would be saved automatically based on a vim setting, say, or maybe a ctrl-key does it before I exit. It would be handy if vim could automatically look for such a file.

And it would be preferable not to have to edit the settings into and out of the document itself.

+13  A: 

Yes, vim settings can be included within the document.

They are mostly found within comments, so they don't mess up the original file. An example for tab-specific settings is:

/* ex: set tabstop=8 expandtab: */
ypnos
Be sure 'modeline' is set.
Jeremy Cantrell
+4  A: 

You can save your settings globally by editing your .vimrc file.

Vim also lets you save settings per file by using modelines

Matthewd
+3  A: 

Here's how you save all your current settings to a file:

:redir > textfile.txt 
:set all 
:redir END

If you like, just rename that file to ~/.vimrc and away you go.

Harley
+10  A: 

You can use Vim's Session support:

:mksession

you can later load this by either running vim -S Session.vim, or using source Session.vim

There are also vim addons to automate session loading/saving

Hasturkun
+1  A: 

You could maybe save the file as a particular type, e.g. special filename format or extension, and then define an autocommand in your .vimrc for that filetype.

I do this for my makefiles to ensure that I have the various settings I need for specific files.

For example, here's my autocommand dec.

if has("autocmd")
  autocmd BufRead,BufNewFile Makefile*  :set noexpandtab
  autocmd BufRead,BufNewFile mirror.conf    :set noexpandtab
  autocmd BufRead,BufNewFile *.html*    :set shiftwidth=2
  autocmd BufRead,BufNewFile diff_files :set autowrite
  autocmd BufRead,BufNewFile lbnamed*   :set ft=perl
  autocmd BufRead,BufNewFile *.t        :set ft=perl
endif

HTH

cheers,

Rob

Rob Wells