Responsive Directory Truncation in Powerlevel10k

by romka
GNU/Linux ◆ xterm-256color ◆ zsh 6180 views

Powerlevel10k is a theme for ZSH. This screencast shows how it can truncate your current directory to make it fit on the prompt line.

When the terminal window is wide enough, the full directory is displayed.

~/work/projects/repos/skynet/src/ai/terminator/models/T-800

If the full directory doesn’t fit, the leftmost segment gets truncated to its shortest unique prefix. ~/work becomes ~/wo. It cannot be truncated to ~/w because there is ~/wireguard and thus ~/w would be ambiguous.

Two segments in this directory are important and never get truncated: skynet, because it’s a root of a Git repository; and T-800, because it’s the last segment.

Directory segments are shown in one of 3 colors:

  • Important segments are bright.
  • Truncated segment are bleak.
  • Regular segment (not truncated but can be) use in-between color.

(It’s difficult to distinguish these colors on asciinema.org. Rest assured they are customizable, so you can get any level of contrast you like.)

After several rounds of truncation the directory turns into ~/wo/p/r/skynet/s/a/t/m/T-800 and cannot be shortened any further. If it still doesn’t fit on the prompt line, right prompt gets hidden.

Truncated directories can be tab-completed to their original values. Typing ~/wo/p/r/skynet/s/a/t/m/T-800<TAB> yields ~/work/projects/repos/skynet/src/ai/terminator/models/T-800/.

Try it out:

git clone https://github.com/romkatv/powerlevel10k.git ~/powerlevel10k
echo 'source ~/powerlevel10k/powerlevel10k.zsh-theme' >>! ~/.zshrc

Restart zsh and type p10k configure.

Alternative installation methods available.