Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In zsh, is there a way to navigate through the history of directories that I have visited (those listed in dirs) when I am typing a command in the command line?

For example, say that I start typing cp -r in the terminal, I would like to then use M-n or M-p until I find a directory, from the list of directories that I have visited, that I want to follow cp -r in the command that I am typing. Eventually I would find /some/path/ for the cp source directory, and then type <SPC> and repeat the process to find /some/other/path for the destination directory for my cp command.

In case it helps, the code below helps me navigate my command history from the command line using C-n and C-p, which allows me to autocomplete my command with other commands that I previously typed.

up-line-or-history-beginning-search () {
  if [[ -n $PREBUFFER ]]; then
    zle up-line-or-history
  else
    zle history-beginning-search-backward
  fi
}
down-line-or-history-beginning-search () {
  if [[ -n $PREBUFFER ]]; then
    zle down-line-or-history
  else
    zle history-beginning-search-forward
  fi
}

zle -N up-line-or-history-beginning-search
zle -N down-line-or-history-beginning-search

bindkey '^P' up-line-or-history-beginning-search
bindkey '^N' down-line-or-history-beginning-search
share|improve this question
    
cd -<TAB> if you have pushd enabled? –  Ulrich Dangel Mar 1 '13 at 22:31
    
@UlrichDangel That is very close to what I want! There are two differences: 1) I think it only shows directories within my current session and 2) It doesn't let me iterate pass the last item in the list. Is there any alternative? –  user815423426 Mar 1 '13 at 22:59
    
no, but you can increase the stacksize –  Ulrich Dangel Mar 2 '13 at 8:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.