14

How to take advantage of the clumsy and popd fatter with the built-in Bash dirs...

 3 years ago
source link: https://www.codesd.com/item/how-to-take-advantage-of-the-clumsy-and-popd-fatter-with-the-built-in-bash-dirs-control.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to take advantage of the clumsy and popd fatter with the built-in Bash dirs control

advertisements

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:

  1. pushd - toggle the two directories from the top.
  2. pushd +1 - change to the second from top directory.
  3. pushd -0 - change to the bottom from top directory.
  4. popd +0 - pop the top changing to the second from top directory.
  5. 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:

  1. <ctrl>-^ - vim hot key to toggle the current with the previous buffer.
  2. <alt>-< - Shift left and carry.
  3. <alt>-x< - Shift left and crop.
  4. <alt>-> - shift right and carry.
  5. <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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK