this post was submitted on 24 Mar 2024
98 points (94.5% liked)

linuxmemes

20408 readers
961 users here now

I use Arch btw


Sister communities:

Community rules

  1. Follow the site-wide rules and code of conduct
  2. Be civil
  3. Post Linux-related content
  4. No recent reposts

Please report posts and comments that break these rules!

founded 1 year ago
MODERATORS
 
top 28 comments
sorted by: hot top controversial new old
[–] [email protected] 17 points 5 months ago (3 children)
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 
[–] [email protected] 11 points 5 months ago

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

[–] [email protected] 7 points 5 months ago (2 children)

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

[–] [email protected] 4 points 5 months ago (1 children)

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)

[–] [email protected] 2 points 5 months ago (1 children)

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

[–] [email protected] -1 points 5 months ago

Oh good point, I didn’t think about that

[–] [email protected] 0 points 5 months ago (1 children)

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

[–] [email protected] 3 points 5 months ago* (last edited 5 months ago)

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

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

[–] [email protected] 4 points 5 months ago* (last edited 5 months ago)
alias fuck='sudo $(fc -ln -1)'
[–] [email protected] 13 points 5 months ago (1 children)

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

[–] [email protected] 18 points 5 months ago (1 children)

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

[–] [email protected] 5 points 5 months ago

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

[–] [email protected] 4 points 5 months ago (7 children)

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

[–] [email protected] 3 points 5 months ago* (last edited 5 months ago)

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

[–] [email protected] 2 points 5 months ago

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

[–] [email protected] 2 points 5 months ago

It's a simple trick in Vim:

https://stackoverflow.com/a/7078429

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

[–] [email protected] 2 points 5 months ago

i use micro

[–] [email protected] 2 points 5 months ago* (last edited 5 months ago)

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

[–] [email protected] 1 points 5 months ago (1 children)

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

[–] [email protected] 2 points 5 months ago

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.

[–] [email protected] 4 points 5 months ago (1 children)
:w !sudo tee %

Warning: does not work for neovim

[–] [email protected] 1 points 5 months ago (1 children)

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

[–] [email protected] 2 points 5 months ago

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.

[–] [email protected] 4 points 5 months ago

One of the many reasons why I use micro

[–] [email protected] 2 points 5 months ago (1 children)

C-x C-f /sudo::/path/to/file

[–] [email protected] 2 points 5 months ago

I love Emacs. Thanks for teaching me someyhing today

[–] [email protected] 1 points 5 months ago* (last edited 5 months ago)

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.

-- Disable Suda in diff views
if not vim.api.nvim_win_get_option(0, 'diff') then
    vim.g.suda_smart_edit = 1
end

The vimscript version of the same would be:

if ! &diff
    let g:suda_smart_edit = 1
endif