!^ - 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 !! !^ - 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 !! $ 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 $ 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 $ 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_everyonePreventing 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.
^foo^bar
git brnch
^r^ra
This works on all Unix machines
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.
atom path/to/so[tab]/fi[tab] [return] git diff $_
Complete reference: https://www.gnu.org/software/bash/manual/html_node/Variable-...
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.
:w !sudo tee %
If you neglected to sudo before making your changes. " Let :w!! gain sudo privileges without closing and reopening vim
cmap w!! w !sudo tee % >/dev/nullhttp://stackoverflow.com/questions/2600783/how-does-the-vim-...
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.