25

Ok so this happens to me all the time. There has to be a better solution. Let's say you do vim /etc/somefile.conf and then you do i but realize you are not sudo and you can't write. So then I lose my changes by doing :q then sudo !! and make my changes again. Is there a better way to do this?

moinudin
  • 134,091
  • 45
  • 190
  • 216
Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123

7 Answers7

46

Try

:w !sudo tee "%"

The w ! takes the entire file and pipes it into a shell command. The shell command is sudo tee which runs tee as superuser. % is replaced with the current file name. Quotes needed for files that have either spaces or any other special characters in their names.

Victor Yarema
  • 1,183
  • 13
  • 15
Joel Spolsky
  • 33,372
  • 17
  • 89
  • 105
2

Depending on the extent of your changes, it might be faster to save (:w) your file with a different name, and then use sudo and cat to overwrite the content of the original file:

sudo sh -c 'cat changed > file'

Note that both cp and mv will replace the original file and its attributes (ownership, permissions, ACLs) will be lost. Do not use them unless you know how to fix the permissions afterwards.

thkala
  • 84,049
  • 23
  • 157
  • 201
1

Save the file elsewhere (like your home folder) and then sudo mv it to overwrite the original?

Adam Maras
  • 26,269
  • 6
  • 65
  • 91
0

I used this:

function! SaveAsSudo()
    let v=winsaveview()
    let a=system("stat -c \%a " .  shellescape(expand('%')))
    let a=substitute(a,"\n","","g")
    let a=substitute(a,"\r","","g")
    call system("sudo chmod 666 " . shellescape(expand('%')))
    w
    call system("sudo chmod " . a . " " . shellescape(expand('%')))
    call winrestview(v)
endfunction

I have mapped <F2> to :w<CR>. & <F8> to :call SaveAsSudo()<CR>

Only advantage this answer provides over the sudo tee option is: vim does not complain about unsaved buffer or file modified externally.

anishsane
  • 20,270
  • 5
  • 40
  • 73
0

Save the changes as another file and the make the approrpiate replacement.

Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
0

When vim starts up, the statusbar says [readonly], and the first time you try to edit, it says W10: Warning: Changing a readonly file and pauses for a full second. This is enough warning for me to quit and say sudoedit /etc/somefile.conf.

You can enforce this with a plugin: Make buffer modifiable state match file readonly state.

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • Yes I realize that but that warning is not always consistent on every system. Some flavors of linux don't show the warning. – Amir Raminfar Dec 19 '10 at 17:55
0

I use zsh templates and function completion.

Specifically this one. If I don't have write permissions, it prompts for my sudo password and automatically runs "sudo vim"…amongst other things.

jkyle
  • 2,002
  • 1
  • 18
  • 23
  • This is a good idea. However, there is a catch here - we don't always open files as separate files on command line. often files are opened from within `vim` using `:e` or from `(c)tags`. There the shell function will not help. – anishsane Jul 07 '14 at 17:19