Tom was in search of a faster way of typing “cd ../../..” and friends. He wrote a bash function that took a numeric argument. So with his script:
cd ../../..
becomes
xs 3
which is a reduction from 12 keypresses, including “return”, down to 5.
I felt that there was more to be done here. I had a feeling something could be done with readline. So I had a look around the readline documentation (see “info readline”) and found how to define macros for it. Then I stuck this in my readline inputrc file:
"\C-x1": "cd ../\n" "\C-x2": "cd ../../\n" "\C-x3": "cd ../../../\n" "\C-x4": "cd ../../../../\n" "\C-x5": "cd ../../../../../\n" "\C-x6": "cd ../../../../../../\n" "\C-x7": "cd ../../../../../../../\n" "\C-x8": "cd ../../../../../../../../\n" "\C-x9": "cd ../../../../../../../../../\n"
Looks ugly doesn’t it?! I couldn’t find a way of using a numeric argument within a macro, so I resorted to generating this above list with this Python:
for x in range(1,10): print '"\C-x%i": "cd %s\\n"'% (x, "../" * x)
So now with this, those 12 keypresses are further reduced to 3: Control-x 3. Note that the “enter” key doesn’t get pressed.
How to use
To use that, one needs to get it into an rc file for readline. On Fedora 10, this involves editing your “~/.bashrc” file to contain:
export INPUTRC="~/.inputrc"
Then sticking the following in “~/.inputrc”:
"\C-x1": "cd ../\n" "\C-x2": "cd ../../\n" "\C-x3": "cd ../../../\n" "\C-x4": "cd ../../../../\n" "\C-x5": "cd ../../../../../\n" "\C-x6": "cd ../../../../../../\n" "\C-x7": "cd ../../../../../../../\n" "\C-x8": "cd ../../../../../../../../\n" "\C-x9": "cd ../../../../../../../../../\n" $include /etc/inputrc
It feels like this should be possible to make this neater by hooking into bash/readline in some way.
Site by Rob Gilton. © 2008 - 2019