Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Comments on An alternative to pacman sudo nag?

Post

An alternative to pacman sudo nag?

+1
−0

Some pacman functions require sudo. When I forget the sudo, you get:

error: you cannot perform this operation unless you are root.

I then have to press the keys: up, home, s, u, d, o, space, enter to rerun with sudo. I find this irritating and it breaks my flow.

Is there a better solution here? If sudo is required for an operation, it should be run with sudo, rather than erroring out. In most situations where sudo is dangerous, it would prompt for a password anyway, so there's very little danger here. I have almost never ran pacman with sudo when I didn't mean to, but I have forgotten the sudo many times.

I could alias pacman to sudo pacman. However, some operations don't need sudo, like -Q. That alias would result in needless password prompts, which I also want to avoid.

How can I make pacman automatically use sudo when needed? I'd also like it to participate in sudo caching, so for example I don't want a situation like systemctl which automatically asks for sudo password when needed, but fails to remember it.

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Command repetition with modifications (2 comments)
Command repetition with modifications
Canina‭ wrote 3 months ago

This isn't an answer specific to pacman, but if you're using bash, you can use command repetition with a prefix (or suffix).

For example:

$ ls /root
ls: cannot open directory '/root': Permission denied
$ sudo !!
sudo ls /root
[sudo] password for <user>: 
<list of files in /root>
$

!! (which is a synonym for !-1) expands to the immediately preceding command as executed including any parameters. See bash(1) under "event designators" for other variations on the theme.

mr Tsjolder‭ wrote 3 months ago

This feature is known as history expansion and is probably what the OP is looking for. Originally introduced in csh, and introduced in bash, but also seems to work in sh and zsh.