Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8318087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:50:06+00:00 2026-06-08T21:50:06+00:00

I want vim to save folded code after I’ve closed the file. I’ve added

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:50:08+00:00Added an answer on June 8, 2026 at 9:50 pm

    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 $VIM for information how you can use your configuration in sudo environment (e.g. being root).

    As for your current issue.

    See the following quotes from the documentation. :help :mkview:

    :mkvie[w][!] [file] ...
            When [file] is omitted or is a number from 1 to 9, a
            name is generated and 'viewdir' prepended.
                        ...
    

    And :help 'viewdir':

     'viewdir' 'vdir'   string (default for ... for Unix: "~/.vim/view", ...
    

    So after doing sudo all views are stored by Vim at /root/.vim.

    Solution

    Make viewdir point to your ~/.vim/view directory, with something like the following in /root/.vimrc:

    :set viewdir=/home/user/.vim/view
    

    Though, this approach has some issues itself:

    1. Updating views saved by root will cause permission errors.
    2. You wont be able to pass stored views for files under /root or /home/user, because Vim generates file names with tilde instead of full paths.

    The first issue can be solved by running chown or chmod on new view file right after executing :mkview command. It should be something like:

    execute '!chown user:group' eval('&viewdir').'/'.substitute(expand('%:p:~'), '/', '+=', 'g').'='
    

    But I don’t know good solution for the second issue, can only suggest writing a script to convert file names to full paths.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to map w! in vim to save a file using sudo silently
I want to edit .vimrc file from Vim and apply them without restarting Vim.
I want to get the file name without the file extension in Vim. I
Is there a simple way to (in VIM) do save the currently open file
I want to use vim to write a part of my file to another
I have the following in .screenrc # I want to use Vim's navigation keys
I want to learn the vim documentation given in the standard help file. But
I'm editing an HTML file in Vim and I want the browser to refresh
I want csv file to be opened in vim in the same way it
I use Vim. I open a file. I edit it and I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.