Limit the bash shell's prompt length

When you use Terminal in 10.3 and 10.4, you’re probably using the bash shell—it’s the default, unless you’re using an upgrade install that was previously using tcsh.

If you’re not familiar with what a “shell” is, it’s actually a program that runs and provides the interface you use when communicating with the Unix side of OS X via Terminal. In the first versions of OS X, Apple set the default shell to tcsh, but switched to bash with the release of OS X 10.3 and newer.

One of the features of bash is a prompt line that shows the current working directory. As you navigate into the file structure, your prompt string constantly reflects the current working directory:

gargantua5:~ robg$ cd Library/
gargantua5:~/Library robg$ cd Widgets/
gargantua5:~/Library/Widgets robg$ cd WindGuru.wdgt/
gargantua5:~/Library/Widgets/WindGuru.wdgt robg$

While this is great in theory, it can be not-so-great in practice. Consider what happens when you drill down into a deeply-buried system folder, such as this one:

gargantua5:/System/Library/CoreServices/Dock.app/Contents/Resources robg$

If you’ve got an 80-character Terminal window, the above prompt line actually wraps completely around the width of your screen. This makes it very tough to work in Terminal, as your screen becomes a confusing jumble of commands, path display, and output. There are many solutions to this problem, but here are two that I like.

For both of these solutions, you’ll need to use the vi editor to edit your user’s .bash_profile file, in your home directory. Launch Terminal and make sure you’re in your home directory by typing cd and then pressing Return. Next, type vi .bash_profile. If you have an existing file, it will open for editing. If not, a new file will open. From there, follow the appropriate solution as you prefer.

Solution 1: Use the Terminal’s title bar

Instead of displaying your path in your prompt, you can have bash display the prompt in your Terminal’s window title, like this:

To do this, you just need to add these two lines to the .bash_profile file:

export PROMPT_COMMAND='echo -n "^[]1;$PWD^G"'
PS1='e[7m [u] :[e[0;m] '

However, you cannot simply copy and paste all of the the above text—the second line will work, but the first line has two special “quoted” commands in it. So here’s how to enter it. First press i to put vi in insert mode.

Next, copy and paste everything up to, but not including, the first caret (^). The following two characters, ^[, represent the actual Escape key. To type it in vi, type Control-V, then press Escape. This will enter the coded Escape key characters. You can then copy and paste everything up to, but again not including, the next caret. To enter the ^G, you use the same procedure—type Control-V, then press Control-G. Finally, type the closing double-quote and single quote. Whew.

After entering the two lines, press Escape, then type :w to save your changes, and :q to quit the editor. Open a new Terminal window, and you should see the path in the title, and a nice short prompt holding your username.

I won’t attempt to explain the voodoo going on in these two commands, other than the first works to set the window title, and the second uses some ASCII codes to create the black-on-white version of your username as the prompt. Save the file by hitting Escape While many people prefer this ultra-clean prompt with the path in the window title, I find I like at least a short reminder of my current directory in my prompt, which is exactly what you’ll get with the next solution.

Solution 2: Limit the length of the displayed path

The second solution limits the prompt’s path to a given number of characters. Regardless of how deeply you drill into the system, you’ll only see your preferred number of characters in the prompt. Open the .bash_profile file as explained above, press i to enter insert mode, and then paste the following code:

function prompt_command {
  #   How many characters of the $PWD should be kept
  local pwd_length=23
  if [ $(echo -n $PWD | wc -c | tr -d " ") -gt $pwd_length ]
    then newPWD="$(echo -n $PWD | sed -e "s/.*\(.\{$pwd_length\}\)/\1/")"
    else newPWD="$(echo -n $PWD)"
  fi
}
PROMPT_COMMAND=prompt_command
export PROMPT_COMMAND
PS1='\e[7m$newPWD  [\u]:\[\e[0;m\] '

Leave everything as you see it, other than possbly the 23, which represents how many characters you’d like to see in the prompt’s path. When done, hit Escape to exit insert mode, then type :w and :q.

As with the first solution, there’s some fancy voodoo going on here to shorten the path (that’s what the function does), and then display it in your prompt. Open a new Terminal window and navigate into a deeply-buried folder, and you should see something like this:

Resources/English.lproj  [robg]:

While this isn’t as useful as the full path, it’s at least enough to remind me of where I was before I execute my next command.

recommended for you

Mac OS X Hints

Read more »

Subscribe to the Create Newsletter

Comments