9 ms·
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
by csandreasen 11y ago
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 !!
- melling 11y agoTypo correction is useful too. ^foo^bar git brnch ^r^ra This works on all Unix machines
- stewartbutler 11y ago*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.
- lectrick 11y agoYou 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
- stox 11y agoBTW, all of those commands are from the original BSD c-shell. Bash developers knew a good thing when they saw it.
- ferrari8608 11y agoI'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.
- dogecoinbase 11y agoProbably my biggest overall timesaver has been: :w !sudo tee % If you neglected to sudo before making your changes.
- mattikus 11y agoI'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
- mkagenius 11y agoIts 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-write-with-sudo-trick-work http://stackoverflow.com/questions/2600783/how-does-the-vim-...
- jpwright 11y agoI also use that all the time, and pronounce it as "sudo", but with more excitement.
- vehementi 11y agosudo !! is my favorite thing. I named my cat sudo bangbang.
- drone 11y agoLet 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
- tsm 11y agoAnd 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
- willthames 11y agoAnd 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
- more_corn 11y agoYou can also specify a specific argument number from the previous command with colon notation e.g. sudo dpkg -l !:2
- seanp2k2 11y agoI 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-Index.html https://www.gnu.org/software/bash/manual/html_node/Variable-... See also: http://wiki.bash-hackers.org/syntax/shellvars http://wiki.bash-hackers.org/syntax/shellvars
- jorgearturo 11y agoNice. 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.
- csandreasen 11y agoWhen 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'.
- jorgearturo 11y agoI 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
- awalGarg 11y agoI always read `sudo !!` as `suddoooooooooooooooooo... aaargghhh!!!`
- saheel1511 11y agoalt-. (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.
- Zecc 11y agoHuh, I didn't know about that. I use Esc+Period to the same effect.