News for hackers. Stuff that matters.
Re:Run last command as root by chopsueyar on Thursday December 16, 2010 @11:51AM (#2012624)
sudo !! runs the previous line as root, in case you forgot to type sudo
Re:Run last command as root by jrockway on Thursday December 16, 2010 @11:58AM (#2012653)
More like "sudo !!" prepends sudo to the last history entry and executes the result. It may not run as root, depending on the complexity of the shell handiwork you did, and depending on /etc/sudoers.

I really, really, really hope that the author doesn't think "sudo !!" is some special kind of magic, and that he knows "echo !!" works the same way.

Also, there are keyboards with a "Home" key? Most people just press C-a.

Re:Run last command as root by xenophanes on Thursday December 16, 2010 @12:06PM (#2012697)
Do you know where I can find a list of hotkeys along the lines of C-a? I personally created my own hotkey (shift left arrow) but learning standard ones would probably be good.
Re:Run last command as root by pyre on Thursday December 16, 2010 @12:32PM (#2012829)
Most shells default to emacs keybindings:

  C-a beginning of line
  C-k kill text from cursor to end of line
  C-e end of line
  M-b move cursor backwards by a word
  M-f move cursor forwards by a word
  C-w delete backwards by a word
  C-h backspace (great when real backspace doesn't work)
  C-r reverse search through command history
(C-w works in Vim(insert mode) and Emacs, but kills me when I try to type it in a browser window to delete a word... Good-bye window and/or tab)

In zsh I actually have M-w bound to the normal backspace-by-a-word and C-w bound to the vim version which will do things like count '/' as a word separator so that I can easily backspace through sections of a path rather than be required to delete the whole thing (or nothing).

Re:Run last command as root by BrandonM on Thursday December 16, 2010 @02:27PM (#2013246)
> (C-w works in Vim(insert mode) and Emacs, but kills me when I try to type it in a browser window to delete a word... Good-bye window and/or tab)

Control-Shift-T usually solves that problem for me in Firefox.

Re:Run last command as root by xenophanes on Thursday December 16, 2010 @12:40PM (#2012873)
Hmm, FYI most of those work in the safari reply box. I think Apple put them for all Cocoa text fields not just terminal :-)
Re:Run last command as root by uxp on Thursday December 16, 2010 @02:19PM (#2013221)
> I think Apple put them for all Cocoa text fields not just terminal :-)

It is the NSTextField that accepts those commands. The prefix NS stands for NeXTSTEP, so the functionality existed long before Cocoa.

Re:Run last command as root by pyre on Thursday December 16, 2010 @11:58PM (#2015239)
Yea, but you have to hit Ctrl+Option or Cmd+Option (can't remember which off the top of my head) for Meta though, instead of just Option (which is what most people bind to Meta in Terminal.app or iTerm.app).
Re:Run last command as root by jrockway on Thursday December 16, 2010 @12:09PM (#2012718)
If you use bash, read the "Command Line Editing" section of "info readline".
Re:Run last command as root by telemachos on Thursday December 16, 2010 @01:36PM (#2013076)
You can also tweak the behavior of readline (for Bash and other applications) and custom make your own key combinations by editing your ~/.inputrc file. A random example from mine:

    # Insert double quotes & set cursor between them
    "\C-x\"": "\"\"\eb"
    #
    # Insert single quotes & set cursor between them
    "\C-x'": "''\eb"
See also the Bash manual:

http://www.gnu.org/software/bash/manual/bashref.html#Command...

Re:Run last command as root by chopsueyar on Thursday December 16, 2010 @12:01PM (#2012672)
Yeah, I first tried...

grep ".jpg"

^C

ls | !!

...that worked too. Cool command to learn.

Thanks for the clarification, though. Original article was lacking.

Re:Run last command as root by hardik988 on Thursday December 16, 2010 @12:05PM (#2012689)
Hi, I'm the author (admittedly still quite new to Unix). I did play around with the !! and realized it does a lot more. Thanks for clarifying though!

Edit: Typos

Re:Run last command as root by humblest_ever on Thursday December 16, 2010 @12:35PM (#2012842)
It's very powerful. There's more than just the !! for history, also. Check out http://www.catonmat.net/blog/the-definitive-guide-to-bash-co... .
Re:Run last command as root by joevandyk on Thursday December 16, 2010 @12:00PM (#2012670)
I'd hope that most people just hit "<esc> I sudo !!<CR>".
Re:Run last command as root by chopsueyar on Thursday December 16, 2010 @12:05PM (#2012693)
You said Esc-0, first.