fuck() {
sudo $(fc -ln -1)
}
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
According to this super user question, someone said that !!
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)
!!
is a shortcut for whatever the last command was is it not?
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
Now I understand how the Adeptus Mechanicus feel when they perform a ritual power-on.
Is there an editor that can request root privileges without restarting it? That would be quite useful.
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.
Sure, you can do this in emacs: https://fuco1.github.io/2017-04-20-Save-write-protected-files-with-sudo-automatically.html
It’s a simple trick in Vim:
https://stackoverflow.com/a/7078429
For the lazy: :w !sudo tee > /dev/null %
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.
:w !sudo tee %
Warning: does not work for neovim
Iirc the specific reason behind this is
- sudo by default requires a tty to run
- vim’s bang spawns a tty to execute commands
- nvim’s bang executes the command directly, then pipes the output to nvim
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.
Edit:
Made a demo vid - https://youtu.be/YKZuAvoSW5g
I didn’t do any editing since it’s just to prove a point, but I think it does fine.
Here is an alternative Piped link(s):
Piped is a privacy-respecting open-source alternative frontend to YouTube.
I’m open-source; check me out at GitHub.