Isn't there a more intuitive way of dealing with the directory stack than what is possible with pushd
and popd
?
I find the CLIs of the pushd
and popd
built-in commands presented to the user kind of mind-boggling. But at the same time I would love to make frequent use of the directory stack presented by the dirs
built-in command.
So it took me quite a long time to figure out use cases and then to memorize the following ones:
pushd
- toggle the two directories from the top.pushd +1
- change to the second from top directory.pushd -0
- change to the bottom from top directory.popd +0
- pop the top changing to the second from top directory.pushd -0; popd +1
- change to the bottom from top directory and pop the former top.Still having to type them each time just to juggle with the directory stack feels way to heavy to me. I even can't come up with mnemonic names to alias those five use cases.
Any insights?
Recently I discovered the power of GNUs readline library and the short cuts in terms of key strokes it can provide to the user on Bash's command line.
I therefore defined the following hot keys in my ~/.inputrc
file. GNU readline's configuration file:
# "pushd" use case.
"\C-^": "\C-a\C-kpushd\C-m"
# "pushd +1" use case.
"\e<": "\C-a\C-kpushd +1\C-m"
# "popd +0" use case.
"\ex\e<": "\C-a\C-kpopd +0\C-m"
# "pushd -0" use case.
"\e>": "\C-a\C-kpushd -0\C-m"
# "pushd -0; popd +1" use case.
"\ex\e>": "\C-a\C-kpushd -0; popd +1\C-m"
I associate the hot keys from above as follows:
<ctrl>-^
- vim
hot key to toggle the current with the previous buffer.<alt>-<
- Shift left and carry.<alt>-x<
- Shift left and crop.<alt>->
- shift right and carry.<alt>-x>
- shift right and crop.The <ctrl>-ak
key strokes position the cursor at the beginning of the line (a) and delete (k) it. This is important because when <ctrl-m>
(enter) is hit chances are that the user hit the hot key while in the middle of a sentence. The hot key value would get garbled with the current user input and bash couldn't execute it.