You are not logged in.

#1 2009-09-12 00:47:15

A Future Pilot
Member
Registered: 2008-10-17
Posts: 120

Typing sudo AFTER the command

Is it possible?? I've been looking at aliases and functions, and I think it might be possible to make a function for it.

I want it because I get annoyed after typing out a really long command, just to realize that I needed "sudo" at the beginning. :-)

Offline

#2 2009-09-12 00:49:48

Pox
Member
From: Melbourne, AU
Registered: 2007-10-04
Posts: 66

Re: Typing sudo AFTER the command

It'd have to be implemented by the shell; not sure if bash can do it or not.
I have a suggestion, though - just hit ctrl-a or home, and then type sudo.  Not really that hard. tongue

Offline

#3 2009-09-12 00:51:23

A Future Pilot
Member
Registered: 2008-10-17
Posts: 120

Re: Typing sudo AFTER the command

Oh, I didn't know that! Thanks!

Offline

#4 2009-09-12 01:20:11

mikewu
Member
Registered: 2009-06-20
Posts: 8

Re: Typing sudo AFTER the command

Similar to what Pox suggested, I bound something similar using readline macros.
Put this in your ~/.inputrc
"\C-s":"\C-asudo \C-e"
This binds control-s to a macro which jumps to the beginning, prepends sudo, then jumps to the end.

Offline

#5 2009-09-12 02:47:14

A Future Pilot
Member
Registered: 2008-10-17
Posts: 120

Re: Typing sudo AFTER the command

I tried that, (I put in /etc/inputrc instead though) but it didn't work, it just froze the tty hmm...

I was also wondering about the "\e[A":history-search-backward" and "\e[B":history-search-forward" I like what it does, except I'd like to be able to hit down so that it went blank again, instead of having to erase the whole string. e.g. I type sudo then hit up. It comes up twice it comes up with sudo nano /etc/profile and then sudo shutdown -h now. If I hit down it will go back to sudo nano /etc/profile, but not to a blank prompt. This is rather annoying when the final command was very long :-)

Offline

#6 2009-09-12 02:54:52

Square
Member
Registered: 2008-06-11
Posts: 435

Re: Typing sudo AFTER the command

You can also use 'sudo !!' if you've entered a command to rerun it with sudo.


 

Offline

#7 2009-09-12 08:22:33

alterecco
Member
Registered: 2009-07-13
Posts: 152

Re: Typing sudo AFTER the command

A Future Pilot wrote:

I tried that, (I put in /etc/inputrc instead though) but it didn't work, it just froze the tty hmm...

In most terms Ctrl-s will freeze the term. Use Ctrl-q to unfreeze it again (the switch is called xon/xoff). Beware that if you have hacked away at the keyboard in between, the keystrokes will be buffered and sent afterwards (at least in urxvt).
So, the deal is that in general it is not good to bind commands to Ctrl-s or Ctrl-q in the term.

.]

Offline

#8 2009-09-12 12:22:17

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: Typing sudo AFTER the command

Also, remember that the "home" key will return to the start of the line and that you can hit 'enter' at any point. Thus you can hit home, type 'sudo ' and hit enter.

Offline

#9 2009-09-12 13:00:07

Vintendo
Member
From: Netherlands
Registered: 2008-04-21
Posts: 375
Website

Re: Typing sudo AFTER the command

Square wrote:

You can also use 'sudo !!' if you've entered a command to rerun it with sudo.

+1

!! repeats the last command, so you can have a lot of fun with it. Like !! -r if you forgot that the thing you wanted to delete was a directory

Offline

#10 2009-09-12 14:03:41

bernarcher
Forum Fellow
From: Germany
Registered: 2009-02-17
Posts: 2,281

Re: Typing sudo AFTER the command

Addition:
If you want to have history commands like !! not immediately executed you may put the line

# Do not automatically execute ! commands.
shopt -s histverify

into your .bashrc.
(Of course you can enter this command interctively as well. But it will then be forgotten the next time bash starts.)


To know or not to know ...
... the questions remain forever.

Offline

#11 2009-09-12 20:57:29

A Future Pilot
Member
Registered: 2008-10-17
Posts: 120

Re: Typing sudo AFTER the command

OK, thanks for y'all's help! Does anyone know about the string completion thing?

Offline

#12 2009-09-12 21:02:38

Runiq
Member
From: Germany
Registered: 2008-10-29
Posts: 1,053

Re: Typing sudo AFTER the command

Vintendo wrote:
Square wrote:

You can also use 'sudo !!' if you've entered a command to rerun it with sudo.

+1

!! repeats the last command, so you can have a lot of fun with it. Like !! -r if you forgot that the thing you wanted to delete was a directory

Cool stuff, that. Does it come from readline, or is it implemented in the shell?

Offline

#13 2009-09-12 22:51:48

bernarcher
Forum Fellow
From: Germany
Registered: 2009-02-17
Posts: 2,281

Re: Typing sudo AFTER the command

Runiq wrote:
Vintendo wrote:
Square wrote:

You can also use 'sudo !!' if you've entered a command to rerun it with sudo.

+1

!! repeats the last command, so you can have a lot of fun with it. Like !! -r if you forgot that the thing you wanted to delete was a directory

Cool stuff, that. Does it come from readline, or is it implemented in the shell?

This is part of the bash history functions. See man bash for more.


To know or not to know ...
... the questions remain forever.

Offline

Board footer

Powered by FluxBB