Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is it possible to...

  • Step up local ZSH shell history (actions only happened in that prompt)

  • Step up global ZSH history (shared history is on) - default what happens when you press UP arrow when shared history is on

... separately.

For example one might bind CTRL+up for global history, normal up for local history. At the same time it makes sense to use the global history for a backwards history search (i.e. CTRL+R).

This might speed up some shell operations, as some operations are specific to that shell window and you want to go back in them.

share|improve this question

2 Answers 2

up vote 8 down vote accepted

Copy & Paste this to your .zshrc:

Cursors are using local history:

bindkey "${key[Up]}" up-line-or-local-history
bindkey "${key[Down]}" down-line-or-local-history

up-line-or-local-history() {
    zle set-local-history 1
    zle up-line-or-history
    zle set-local-history 0
zle -N up-line-or-local-history
down-line-or-local-history() {
    zle set-local-history 1
    zle down-line-or-history
    zle set-local-history 0
zle -N down-line-or-local-history

If you need also key bindings (CTRL + cursors) to step through the global history add also this to your .zshrc:

bindkey "^[[1;5A" up-line-or-history    # [CTRL] + Cursor up
bindkey "^[[1;5B" down-line-or-history  # [CTRL] + Cursor down

To make this work the option SHARE_HISTORY (see 16.2.4 History) needs to be enabled. Run setopt and check if "sharehistory" is listed. If not add setopt sharehistory to your .zshrc. Then one can use set-local-history as we did above. The documenation says:

By default, history movement commands visit the imported lines as well as the local lines, but you can toggle this on and off with the set-local-history zle binding. It is also possible to create a zle widget that will make some commands ignore imported commands, and some include them.

Note that by default global history is used (and all functions end with "zle set-local-history 0", i.e. local history is disabled). So pressing CTRL + R will search the global history by default (which makes sense in most cases).

This is quite similar to the solution by @mpy, but ready for copy & paste. It overwrites the cursor keys up and down. I used this mail list entry.

See also:

share|improve this answer

You can setup a special zle widget to show only local history items:

function only-local-history () {
        zle set-local-history 1
        zle up-history
        zle set-local-history 0
zle -N only-local-history

Assuming, that is bound to up-line-or-history (I think that is default), you can bind this widget to another key stroke, like CTRL+:

 bindkey "^[Oa" only-local-history

If this works is probably dependent of your terminal. Above line works in URxvt/Screen. With xterm you'll need

 bindkey "^[[1;5A" only-local-history

for CTRL+.

Another variant could be

function peek-history () {
        zle set-local-history
        zle up-history
        zle set-local-history
zle -N peek-history

so, if you have local history enabled, you can peek into the global one or vice versa.

share|improve this answer

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.