How to quickly store and access often used commands?
Another tip: I sometimes use comments to bookmark/tag a command:
my_command #bookmark
then:
[ctrl-r]#bookmark
I find very useful the following readline commands
history-search-backward,
history-search-forward
(be aware they are different from the usual reverse-search-history
, forward-search-history
, tied to Ctrl-R, Ctrl-S).
I have these commands associated to Ctrl-Up and Ctrl-Down putting the following lines into ~/.inputrc
:
"\e[1;5A": history-search-backward
"\e[1;5B": history-search-forward
How they work: write few chars of the beginning of the command, press Ctrl-Up and the next older command starting with that prefix will be shown, press again to see the next, and so on. When you are satisfied, after possibly modifying the command, press Enter to execute.
Use 'alias'
alias
is a great tool for this.
- You can easily declare one on the command line to be used during the current shell session.
- If you'll use it in the future, you can add it to your shell config.
When you use an alias, it's exactly as if you'd typed it, so it's quite flexible. For instance, you can use it with pipes:
alias findfoo="grep 'foo'"
echo 'foo1 \n foo2 \n bar1 \n bar2 \n foo3' | findfoo # Does your custom grep
You should be able to do "slight variations" by passing any flags you didn't already specify.
echo 'foo1 \n foo2 \n bar1 \n bar2 \n foo3' | findfoo -v # finds non-matches