News for hackers. Stuff that matters.
Re:The Fuck – Correct your previous console command by csandreasen on Friday April 17, 2015 @04:22PM (#9396975)
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 !!
Re:The Fuck – Correct your previous console command by drone on Friday April 17, 2015 @08:13PM (#9397858)
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
Re:The Fuck – Correct your previous console command by tsm on Friday April 17, 2015 @10:55PM (#9398165)
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
Re:The Fuck – Correct your previous console command by willthames on Saturday April 18, 2015 @12:59AM (#9398369)
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
Re:The Fuck – Correct your previous console command by jorgearturo on Saturday April 18, 2015 @07:23AM (#9399051)
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.

Re:The Fuck – Correct your previous console command by csandreasen on Saturday April 18, 2015 @08:45AM (#9399247)
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'.
Re:The Fuck – Correct your previous console command by jorgearturo on Saturday April 18, 2015 @11:21AM (#9399729)
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
Re:The Fuck – Correct your previous console command by stox on Friday April 17, 2015 @04:32PM (#9397043)
BTW, all of those commands are from the original BSD c-shell. Bash developers knew a good thing when they saw it.
Re:The Fuck – Correct your previous console command by melling on Friday April 17, 2015 @04:31PM (#9397035)
Typo correction is useful too.

^foo^bar

git brnch

^r^ra

This works on all Unix machines

Re:The Fuck – Correct your previous console command by stewartbutler on Friday April 17, 2015 @05:56PM (#9397409)
*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.

Re:The Fuck – Correct your previous console command by lectrick on Friday April 17, 2015 @10:21PM (#9398097)
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
Re:The Fuck – Correct your previous console command by seanp2k2 on Saturday April 18, 2015 @01:13AM (#9398385)
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

Re:The Fuck – Correct your previous console command by more_corn on Friday April 17, 2015 @09:54PM (#9398070)
You can also specify a specific argument number from the previous command with colon notation e.g. sudo dpkg -l !:2
Re:The Fuck – Correct your previous console command by saheel1511 on Saturday April 18, 2015 @05:59PM (#9401230)
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.
Re:The Fuck – Correct your previous console command by Zecc on Sunday April 19, 2015 @08:42AM (#9402934)
Huh, I didn't know about that. I use Esc+Period to the same effect.
Re:The Fuck – Correct your previous console command by awalGarg on Saturday April 18, 2015 @10:07AM (#9399488)
I always read `sudo !!` as `suddoooooooooooooooooo... aaargghhh!!!`
Re:The Fuck – Correct your previous console command by ferrari8608 on Friday April 17, 2015 @06:09PM (#9397459)
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.

Re:The Fuck – Correct your previous console command by dogecoinbase on Friday April 17, 2015 @07:04PM (#9397679)
Probably my biggest overall timesaver has been:

   :w !sudo tee %
If you neglected to sudo before making your changes.
Re:The Fuck – Correct your previous console command by mattikus on Friday April 17, 2015 @08:24PM (#9397891)
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
Re:The Fuck – Correct your previous console command by mkagenius on Saturday April 18, 2015 @03:02AM (#9398565)
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-...

Re:The Fuck – Correct your previous console command by vram22 on Saturday April 18, 2015 @04:49AM (#9398767)
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.

Re:The Fuck – Correct your previous console command by jpwright on Friday April 17, 2015 @09:15PM (#9397997)
I also use that all the time, and pronounce it as "sudo", but with more excitement.
Re:The Fuck – Correct your previous console command by vehementi on Saturday April 18, 2015 @02:57AM (#9398554)
sudo !! is my favorite thing. I named my cat sudo bangbang.
Re:The Fuck – Correct your previous console command by okasaki on Saturday April 18, 2015 @04:15AM (#9398700)
I don't see the advantage over pressing up, C-a and typing sudo
Re:The Fuck – Correct your previous console command by LukeShu on Saturday April 18, 2015 @12:08PM (#9399902)
Historically: it predates the ability to do interactive line-editing.