How to make zsh always show environment variable name instead of the path?

2

I have an environment variable set to my projects folder like this:

PROJ=$HOME/projects

When I want to open a project folder i just type PROJ/project_name and the shell opens the directory and my prompt looks like this:

~PROJ/project_name

When I opens a new tmux window, it opens a new shell with the prompt showing the full path like this:

~/projects/project_name

But I wanted it to keep the variable name on the prompt.

How can I configure tmux / zsh (I'm also using oh-my-zsh) to keep using the environment variable when I create new windows?

Dimas Kotvan

Posted 2015-04-06T13:07:41.290

Reputation: 137

Answers

1

You need to enable the AUTO_NAME_DIRS option in your Zsh configuration

setopt autonamedirs

it has to happen before you set PROJ.

Alternatively, if you do not need PROJ for anything other than switching (and displaying) paths in Zsh, you can set

hash -d PROJ=$HOME/project

Explanation:

The feature you are using is called "Static named directories". Usually named directories need do be called with a ~ followed by the name of a shell parameter whose value begins with a /, PROJ in your case.

If CDABLE_VARS is enabled (which Oh-My-Zsh does by default) the ~ is not really required. That is why you can use PROJ/project_name instead of ~PROJ/project_name. (but it would fail, if there were a directory with the actual name PROJ).

As Oh-My-Zsh also enables AUTO_CD you do not even need to use cd. If a command cannot be executed and matches the name of a directory, Zsh will cd to that directory.

With all named directories lookups may happen in two directions

  1. does a given name point to a directory (e.g. does a parameter contain a path starting with /)?
  2. does the current directory have a name?

While the first kind of lookup happens automatically when an argument starts with ~ (or in some cases and enabled CDABLE_VARS even without), the second kind (which is used for the prompt) requires the directory to be listed in the directory hash table (hash -d for a listing of that table). On a freshly started Zsh this hash table is usally empty. It will then be filled with data acquired when doing ~ expansions.

In your original shell PROJ has been successfully expanded to $HOME/projects and so the directory hash table now contains PROJ=$HOME/projects (where $HOME is replaced with your actual home directory path). Zsh can now look it up for its prompt. When you start tmux, a new shell is started and the directory hash table is again empty, thus the name is not replaced in the prompt.

With AUTO_NAME_DIRS an entry in the directory hash table is created immediately when a parameter is set to a value that starts with / (or it is removed if the new value does not start with /). You can also add manually to the directory hash table with hash -d NAME=PATH.

Adaephon

Posted 2015-04-06T13:07:41.290

Reputation: 3 864

It's working now. Thanks for the complete response. – Dimas Kotvan – 2015-04-16T16:53:59.363