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?

- 134,091
- 45
- 190
- 216

- 33,777
- 7
- 93
- 123
7 Answers
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.

- 1,183
- 13
- 15

- 33,372
- 17
- 89
- 105
-
3:w ! is very different than :w! Now I know. :) – Amir Raminfar Dec 19 '10 at 18:01
-
2and that is why i never use vim. And also thanks for putting me above 10,000 rep. :) – Joel Spolsky Dec 19 '10 at 18:05
-
7I have the following in my .vimrc for just this purpose: "cmap w!! w !sudo tee % > /dev/null" – kejadlen Dec 19 '10 at 18:27
-
@kejadlen - that's a great solution! – Amir Raminfar Dec 19 '10 at 18:43
-
This doesn't work for files that have spaces in their names. The proper generic answer should be `:w !sudo tee "%"`. – Victor Yarema Jun 29 '20 at 18:26
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.

- 84,049
- 23
- 157
- 201
Save the file elsewhere (like your home folder) and then sudo mv
it to overwrite the original?

- 26,269
- 6
- 65
- 91
-
1I'd suggest `cat changed > file` to preserve the original file attributes. `mv` for this is a recipe for disaster. – thkala Dec 19 '10 at 17:33
-
What would be the command to do this within vim, once you've already made your changes as a non-root user? – WhiteHotLoveTiger Apr 04 '17 at 18:46
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.

- 20,270
- 5
- 40
- 73
-
A nice race you have here. Of course it probably doesn't matter much on a personal machine. – Michał Politowski Jan 12 '14 at 09:21
-
Save the changes as another file and the make the approrpiate replacement.

- 22,026
- 26
- 90
- 148
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.

- 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
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.

- 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