README.md 3.13 KB
Newer Older
1
# shrink-path
2

3
4
5
6
7
8
9
A plugin to shrink directory paths for brevity and pretty-printing.

To use it, add `shrink-path` to the plugins array in your zshrc file:

```zsh
plugins=(... shrink-path)
```
10
11
12
13
14

## Examples

For this directory tree:
```
15
16
17
18
19
20
21
22
/home/
  me/
    f o o/     # The prefix f is ambiguous between "f o o" and "f i g".
      bar/
        quux/
      biz/     # The prefix b is ambiguous between bar and biz.
    f i g/
      baz/
23
24
25
```
here are the results of calling `shrink_path <option> /home/me/foo/bar/quux`:
```
26
27
28
29
30
31
32
33
34
35
36
37
Option        Result
<none>        /h/m/f o/ba/q
-l|--last     /h/m/f o/ba/q
-s|--short    /h/m/f/b/q
-t|--tilde    ~/f o/ba/q
-f|--fish     ~/f/b/quux
-g|--glob     /h*/m*/f o*/ba*/q*
-3            /hom/me/f o/bar/quu
-e '$' -3     /hom$/me/f o$/bar/quu$
-q            /h/m/f\ o/ba/q
-g -q         /h*/m*/f\ o*/ba*/q*
-x            /home/me/foo/bar/quux
38
39
40
41
42
43
44
```

## Usage

For a fish-style working directory in your command prompt, add the following to
your theme or zshrc:

45
46
47
```zsh
setopt prompt_subst
PS1='%n@%m $(shrink_path -f)>'
48
49
50
51
52
53
```

The following options are available:

```
    -f, --fish       fish simulation, equivalent to -l -s -t.
54
    -g, --glob       Add asterisk to allow globbing of shrunk path (equivalent to -e "*")
55
    -l, --last       Print the last directory's full name.
56
    -s, --short      Truncate directory names to the number of characters given by -. Without
57
58
59
                     -s, names are truncated without making them ambiguous.
    -t, --tilde      Substitute ~ for the home directory.
    -T, --nameddirs  Substitute named directories as well.
60
61
62
    -#               Truncate each directly to at least this many characters inclusive of the
                     ellipsis character(s) (defaulting to 1).
    -e SYMBOL        Postfix symbol(s) to indicate that a directory name had been truncated.
63
    -q, --quote      Quote special characters in the shrunk path
64
    -x, --expand     Print the full path. This takes precedence over the other options
65
66
67
```

The long options can also be set via zstyle, like
68
69
```zsh
zstyle :prompt:shrink_path fish yes
70
71
72
73
74
75
```

Note: Directory names containing two or more consecutive spaces are not yet
supported.


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
## Trick: toggle shrinking with a keyboard shortcut

You can use the `expand` option to disable the path shrinking. You can combine that
with a key binding widget to toggle path shrinking on and off.

```zsh
# Toggle off path shrinking
zstyle ':prompt:shrink_path' expand true
# Toggle on path shrinking
zstyle -d ':prompt:shrink_path' expand
```

Combined with a widget:

```zsh
# Widget definition
shrink-path-toggle() {
  zstyle -t ':prompt:shrink_path' expand \
    && zstyle -d ':prompt:shrink_path' expand \
    || zstyle ':prompt:shrink_path' expand true
  zle reset-prompt
}
zle -N shrink-path-toggle
# Key binding to ALT+SHIFT+S
bindkey "^[S" shrink-path-toggle
```

103
104
105
## License

Copyright (C) 2008 by Daniel Friesel <derf@xxxxxxxxxxxxxxxxxx>
106
Copyright (C) 2018-2020 by Pavel N. Krivitsky
107

Janosch Schwalm's avatar
Janosch Schwalm committed
108
License: WTFPL <http://www.wtfpl.net>
109

Janosch Schwalm's avatar
Janosch Schwalm committed
110
111
Ref: https://www.zsh.org/mla/workers/2009/msg00415.html
     https://www.zsh.org/mla/workers/2009/msg00419.html
112
113
114
115
116


## Misc

Keywords: prompt directory truncate shrink collapse fish