When you realized you forgot sudo
When you realized you forgot sudo
:w !sudo tee %
Apparently that doesn’t work in NeoVim, so recently I installed the suda plugin.
Personally, I just doas nvim and then the file name that needs root access, but it’s a handy plugin in case I forget.
??? I used this in neovim twice today
:w !sudo tee % then reload when it asks.
kate does this in KDE, but it’s not cli.
tramp to open the file with /sudo:
This function takes the last command and puts sudo in front of it. Actually used it in a zoom call at work without thinking and it took a second to realize why everyone was laughing. Not my invention–came across it years ago on stackoverflow or someplace and thought it was funny/useful.
kmirl@tux:~$ ls /root ls: cannot open directory '/root': Permission denied kmirl@tux:~$ fuck [sudo] password for kmirl: bin debs docs Mail!! is a shortcut for whatever the last command was is it not?
!! won’t work in a function, so you must use the fc -ln -1 command in a subshell instead. Note the response that says eval shouldn’t be used (not sure why)
Eval shouldn’t be used on userinput. Meaning that if smb other than you may use this to change the system he could put malicious code in the eval part.
Probably doesn’t matter on shell level
Warning: does not work for neovim
Iirc the specific reason behind this is
As a result, sudo (without args) can’t work in nvim as it doesn’t have a tty to prompt the user for passwords. Nvim also used to do what vim did, but they found out spawning the tty was causing other issues (still present in vim) so they changed it.
There must be more to this. I just launched a terminal and created a file to test with nvim on arch and it works perfectly fine.
Take a file, sudo chown root:root filename, sudo chmod 700 filename, edit with nvim and save with :w !sudo tee % then reload. Works fine.
I’m on arch with suckless st.
I didn’t do any editing since it’s just to prove a point, but I think it does fine.
:w ! Sudo tee %
Kwrite/Kate asks you for password. Seriously, why can’t they all just use pkexec or some abstraction of it?
Sadly, i currently borked all Qt apps on my Gtk setup.
ssu nano <file>. Graphical editors are in question.
sudo !! without me having a conscious thought about it.
Lazy vim way I do it:
ggVG"wY:q! followed by sudo !! then VG"wp:x
Grab entire file and stuff it in register W
Exit file
Reopen sudo
Select all and replace with register W them write
Lazier way:
:w !sudo tee %
Yeah learning about tee from this thread honestly.
It’s been interesting realizing I had such a useful tool at my disposal but never knew
Use suda.vim for automatically dealing with such cases. Works with neovim as well.
I’ll also recommend adding the following to your init.lua or some config file because suda doesn’t play nicely with nvim -d or vimdiff.
sudoedit is more secure btw. Many editors are not built to be ran as root, and this copies the file to a temo directory, edits it without root, and then overwrites the original file on save with root.