Hacker News new | past | comments | ask | show | jobs | submit login

Cool idea. For those systems where you don't have the option of adding a 3rd party tool, it's probably worth knowing some of the standard bash shortcuts. The easy to remember ones are:

  !^ - the first argument from the previous command
  !$ - the last argument from the previous command
  !* - all of the arguments from the previous command
  !! - the entire previous command
For example:

  $ apt-get install foo
  # Crap
  $ sudo !!



Let us not forget, also substitution regexs in bash...

    $ ls -l /tmp/foo
    ls: cannot access /tmp/foo: No such file or directory
    $ !:s/foo/bar
    -rw-rw-r-- 1 user user 0 Apr 17 19:12 /tmp/bar


And the shorthand:

    $ ls -l /tmp/foo
    ls: cannot access /tmp/foo: No such file or directory
    $ ^foo^bar
    -rw-rw-r-- 1 user user 0 Apr 17 19:12 /tmp/bar


And the reason for the other approach - global substitution (otherwise the caret approach is simpler)

    $ ls
    hello_world hi_there_world
    $ mv hello_world hello_everyone
    $ !!:gs/hello/hi_there
    mv hi_there_world hi_there_everyone
    $ ls
    hi_there_world hi_there_everyone


Nice. Now that we're getting all stackoverflowy in here, it would be worth mentioning two other handy tips.

Preventing typo mistakes with bash autocomplete is even better than correcting mistakes afterwards, it also makes typing so much faster. It works with commands AND it also works for files inside the current working directory.

Hint: start typing your command and then hit TAB, voilà! Note: if there are more than 1 command/file option that starts with the same chars you wrote, hit TAB twice and you will have all the options displayed in front of you, it comes in handy to remember fast instead of browsing documentation.

Command example:

$ git rem[TAB] ---> $ git remote

$ git remote a[TAB] ---> $ git remote add

File example:

$ git add i[TAB] --> $ git add index.js

Multiple options example (TAB twice):

$ git r[TAB,TAB]

(will give you the options:)

rebase relink replace revert reflog remote request-pull rm release repack reset

The result is NO TYPOS! ;)

--------------------------

It would also be worth remembering the unix convention of using $ for regular user prompt and # for super user prompt.

In other words:

$ <--- if this is your prompt you're working on a regular user session.

# <--- if this is your prompt you're working on a super user session and you don't need sudo.

Again, that is just a convention, your OS/distro could be different.


When I was typing the '#' I was thinking 'comment', not 'root shell'; in retrospect I can see how it might have been confusing, especially when my example used 'sudo'.


I was not criticizing your use of # at all. Using # to comment is perfectly fine I guess. Now that you mentioned it, it might be a bit confusing for newbies, but don't worry I don't think anyone would try to send the command 'Crap' over a super user session, although why the hell not? haha


BTW, all of those commands are from the original BSD c-shell. Bash developers knew a good thing when they saw it.


Typo correction is useful too.

^foo^bar

git brnch

^r^ra

This works on all Unix machines


*Works if properly implemented. Had to manually disable this behavior in eshell as it prevented you from running regex with two carets, e.g. '$ grep -E '^sometext[^ ]+moretext'.

You can also just run a sed style substitution in most shells, e.g. '$ !!:s/before/after/' or '$ !!:gs/x/y/', which is safer IMO.


You know... I've seen this notation on discussion forums (and probably IRC etc.) for years, and had no idea until now that it was a reference to this shell capability lol


I use $_ all the time, since it's the last argument to the previous command [after expansion]. Example would be:

atom path/to/so[tab]/fi[tab] [return] git diff $_

Complete reference: https://www.gnu.org/software/bash/manual/html_node/Variable-...

See also: http://wiki.bash-hackers.org/syntax/shellvars


You can also specify a specific argument number from the previous command with colon notation e.g. sudo dpkg -l !:2


alt-. (Alt-period) is a good shortcut to keep in mind. On first press, it inputs the last arg from the previous command. If you press alt-. again, it inputs the last arg from the previous-to-previous command. And so on.


Huh, I didn't know about that. I use Esc+Period to the same effect.


I always read `sudo !!` as `suddoooooooooooooooooo... aaargghhh!!!`


I've been using "sudo !!" (which I pronounce as sudo bangbang in my head) for a while now, mostly with vim for editing stuff in /etc. It's very nifty.

Another nifty tool is vi editing mode. You run "set -o vi", and now your shell takes vi modal editing commands. That along with Ctrl-r for reverse history search has made life in the shell so much easier. Bash has a ton of little stuff like that built in. Zsh has a few more.


Probably my biggest overall timesaver has been:

   :w !sudo tee %
If you neglected to sudo before making your changes.


I've had this in my vimrc for years:

   " Let :w!! gain sudo privileges without closing and reopening vim
   cmap w!! w !sudo tee % >/dev/null


Its much more easier to remember when you know how that actually works. Here is link explaining how that actually works:

http://stackoverflow.com/questions/2600783/how-does-the-vim-...


Yes, that vi editing mode of bash is very useful. IIRC it was there in ksh too. Some people might not know that you can use both the insert/append mode and the command mode of vi(m) in that vi editing mode of bash, to edit previously entered commands. You just switch between the two modes the same way as you do in the real vi(m), with ESC to go from insert/append mode to command mode, and i or a to go from command mode to insert/append mode.

Also, after you have entered a previous command, to edit it, or any previous command in the history, the first thing you have to do is press ESC (to go to command mode) and then k, thereby bring the last entered command into view for editing.

When editing a command, but in command mode (not insert/append mode), you can also press v to open the command in vi(m) in a temporary file, then have the full screen editor at your disposal to edit the command (useful for long scripts including for/while loops or long pipelines), and then when you save and exit (:wq or :x followed by Enter), the newly edited command gets executed by bash.

Also, this editing technique is useful not only for correcting typos in commands, but for modifying a previous correct command, to change some words in it and then re-issue it.


I also use that all the time, and pronounce it as "sudo", but with more excitement.


sudo !! is my favorite thing. I named my cat sudo bangbang.


I don't see the advantage over pressing up, C-a and typing sudo


Historically: it predates the ability to do interactive line-editing.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: