Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

To define what I mean by "custom path identifier", I mean a short set of character(s) that represent a whole path, and example of this would be the ~, if you type cd ~ in your Terminal Emulator it will go to your home folder. But I'd like to make a custom one for a different path. I looked in the .bashrc folder for hints on how to do this, but I could find anything unfortunately.


The "custom path identifier" I'm trying to make is ~~ to /home/myuser/Applications/Custom/Java/. Since I'm going to be using that path a lot ( and daily) I don't want to keep retyping the whole path, and just typing ~~ would be nice. I tried using the alias command:

alias ~~="/home/myuser/Applications/Custom/Java/"

But, unfortunately this did not work.


On the side note, I apologize if I used the wrong terminology for anything, I'm fairly new to all this Linux stuff and I know very little. So thanks for any extra information I get.

share|improve this question
3  
You can use alias ~~="cd /home/myuser/Applications/Custom/Java/" and you can execute ~~ to cd to /home/myuser/Applications/Custom/Java/ –  Nischay Jul 2 at 7:52
    
@Nischay, That is a very good solution. But it doesn't answer the question "How do I make a custom path identifier?". Thanks though! –  Xero Jul 2 at 7:55
    
In zsh, you get just the syntax you proposed - named dirs are handeled like user directories, especially in the shell prompt. (See my answer) –  Volker Siegel Jul 2 at 14:54

3 Answers 3

up vote 7 down vote accepted

The tilde (~) has a special meaning in bash, is an expansion of bash and takes the value of $HOME environment variable. Read Tilde Expansion section from GNU Bash Reference Manual to understant how it wors. So, I think that what you want to accomplish is something similar to:

myuser@ubuntu:~$ pushd -n "/home/myuser/Applications/Custom/Java"
~ ~/Applications/Custom/Java
myuser@ubuntu:~$ cd ~1
myuser@ubuntu:~/Applications/Custom/Java$

Read man pushd for more info.

Next, to go directly inside of /home/myuser/Applications/Custom/Java everytime when you will run cd ~1 command in your terminal just add the following line to the end of your ~/.bashrc file:

pushd -n "/home/myuser/Applications/Custom/Java" >/dev/null

Reload your ~/.bashrc file with source ~/.bashrc and then use cd ~1 everytime when you want to change the current working directory to /home/myuser/Applications/Custom/Java.

share|improve this answer
1  
Brilliant solution, but it works only if you do not perform popd up to empty the directory stack. At that point needs to be recharged file .bashrc or rerun the pushd command. –  girardengo Jul 2 at 9:56
3  
@girardengo This can be solved also by adding the following alias in ~/.bash_aliases file: alias popd='popd && pushd -n "/home/myuser/Applications/Custom/Java" >/dev/null' –  Radu Rădeanu Jul 2 at 10:14

In zsh this is dead easy, because you don't need to write cd explicitly, you can simply create an alias like this:

alias x=/some/path

Add this to your .zshrc (or whatever file you will source on the zsh startup) and you're ready to go. Now you can jump to this directory by calling the alias, namely x in this example.

share|improve this answer
    
Use cdable_vars (see my answer), and you can just set a variable, not an alias (which is a command, it not a zsh global alias): x=/some/path. Works with bash too! –  Volker Siegel Jul 2 at 14:44

You could use the shell option "cdable_vars"

shopt -s cdable_vars

Also, take a look at CDPATH,
and the shell option "autocd"

For the description of how CDPATH works, see the cd section in the man page of bash

man bash | less -p 'cd \['

The solutions work for both bash and zsh.


If you use zsh, you can use global aliases to replace an identifier everywhere in the command line, not just as command.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.