Some BASH CLI Convenience Settings that make your life easier
Working in BASH CLI can be really a pleasure. My BASH configuration (.bash_profile & .bashrc) provides, among others, the following frequently-used and really handy features (for more tips on using BASH as a CLI, please read http://lgfang.github.io/images/../mynotes/util/shell-cli.html):
Informative PS1
As shown in the picture above, you will get a PS1 which presents the following information:
- Event number: you can re-run previous command line by
!<event number>
, for example!1063
or select only the first argument like this:!1063:1
- Timestamp: gives you a hint of when previous command ends.
- MY_WARN/MY_EXTRA: you may set these two variables to show extra information and/or error messages as you want. For me, I often use MY_WARN indicate if I have source in certain harzadous configure file.
- Current git branch: if you are in a git repository, this tells you the current branch.
Search and re-run previous command which starts with xxx
For examle, just type grep a
and then repeat <Alt>-p
, you will be able to
see and select previous commands that starts with grep a
.
Note that for putty users, right <Alt>
does NOT work since it is actually
<AltGr>
).
This is superior than traditional history search in two ways:
- You can launch the search after you have already typed something.
- It matches only the commands that start with the string, which is what we want most of time.
Bookmark directories
You can bookmark frequently accessed directories and jump to them at ease
with commands dm
, dj
, and lsdm
from me.
cd recent directories (i.e. "cd +n")
The history of my "cd" is recorded and can be easily re-applied. This is
achieved by a BASH function cd
written by me based on the built-in pushd
.
cd path substitution (i.e. "cd from to")
This is inspired by KSH and is also implemented in my cd
function.
cd up multiple levels (i.e. "cd ….")
Inspired by eshell(Emacs), implemented in my cd
function.
Automatically attach to or create a tmux session.
Tmux is an awesome terminal multiplexer (see this post for more introduction). I use it practically every day and strongly recommend you give it a shot.
tg
is a bash function written by me to allow me to type less.
Run tg
without any argument, you will be prompted to select one session
from existing sessions. If there is no session at all, a session named "misc"
will be created and attached to without prompt.
Run tg name
will attach to the session named "name". If such session does
not exist, it will be created first.
Please note that tg
runs tmux with option -2
, i.e. force tmux to assume
the terminal supports 256 colours.
Push to gerrit (for code review)
Run gerrit
instead of git push refs/for/<current_branch_name>
to submit
code to gerrit for review.
Or, gerrit branch_name
to push to a branch other than current branch for
review.
Go to current path of emacsclient
If you use emacs in *nix, you shall be using emacs daemon/client. If you use emacsclient, you may find this command handy when you need it. IIRC, this is inspired by coldnew.
blog comments powered by Disqus