Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zsh shorten length of current path

Tags:

macos

prompt

zsh

Whenever I navigate deeper into a directory, zsh displays the full path in the prompt.

Instead of displaying

x@y:~/i/am/a/really/really/really/really/long/path/somewhere

I would rather like to have

x@y:~/path/somewhere

How can I achieve this?

I'm using zsh with iTerm on OSX Yosemite 10.10.4.

EDIT:

Here is my bashrc-file:

  1 # System-wide .bashrc file for interactive bash(1) shells.
  2 if [ -z "$PS1" ]; then
  3    return
  4 fi
  5
  6 PS1='\h:\W \u\$ '
  7 # Make bash check its window size after a process completes
  8 shopt -s checkwinsize
  9 # Tell the terminal about the working directory at each prompt.
  10 if [ "$TERM_PROGRAM" == "Apple_Terminal" ] && [ -z "$INSIDE_EMACS" ]; then
  11     update_terminal_cwd() {
  12         # Identify the directory using a "file:" scheme URL,
  13         # including the host name to disambiguate local vs.
  14         # remote connections. Percent-escape spaces.
  15         local SEARCH=' '
  16         local REPLACE='%20'
  17         local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
  18         printf '\e]7;%s\a' "$PWD_URL"
  19     }
  20     PROMPT_COMMAND="update_terminal_cwd; $PROMPT_COMMAND"
  21 fi
like image 781
sqe Avatar asked May 19 '15 11:05

sqe


People also ask

How do I shorten the current directory path shown on terminal?

To change it for the current terminal instance only Just enter PS1='\u:\W\$ ' and press enter.

How do I shorten a directory path?

If you want to shorten a relative path that seems too long, you'll have to go to that path to make it current (i.e.: "."). Use PUSHD , CD , and POPD to go back and forth to a specific path in order to temporarily use a shorter relative path.

How do I shorten a path in CMD?

To remove the path from the prompt use 'prompt $g' which will just show the chevron. You can then use the 'cd' command anytime to see the directory you are in. E.g. This may be a better answer, but I am glad they are both here.


1 Answers

To specify a number of shown trailing components of the path insert an integer into corresponding escape sequence in your prompt. In your case, %2~ will do the trick. Excerpt from zshmisc(1):

%d 
%/     Current working directory.  If an integer follows the `%', 
       it specifies a number of trailing components of the current 
       working directory to show; zero means the whole path.  A 
       negative integer specifies leading  components, i.e. %-1d 
       specifies the first component.

%~     As  %d  and  %/, but if the current working directory starts
       with $HOME, that part is replaced by a `~'. Furthermore, if 
       it has a named directory as its prefix, that part is replaced 
       by a `~' followed by the name of the directory, but only if 
       the result is shorter than the full path; see Dynamic and 
       Static named directories in zshexpn(1).
like image 196
Vladimir Zakharov Avatar answered Oct 13 '22 12:10

Vladimir Zakharov