Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change file permissions in Vim with open file

Tags:

linux

vim

sudo

I vim somefile.txt often without using sudo. Is it possible to use sudo within an open vim session so you don't have to close the file and reopen? New to Vim and Linux in general so any help is very appreciated. Thank you.


1 Answers

Well seems like I was late to the party. As said you should use :w !sudo tee % to switch to sudo. If you would like to change the permissions of the file so you will not need sudo again you could use :!chmod +w % in command mode with %getting replaced by the filename to change the permissions of the file afterwards, as stated here: How to change file permission from within vi.

like image 177
Vincent Scharf Avatar answered Nov 29 '25 18:11

Vincent Scharf