Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save a file in vim using sudo?

Tags:

vim

I want to map w! in vim to save a file using sudo silently (without need to press [Enter] and [L] keys).

Here is a line that works just perfectly:

command! W :execute ':silent w !sudo tee % > /dev/null' | :edit!

But it is W, not w! (with exclamation point)

How do I get that?

like image 216
blackst0ne Avatar asked Nov 05 '22 02:11

blackst0ne


1 Answers

The builtin commands cannot be user-overridden.

You could however do

 :cnoreabbrev w! W

to 'magically' translate a lone w to W, which has the desired effect, AFAICT

like image 146
sehe Avatar answered Nov 14 '22 15:11

sehe