I want vim to save folded code after I’ve closed the file. I’ve added the following code to both /etc/vimrc and ~/.vimrc:
au BufWinLeave ?* mkview
au BufWinEnter ?* silent loadview
When I open a file as a regular user (vim file) it works as expected. If I instead open with sudo vim file it doesn’t save folded code.
I know /etc/vimrc is being read. My theme is set at the bottom of that file and is working fine. I ran into a similar problem last week installing the vim surround plugin. When that was installed in ~/.vim it only applied to vim file. To get it working with sudo I had to install it separately in /usr/share/vim/current. What could be causing this?
Prolog
In fact you answered your question, so this is indeed the same issue you had before. It is caused by the algorithm Vim uses to find its configuration (see
:help initialization). But I wouldn’t call your solution a correct one. See:help $VIMfor information how you can use your configuration insudoenvironment (e.g. beingroot).As for your current issue.
See the following quotes from the documentation.
:help :mkview:And
:help 'viewdir':So after doing
sudoall views are stored by Vim at/root/.vim.Solution
Make
viewdirpoint to your~/.vim/viewdirectory, with something like the following in/root/.vimrc:Though, this approach has some issues itself:
/rootor/home/user, because Vim generates file names with tilde instead of full paths.The first issue can be solved by running
chownorchmodon new view file right after executing:mkviewcommand. It should be something like:But I don’t know good solution for the second issue, can only suggest writing a script to convert file names to full paths.