50 points
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 
permalink
report
reply
30 points

Considering the function name, here’s an obligatory thefuck plug

permalink
report
parent
reply
18 points

Isn’t this the same effect as just running ‘sudo !!’ ?

permalink
report
parent
reply
13 points

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)

permalink
report
parent
reply
8 points

Yeah but instead of a function you just make it an alias.

permalink
report
parent
reply
4 points

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

permalink
report
parent
reply
3 points

no because it takes the previous command and adds sudo to it right?

permalink
report
parent
reply
6 points
*

!! is a shortcut for whatever the last command was is it not?

E: https://devhints.io/bash#history

permalink
report
parent
reply
15 points
*
alias fuck='sudo $(fc -ln -1)'
permalink
report
parent
reply
26 points

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

permalink
report
reply
28 points

Now I understand how the Adeptus Mechanicus feel when they perform a ritual power-on.

permalink
report
parent
reply
10 points

It’s funny how years of use make that so intuitive but spelled out it’s a garbled mess

permalink
report
parent
reply
10 points

Lazier way:

:w !sudo tee %

permalink
report
parent
reply
2 points

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

permalink
report
parent
reply
15 points

Is there an editor that can request root privileges without restarting it? That would be quite useful.

permalink
report
reply
11 points

In vim, in normal mode you can do: :w !sudo tee %

permalink
report
parent
reply
2 points

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.

permalink
report
parent
reply
1 point

??? I used this in neovim twice today

:w !sudo tee % then reload when it asks.

permalink
report
parent
reply
9 points

i use micro

permalink
report
parent
reply
5 points

It’s a simple trick in Vim:

https://stackoverflow.com/a/7078429

For the lazy: :w !sudo tee > /dev/null %

permalink
report
parent
reply
4 points

kate does this in KDE, but it’s not cli.

permalink
report
parent
reply
4 points

(n)vim + suda.vim.

permalink
report
parent
reply
4 points
*

micro ftw, no need to even memorize a command, it’ll just ask if you want to use sudo

permalink
report
parent
reply
4 points
*

Yeah, in emacs you use tramp to open the file with /sudo::

permalink
report
parent
reply
11 points

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.

permalink
report
reply
2 points

So does Micro. Nano… Nano does not.

permalink
report
parent
reply
2 points

Eh, on console i can just do ssu nano <file>. Graphical editors are in question.

permalink
report
parent
reply
11 points
:w !sudo tee %

Warning: does not work for neovim

permalink
report
reply
2 points

Neovim, the one true vim, why hast thou forsaken me.

permalink
report
parent
reply
2 points

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.

permalink
report
parent
reply
1 point
*

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

permalink
report
parent
reply
1 point

sure it works in neovim

I didn’t do any editing since it’s just to prove a point, but I think it does fine.

permalink
report
parent
reply
1 point

Here is an alternative Piped link(s):

sure it works in neovim

Piped is a privacy-respecting open-source alternative frontend to YouTube.

I’m open-source; check me out at GitHub.

permalink
report
parent
reply

linuxmemes

!linuxmemes@lemmy.world

Create post

Hint: :q!


Sister communities:

Community rules (click to expand)

1. Follow the site-wide rules
2. Be civil
  • Understand the difference between a joke and an insult.
  • Do not harrass or attack members of the community for any reason.
  • Leave remarks of “peasantry” to the PCMR community. If you dislike an OS/service/application, attack the thing you dislike, not the individuals who use it. Some people may not have a choice.
  • Bigotry will not be tolerated.
  • These rules are somewhat loosened when the subject is a public figure. Still, do not attack their person or incite harrassment.
3. Post Linux-related content
  • Including Unix and BSD.
  • Non-Linux content is acceptable as long as it makes a reference to Linux. For example, the poorly made mockery of sudo in Windows.
  • No porn. Even if you watch it on a Linux machine.
4. No recent reposts
  • Everybody uses Arch btw, can’t quit Vim, and wants to interject for a moment. You can stop now.

Please report posts and comments that break these rules!

Community stats

  • 6.6K

    Monthly active users

  • 1.3K

    Posts

  • 71K

    Comments