Electrotek

From programming to day dreaming

Save changes in Vim even if you forgot to sudo

If you forgot to run Vim with sudo and don’t have permissions to save a file, you can use a little trick from within Vim:

:w ! sudo tee % 

Write buffer [w] to external (system) command [!], the command is [sudo tee] and argument for tee is current filename [%]. Usually you would also redirect tee output somewhere (since it splits standard input by writing into argument file and standard output), in our case it could be tee % > /dev/null, but I don’t think it’s necessary here.

Probably a known thing, but anyway, love it, so just wanted to share…

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.