Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: becoming root inside vim

  1. #1
    Join Date
    May 2008
    Beans
    4

    becoming root inside vim

    Hey,
    too often when browsing as normal user in say /etc i:

    $ vim <filename>

    to start editing and then when i go to :wq to save i realize i had forgotten to start it all off with:

    $ sudo vim <filename>

    and i now don't have permission to save. I know that i can :w /tmp/x and then:

    $ sudo cp /tmp/x /etc/<filename>

    but i was wondering if someone perhaps knew of some nice vim trick i could do so that i can save without retyping all the text..., sort of the way you can "become root" from within aptitude

    many thanks!
    _J

  2. #2
    Join Date
    Apr 2007
    Beans
    14,781

    Re: becoming root inside vim

    Quote Originally Posted by purpleidea View Post
    but i was wondering if someone perhaps knew of some nice vim trick i could do so that i can save without retyping all the text..., sort of the way you can "become root" from within aptitude
    Yes, run the editor with sudo when you do it.

  3. #3
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,279
    Distro
    Kubuntu

    Re: becoming root inside vim

    I'm not aware of any trick, other than perhaps doing something (stupid) like changing the permissions of the file so that it is writable by a regular user, and then after the write is complete, reverting back to the original permissions.

    While in vim, you can issue shell commands using the :!<cmd>, where <cmd> could be something like "sudo chmod 666 filename". When you are done, repeat the command but change the permissions to 644.

    Personally I think this is crude and should be avoided. When you use vim to open a protected file, it tells you at the very bottom of the editor that it is a read-only file. Try to get into the habit at looking at the bottom of the editor when you first edit a file.

  4. #4
    Join Date
    Dec 2006
    Location
    Australia
    Beans
    1,097
    Distro
    Xubuntu 15.10 Wily Werewolf

    Re: becoming root inside vim

    Quote Originally Posted by purpleidea View Post
    but i was wondering if someone perhaps knew of some nice vim trick i could do so that i can save without retyping all the text..., sort of the way you can "become root" from within aptitude
    I don't know of any tricks like that within vim.

    You could always :w ~/temp.file first and then sudo cp ~/temp.file /destination/dir later.

  5. #5
    Join Date
    Oct 2007
    Location
    Fort Collins, CO, USA
    Beans
    480
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: becoming root inside vim

    That is an interesting question.
    I sometimes find myself writing temporary files and thinking "I'll be baack".
    There is a sudo vi feature at
    http://www.vim.org/scripts/script.php?script_id=729
    that has a special sudo prefix for opening a file with sudo.
    Then you could actually use split buffers to merge in changes.

    Taking a tactic from that script it would be possible to use a command map
    to do an sudo write from a : command.
    Warm up with putting this in .vimrc-
    Code:
    cmap w!! %!sudo tee > /dev/null %
    then modify a protected file and try to write with
    :w
    then
    :w!
    then
    :w!!

    Got it!

  6. #6
    Join Date
    Jun 2006
    Location
    Gwangju, Korea
    Beans
    3,479

    Re: becoming root inside vim

    I make a wrapper for vim. I call this script ~/bin/vi (I'm used to typing vi instead of vim, anyway):
    Code:
    #!/bin/bash
    
    precommand=
    for i in "$@" ; do
        if [ -f "$i" -a ! -w "$i" ] ; then precommand="sudo " ; break ; fi
    done
    
    exec ${precommand}vim "$@"
    This script runs vim with sudo if any file mentioned on the command line isn't writable.

    For the way I work, this isn't risky. Everything (with a few minor exceptions) outside of $HOME isn't user-writable. So the only safety against accidentally doing something stupid is knowing what I'm editing--no matter whether I typed sudo or not.
    Last edited by mssever; May 5th, 2008 at 07:39 AM.

  7. #7
    Join Date
    May 2008
    Beans
    4

    Re: becoming root inside vim

    Quote Originally Posted by stroyan View Post
    That is an interesting question.
    I sometimes find myself writing temporary files and thinking "I'll be baack".
    There is a sudo vi feature at
    http://www.vim.org/scripts/script.php?script_id=729
    that has a special sudo prefix for opening a file with sudo.
    Then you could actually use split buffers to merge in changes.

    Taking a tactic from that script it would be possible to use a command map
    to do an sudo write from a : command.
    Warm up with putting this in .vimrc-
    Code:
    cmap w!! %!sudo tee > /dev/null %
    then modify a protected file and try to write with
    :w
    then
    :w!
    then
    :w!!

    Got it!
    took me a bit of effort to figure this out, but ultimately this was the perfect answer / solution.
    many thanks !

    _J

  8. #8
    Join Date
    May 2007
    Location
    Waikato, New Zealand
    Beans
    19
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: becoming root inside vim

    Quote Originally Posted by purpleidea View Post
    took me a bit of effort to figure this out, but ultimately this was the perfect answer / solution.
    many thanks !

    _J
    This is exactly what I'd also like to do - I've placed the line in my .vimrc and tried typing w!! which seems to do nothing. What am I missing?

  9. #9
    Join Date
    Oct 2007
    Location
    $LOCATION
    Beans
    733

    Re: becoming root inside vim

    whenever that happens to me I just do :q! which should force it to save.
    hp dv6395 ea 2gb ram core 2 duo 2.00 Ghz archlinux User | Arch rocks! | Submit Bug Reports! https://launchpad.net/ubuntu | Include detail in your thread titles!

  10. #10
    Join Date
    Dec 2006
    Location
    Australia
    Beans
    1,097
    Distro
    Xubuntu 15.10 Wily Werewolf

    Re: becoming root inside vim

    Quote Originally Posted by MONODA View Post
    whenever that happens to me I just do :q! which should force it to save.
    Actually that forces vim to quit without saving.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •