54

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.

dkretz
  • 37,399
  • 13
  • 80
  • 138

5 Answers5

114

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: */

Note that this command works in most cases, however, servers are often setup without modeline turned on for security reasons. To turn on that feature add the following in your $HOME/.vimrc or the system $VIM/vimrc:

set modeline
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
ypnos
  • 50,202
  • 14
  • 95
  • 141
  • 14
    Be sure 'modeline' is set. – Jeremy Cantrell Nov 24 '08 at 16:24
  • 9
    More information is available on http://vim.wikia.com/wiki/Modeline_magic. For vim, you would have to use `/* vim: set tabstop=8 expandtab: */` for example. – Lekensteyn Feb 14 '12 at 19:58
  • 2
    @Lekensteyn or simply :help modeline ;) – Jaffa Jun 02 '12 at 13:10
  • Just my two cents, but I have to say it's a bit arrogant for an editor to pollute documents by storing editor-specific settings. – joonas.fi Jun 17 '15 at 10:42
  • 1
    I agree, however note that it is the user that puts these comments in, not the editor by itself. Also, vim is not the only editor that supports that feature these days. It depends on your habits if you like to use the feature or not. I personally don't. – ypnos Jun 17 '15 at 14:13
  • 1
    On my machine, `modelines` (plural) was set to `0`, causing it to still ignore. I guess also as a precaution. You can check the current setting with `:set modelines`, and modify with something line `:set modelines=10` to make sure it searches for inline settings within the first 10 lines. – YoYo Feb 20 '16 at 20:01
  • for example in Python: `# vim: set shiftwidth=2:` – Vitaly Zdanevich Apr 15 '17 at 18:17
23

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
  • 35,395
  • 6
  • 71
  • 104
10

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 Holcombe
  • 175,848
  • 15
  • 70
  • 63
  • If you don't have a bunch of plugins already installed, there is also the `:mkvimrc` command. But that also captures mappings, so if you have a bunch of plugin mappings you will need to go through and delete all those so you don't define them twice. – Ben Nov 26 '13 at 15:18
  • It worked for me, except the `~/.vimrc` part, which I haven't tried because it can't work anyway. This doesn't exactly do what's asked, but it helps. – Evgeni Sergeev Apr 24 '14 at 03:19
9

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

Vim also lets you save settings per file by using modelines

Matthewd
  • 376
  • 1
  • 4
4

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
Sagar Jain
  • 7,475
  • 12
  • 47
  • 83
Rob Wells
  • 36,220
  • 13
  • 81
  • 146