Better Directory Handling in Bash
When dealing with directories in bash, some obvious things seem like too much work…
Here are 2 quick tips for easier handling of directories in bash.
unix-filename-rubout
When dealing with long directory names, you can usually complete forward by pressing TAB
.
But going backward? … that usually means pressing BACKSPACE
repeatedly.
There is a better way: unix-filename-rubout
. Add this to your ~/.inputrc
:
What does it do?
(TAB forward, ctrl-f backward)
Every time you hit ctrl-f
, it deletes backward until the previous /
: useful!
Why ctrl-f
?
By default, in bash, it’s not mapped to anything. And it’s easy to type. You
can check your bindings with bind -P
:
(detour: use bind -P
to see what is bound to each ctrl
keys)
cd to filename
What usually happens if you try to cd to a filename?
(this happens often when copying-pasting path)
For years, I swore at bash, manually deleted the last part of the path, and
tried again. In fact, that’s what drove me to find unix-filename-rubout
in the
first place.
There’s no built-in fix for this, but it’s not too complicated to wrap the cd
in a function
and add some extra quality-of-life logic in there:
in short: if the path is a filename, use the directory containing the file instead!
Conclusion
When using a shell, it’s often the “little things” that turn chores into seamless experiences.
Give those a try and let me know about your own tweaks.