Can I add a command to my bash profile whenever I create a new folder? - bash

I've been creating commands that jet me to my folder I want to work within, and it's great, but...
Could there be a way to add the command automatically whenever I create a new folder?
E.g., I make a folder on my comp, and this gets added to my bash profile:
alias foldername='cd /Applications/MAMP/htdocs/foldername/wp-content/themes/foldername'
I'm on a mac; el capitan.

You can create a function in your .bashrc:
# Changes directory to project in htdocs
function project () {
cd "/Applications/MAMP/htdocs/$1"
}
Once you've sourced the bashrc or started a new shell you can navigate through to sub folders of htdocs using
project FOLDER_NAME

If you are willing to install a newer version of bash (via Homebrew, for instance), you can set the autocd option.
$ shopt -s autocd
$ pwd
/Users/me
$ mkdir foo
$ foo
cd foo
$ pwd
/Users/me/foo
The version of zsh that ships with Mac OS X has an identically named option, if you are willing to change shells.
% setopt autocd
% pwd
/Users/me
% mkdir foo
% foo
% pwd
/Users/me/foo

You could also use CDPATH environment variable for this sort of thing (assuming your target directories live under a common directory (or three).
From the POSIX specification for cd:
CDPATH
A <colon>-separated list of pathnames that refer to directories. The cd utility shall use this list in its attempt to change the directory, as described in the DESCRIPTION. An empty string in place of a directory pathname represents the current directory. If CDPATH is not set, it shall be treated as if it were an empty string.
So if you set CDPATH=.:/Applications/MAMP/htdocs/foldername/wp-content/themes you could then just use cd foldername from anywhere to go to /Applications/MAMP/htdocs/foldername/wp-content/themes/foldername... assuming of course that there wasn't a directory foldername in your current directory (if you wanted it to work for that too you would need to invert the order of the items in CDPATH).

Another approach referenced in the comment is the create a simple function that creates and changes to a directory. Aliased to mdcd it does just that creates and on successful creation changes to the directory. I use it in my ~/.bashrc:
mkdircd ()
{
[ -z "$1" ] && {
printf "usage: mdcd <dir>\n";
return 1
}
mkdir -p "$1" && cd "$1" || {
printf "error: unable to create directory '%s'\n" "$1";
return 1
}
return 0
}
...
alias mdcd='mkdircd'
You can give it a try and see if it fits your needs.

Related

Behavior of 'cd --' (two hyphens)

I know that cd ~- changes directory to $OLDPWD.
I'm using GNU bash, version 4.4.23(1)-release (x86_64-apple-darwin17.5.0) on a Macbook.
'cd --' appears to have the same behavior as 'cd ~-'.
Why?
With Bash -- is used to specify the end of a command options.
So cd -- means cd.
cd without argument change your current directory to your home directory (like cd ~).
The fact it leads you to your last PWD is a coincidence.
That's not correct. cd -- changes to your home directory, just like cd only. Consider cd -- a pure cd with no options and no parameters given. See also https://unix.stackexchange.com/a/11382.

Enter to unix shell with specific working directory

I want to accomplish this:
[zsh]$ pwd
/home/user
*[zsh]$ bash # enter to a bash shell at the same time as `cd ~/Desktop`.
[bash]$ pwd
/home/user/Desktop
[bash]$ exit
[zsh]$ pwd
**/home/user
I would like to know if there is any way to enter to the unix shell at the same time as changing a directory to some specific path. It's important that:
Line * is supposed to be a single-line command for entering a shell and changing a directory,
After exist from any new shell, it's expected to return to the latest location as it was before entering to the shell, see line **.
Using subshells is also useful for changing the current working directory temporary:
% (cd ~/Desktop && bash)
One straightforward answer consists in using a bash configuration file switching to the proper directory. By creating a file ~/.my_bashrc containing a single line:
cd ~/Desktop
you can then just type:
bash --rcfile ~/.my_bashrc
in a terminal to open a new shell directly in the Desktop directory.
Of course you can add other commands in ~/.my_bashrc (aliases, etc.), like in any regular bashrc file.
Simply do this.
cd /home/user/Desktop && bash
This will try to change your current directory to /home/user/Desktop and if it succeeds changes the shell to bash else throws error.
I think answer of #hchbaw is a bit tricky. I've just found a more effective solution from run bash command in new shell and stay in new shell after this command executes. In my case I can use:
bash --rcfile <(echo "cd ~/Desktop")
You can use pushd and popd commands:
pushd ~/Desktop && bash ; popd
pushd in this case is like "remember and cd" - it adds new directory to the top of directory stack, making it current directory. Next you start bash and after you exit bash, popd takes you back to the directory remembered by pushd.
EDIT: changed && to ; for better error handling, as pointed out in comment.

How to create a symlink to open a directory in Terminal on Mac osx?

There are certain folders I am cd'ing into all day long...for instance, "dev" which is in my Documents folder.
I had this bright idea to set up symlinks so I could simply type "dev" and Terminal would cd into that directory. This doesn't seem to work though. My two attempts are below.
Anyone know how to do this or is there a better way?
ln -s /Users/kelly/Documents/Dev/ dev
ln -s 'cd /Users/kelly/Documents/Dev/' dev
$ dev
bash: dev: command not found
With a symlink you can use
ln -s your/dev/directory/ dev
but you can only use it in the directory you created it in and in the form of cd dev.
If you just want to type dev at any point, use an alias:
alias dev="cd your/dev/direcrory/"
(this should then be in your ~/.bashrc)
You could use an alias for dev. Add to your ${HOME}/.bashrc
alias dev='cd /Users/kelly/Documents/Dev/'
and bash correctly parses ~ in an alias:
alias dev='cd ~/Documents/Dev/'
Using an alias eliminates the need for the symbolic link at all.
You have to write cd dev in your case, but it might be better for you to use bash aliases...
Write in your $HOME/.bash_aliases file:
alias dev='cd /Users/kelly/Documents/Dev/'
after opening a new terminal executing dev will give you, what you want...
In your $HOME/.bashrc, declare an array which maps directories to aliases.
declare -A __diraliasmap=(
[dev]="/Users/kelly/Documents/Dev"
[other]="/Users/kelly/Documents"
)
Also define a command_not_found_handle function. If defined, this function will be run by bash when a command is not found. In the function, check whether the command which failed is listed as an alias for a directory and, if so, cd to the associated directory.
command_not_found_handle()
{
if [[ ${__diraliasmap[$1]+set} = set ]]; then
builtin cd "${__diraliasmap[$1]}"
else
false
fi
}

alias to source a bash file from another dir and return to current dir

I am trying to source the Firefox addon sdk. To do so, I must cd into the sdk's dir, then run source bin/activate. If I don't cd into that dir, and source directly from whatever path I am in currently, the following happens:
$ source ~/src/devtools/addon-sdk/bin/activate
Welcome to the Add-on SDK. Run 'cfx docs' for assistance.
$ cfx
-bash: cfx: command not found
I want to have an alias for that, which cd's into the sdk, sources it, then returns to my current directory:
alias acfx='cd ~/src/devtools/addon-sdk && source bin/activate && cd "$(dirname "$0")"'
This sources the sdk correctly, but alas does not return to the directory I invoked the alias:
$ acfx
Welcome to the Add-on SDK. Run 'cfx docs' for assistance.
dirname: illegal option -- b
usage: dirname path
I am lost here, how do I return to the original directory? Or specify a 'working directory' for source?
You can execute cd and subsequent command in a sub-shell like this:
(cd ~/src/devtools/addon-sdk && source bin/activate)
If for some reason you don't want to create sub-shell then use cd - to change dir to the the previous dir:
cd ~/src/devtools/addon-sdk && source bin/activate && cd -
You can use the pushd and popd shell builtins:
alias acfx='pushd ~/src/devtools/addon-sdk && source bin/activate && popd'
The $(dirname "$0") trick only works when invoked from a script; on the prompt, $0 will be bash, so you'd try to return to . (because dirname bash prints .). In your case, I'm guessing $0 is something different; maybe -bash?
You can save the previous path and then go to it:
prev_dir=$(pwd); ... your commands ... ; cd $prev_dir
In your case:
alias acfx='prev_dir=$(pwd); cd ~/src/devtools/addon-sdk; source bin/activate; cd $prev_dir'
I am not curious if this solves your original problem. can you try cd to that dir, source , cd to some other temp dir, and then try cfx ? i mean sourcing should not be different whether done from the current dir, or some specific dir. If the script assumes the user to be present in the current dir, then it's wrong. May be in that case adding export PATH=~/src/devtools/addon-sdk:$PATH in script might help

Autocomplete backslashes in zsh

Is there a zsh script that would allow me to auto complete spaces with backslashes?
For example:
assume there is a folder called "My folder" with a space in between.
If I want to get inside, I
cd My\ folder
However, I want a way to type
cd My folder
and zsh will automatically know to put a backslash so I don't have to.
A little late on the answer, but this should allow you to do what it is you're looking to do as well as retain the normal behavior for the cd command and also take you to your ~ directory if no directory is given.
Add to your ~/.zshrc file:
function cd() {
new_directory="$*";
if [ $# -eq 0 ]; then
new_directory=${HOME};
fi;
builtin cd "${new_directory}"
}
However your ZSH should have autocompletion built in, in which case you can enter the first few letters of the directory that you're trying to cd into, then hit tab and it should complete the directory name or give a list of directories if more than one matches.
You could Make an alias with "Myfolder" and run that in a zsh script.
alias -g "Myfolder"="My\ folder" ---> cd Myfolder
I don't know if there is a way to make it possible with having the space in between the words. If it has to be my "My folder" then I don't know that I can help you.

Resources