dirhistory.plugin.zsh 4.96 KB
Newer Older
Jeff Williams's avatar
Jeff Williams committed
1
2
3
4
## 
#   Navigate directory history using ALT-LEFT and ALT-RIGHT. ALT-LEFT moves back to directories 
#   that the user has changed to in the past, and ALT-RIGHT undoes ALT-LEFT.
# 
5
#   Navigate directory hierarchy using ALT-UP and ALT-DOWN.
6
7
8
#   ALT-UP moves to higher hierarchy (cd ..)
#   ALT-DOWN moves into the first directory found in alphabetical order
#
Jeff Williams's avatar
Jeff Williams committed
9

10
dirhistory_past=($PWD)
Jeff Williams's avatar
Jeff Williams committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
dirhistory_future=()
export dirhistory_past
export dirhistory_future

export DIRHISTORY_SIZE=30

# Pop the last element of dirhistory_past. 
# Pass the name of the variable to return the result in. 
# Returns the element if the array was not empty,
# otherwise returns empty string.
function pop_past() {
  eval "$1='$dirhistory_past[$#dirhistory_past]'"
  if [[ $#dirhistory_past -gt 0 ]]; then
    dirhistory_past[$#dirhistory_past]=()
  fi
}

function pop_future() {
  eval "$1='$dirhistory_future[$#dirhistory_future]'"
  if [[ $#dirhistory_future -gt 0 ]]; then
    dirhistory_future[$#dirhistory_future]=()
  fi
}

# Push a new element onto the end of dirhistory_past. If the size of the array 
# is >= DIRHISTORY_SIZE, the array is shifted
function push_past() {
  if [[ $#dirhistory_past -ge $DIRHISTORY_SIZE ]]; then
    shift dirhistory_past
  fi
  if [[ $#dirhistory_past -eq 0 || $dirhistory_past[$#dirhistory_past] != "$1" ]]; then
    dirhistory_past+=($1)
  fi
}

function push_future() {
  if [[ $#dirhistory_future -ge $DIRHISTORY_SIZE ]]; then
    shift dirhistory_future
  fi
  if [[ $#dirhistory_future -eq 0 || $dirhistory_futuret[$#dirhistory_future] != "$1" ]]; then
    dirhistory_future+=($1)
  fi
}

# Called by zsh when directory changes
56
57
autoload -U add-zsh-hook
add-zsh-hook chpwd chpwd_dirhistory
xiao's avatar
xiao committed
58
function chpwd_dirhistory() {
59
  push_past $PWD
Jeff Williams's avatar
Jeff Williams committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  # If DIRHISTORY_CD is not set...
  if [[ -z "${DIRHISTORY_CD+x}" ]]; then
    # ... clear future.
    dirhistory_future=()
  fi
}

function dirhistory_cd(){
  DIRHISTORY_CD="1"
  cd $1
  unset DIRHISTORY_CD
}

# Move backward in directory history
function dirhistory_back() {
  local cw=""
  local d=""
  # Last element in dirhistory_past is the cwd.

  pop_past cw 
  if [[ "" == "$cw" ]]; then
    # Someone overwrote our variable. Recover it.
82
    dirhistory_past=($PWD)
Jeff Williams's avatar
Jeff Williams committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    return
  fi

  pop_past d
  if [[ "" != "$d" ]]; then
    dirhistory_cd $d
    push_future $cw
  else
    push_past $cw
  fi
}


# Move forward in directory history
function dirhistory_forward() {
  local d=""

  pop_future d
  if [[ "" != "$d" ]]; then
    dirhistory_cd $d
    push_past $d
  fi
}


# Bind keys to history navigation
function dirhistory_zle_dirhistory_back() {
  # Erase current line in buffer
111
112
113
  zle .kill-buffer
  dirhistory_back
  zle .accept-line
Jeff Williams's avatar
Jeff Williams committed
114
115
116
117
}

function dirhistory_zle_dirhistory_future() {
  # Erase current line in buffer
118
  zle .kill-buffer
Jeff Williams's avatar
Jeff Williams committed
119
  dirhistory_forward
120
  zle .accept-line
Jeff Williams's avatar
Jeff Williams committed
121
122
123
124
125
126
}

zle -N dirhistory_zle_dirhistory_back
# xterm in normal mode
bindkey "\e[3D" dirhistory_zle_dirhistory_back
bindkey "\e[1;3D" dirhistory_zle_dirhistory_back
127
# Mac teminal (alt+left/right)
128
if [[ "$TERM_PROGRAM" == "Apple_Terminal" || "$TERM_PROGRAM" == "iTerm.app" ]]; then
129
130
  bindkey "^[b" dirhistory_zle_dirhistory_back
fi
Jeff Williams's avatar
Jeff Williams committed
131
132
133
134
135
136
137
138
# Putty:
bindkey "\e\e[D" dirhistory_zle_dirhistory_back
# GNU screen:
bindkey "\eO3D" dirhistory_zle_dirhistory_back

zle -N dirhistory_zle_dirhistory_future
bindkey "\e[3C" dirhistory_zle_dirhistory_future
bindkey "\e[1;3C" dirhistory_zle_dirhistory_future
139
if [[ "$TERM_PROGRAM" == "Apple_Terminal" || "$TERM_PROGRAM" == "iTerm.app" ]]; then
140
141
  bindkey "^[f" dirhistory_zle_dirhistory_future
fi
Jeff Williams's avatar
Jeff Williams committed
142
143
144
145
bindkey "\e\e[C" dirhistory_zle_dirhistory_future
bindkey "\eO3C" dirhistory_zle_dirhistory_future


146
147
148
149
150
151
# 
# HIERARCHY Implemented in this section, in case someone wants to split it to another plugin if it clashes bindings
# 

# Move up in hierarchy
function dirhistory_up() {
152
  cd .. || return 1
153
154
155
156
}

# Move down in hierarchy
function dirhistory_down() {
157
  cd "$(find . -mindepth 1 -maxdepth 1 -type d | sort -n | head -n 1)" || return 1
158
159
160
161
162
}


# Bind keys to hierarchy navigation
function dirhistory_zle_dirhistory_up() {
163
  zle .kill-buffer   # Erase current line in buffer
164
  dirhistory_up
165
  zle .accept-line
166
167
168
}

function dirhistory_zle_dirhistory_down() {
169
  zle .kill-buffer   # Erase current line in buffer
170
  dirhistory_down
171
  zle .accept-line
172
173
174
175
176
177
}

zle -N dirhistory_zle_dirhistory_up
# xterm in normal mode
bindkey "\e[3A" dirhistory_zle_dirhistory_up
bindkey "\e[1;3A" dirhistory_zle_dirhistory_up
178
179
180
if [[ "$TERM_PROGRAM" == "Apple_Terminal" || "$TERM_PROGRAM" == "iTerm.app" ]]; then
  bindkey "^[[A" dirhistory_zle_dirhistory_up
fi
181
182
183
184
185
186
187
188
# Putty:
bindkey "\e\e[A" dirhistory_zle_dirhistory_up
# GNU screen:
bindkey "\eO3A" dirhistory_zle_dirhistory_up

zle -N dirhistory_zle_dirhistory_down
bindkey "\e[3B" dirhistory_zle_dirhistory_down
bindkey "\e[1;3B" dirhistory_zle_dirhistory_down
189
190
191
if [[ "$TERM_PROGRAM" == "Apple_Terminal" || "$TERM_PROGRAM" == "iTerm.app" ]]; then
  bindkey "^[[B" dirhistory_zle_dirhistory_down
fi
192
193
bindkey "\e\e[B" dirhistory_zle_dirhistory_down
bindkey "\eO3B" dirhistory_zle_dirhistory_down