6

I just recently learned how to use folds and I find it very useful. I have added a rule to automatically save the view with my folds when I'm editing the markdown files with the rule:

augroup QuickNotes
  au BufWinLeave *.md mkview
  au BufWinEnter *.md silent loadview
augroup END

But when I have saved it and copied a file somewhere the folds were there no more.

My question is:
Is it possible to save folds (the view) into the text file I'm editting?
If not then maybe I can write a rule that will save the view into something like .%.view (dot filename dot view) ?

Extra curious:
If I save the views to the default location- where is that?

2

You can give :mkview a filename as argument:

augroup QuickNotes
    autocmd!
    autocmd BufWinLeave *.md execute "mkview! " . expand('<afile>:p:h') . "/." . expand('<afile>:t') . ".view"

and use :source instead of :loadview:

    autocmd BufWinEnter *.md execute "silent! source " . expand('%:p:h') . "/." . expand('%:t') . ".view"
augroup END
  • Thanks. I will test this. Could you also elaborate on where views are saved by default? – lewiatan Sep 9 '16 at 7:54
  • I does save the view to the file but the view does not get reloaded later... – lewiatan Sep 9 '16 at 8:11
  • Without argument, :mkview and :loadview will work with the default location. If you want to make them work with a specific filename you must give it to them as argument. – romainl Sep 9 '16 at 8:14
  • yes, but how? for me loadview only takes one argument which is [nr]. Help says that : Load the view for the current file. When [nr] is omitted, the view stored with ":mkview" is loaded. When [nr] is specified, the view stored with ":mkview [nr]" is loaded. – lewiatan Sep 9 '16 at 8:26
  • 1
    You can use :source <path/to/view>; the saved view is nothing but an auto-generated Vim script. – Ingo Karkat Sep 9 '16 at 8:40
2

You'll find that information at :help :mkview:

:mkvie[w][!] [file] Write a Vim script that restores the contents of the
                        current window.
                        When [!] is included an existing file is overwritten.
                        When [file] is omitted or is a number from 1 to 9, a
                        name is generated and 'viewdir' prepended.  [...]

The generated filename has the full path of the original file encoded in it; that explains why it isn't loaded if you rename the original file.

So, you either need to move / copy the corresponding view file (found inside 'viewdir'; default ~/.vim/view), or :source it once to reapply the folds (then :autocmds will then ensure the view gets persisted under the new file name).

The view always is a separate file; I would not recommend putting it into the same directory as the original file.

  • " I would not recommend putting it into the same directory as the original file." ...why? – user6479 Jan 31 '18 at 21:47
  • @gojira: It just clutters the directory; you don't want to accidentally check it into version control, or upload it to a server. Also, unless you specify your own file, the view file name has the full original filespec encoded into it, so you cannot move both easily around, anyway (this is what my answer is about). – Ingo Karkat Feb 2 '18 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.