Superpowered cd

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
shawnhcorey
Level 4
Level 4
Posts: 217
Joined: Thu Jun 17, 2010 11:23 am
Location: The Great White North
Contact:

Superpowered cd

Post by shawnhcorey »

How to superpower cd:

1. Edit your ~/.bash_aliases and add this line:

Code: Select all

export CDPATH=.:~/.cdpath

2. Source the aliases.

Code: Select all

. ~/.bash_aliases
3. Create the directory ~/.cdpath

4. Change to the directory and add links to directories you want cd to do a quick jump to.

Code: Select all

cd ~/.cdpath
ln -s ~/bin bin
Now you can quickly change to your bin from any other directory:

Code: Select all

cd bin
Last edited by LockBot on Sun Dec 10, 2023 11:00 pm, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Don't stop where the ink does.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Superpowered cd

Post by Termy »

That's a really cool feature of BASH. My only reservation is that this non-standard approach could lead to confusing and potentially disastrous results, especially if someone else uses your computer. I would put the exported variable in '~/.bashrc', as it's not an alias and not everyone will have a sourced '~/.bash_aliases' file. An alias is something that uses the alias shell builtin.

My preference is to add a cd alias for a given path:

Code: Select all

alias sd="cd /media/$USER"
Well, technically, I use functions and have aliasing completely disabled, but I used to use the above alias. I can see this $CDPATH feature being useful when in a Git repository, where your BASH prompt could set this parameter for when you're in a repository, allowing you to quickly jump around. Then again, the same caution as above applies here too, perhaps more so.
I'm also Terminalforlife on GitHub.
mikeflan
Level 17
Level 17
Posts: 7145
Joined: Sun Apr 26, 2020 9:28 am
Location: Houston, TX

Re: Superpowered cd

Post by mikeflan »

not everyone will have a sourced '~/.bash_aliases' file.
I agree. I don't have a '~/.bash_aliases' file on my LM 20.3. Not sure why.

I think the bash alias idea is a good one. I might not need it since I have a home-grown program to start a terminal on almost any directory I normally work with. OTOH it might help me out since these newly spawned terminals don't always have the bash history I would like them to have.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Superpowered cd

Post by Termy »

mikeflan wrote: Sun Jun 11, 2023 7:48 am [...]
I'm not even biased against the file, as I've used one for a few years. :D It's nice to have functions and aliases spread out in different files, especially if you have quite a bulky BASH setup. Now everything like this and more is just in '~/.bash_functions' and the regular BASH stuff in '~/.bashrc'. To each their own, of course. BTW, while these filenames coincide with similar BASH files, it's not listed in BASH's man page as core functionality, so from that I can infer that we shouldn't assume this file is a given or be surprised if it's not present — unless this has since changed.
I'm also Terminalforlife on GitHub.
mintician
Level 3
Level 3
Posts: 110
Joined: Tue May 02, 2023 3:48 am

Re: Superpowered cd

Post by mintician »

Linux Mint 21.1 Cinnamon
Locked

Return to “Scripts & Bash”