0

I quite often open linux system configuration files with vim and sometimes have a session with lots of tabs, splits, quicklists etc that I want to save to a session and restore again later.
The problem is I usually only open as a non-root user with read-only access and therefore my vim process doesn't have privileges to save the Session.vim file in the current directory (I guess it may be able to save to home - but My preference is to save the session file to the site where I'm working).
Is it possible to elevate to sudo privileges inside vim simply for the :mksession command?

  • Why don't you simply do all that as root, as intended? – romainl Nov 21 '15 at 11:11
  • @romainl Im not sure I understand, do you mean :! sudo vim -exec "mksession" or something like that? – the_velour_fog Nov 21 '15 at 11:14
  • No. Edit system files as root. – romainl Nov 21 '15 at 11:56
  • you mean like in my answer below? – the_velour_fog Nov 21 '15 at 11:58
  • 2
    Editors are for editing files, if you only want to take a look, use a pager like less. As for "sessions", take a look at screen, tmuxor detach. – romainl Nov 21 '15 at 12:50
1

Its a bit hacky, so I'd be interested if anyone else has a better answer, but I was able to achieve this inside vim with:

  • :! sudo touch Session.vim
  • :! sudo chown user:user Session.vim
  • :mksession Session.vim or :Obsess (because I'm using tim popes vim-obsession plugin)

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.