- Notifications
You must be signed in to change notification settings - Fork169
🐠 ZSH port of Fish history search (up arrow)
zsh-users/zsh-history-substring-search
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This is a clean-room implementation of theFish shell's history searchfeature, where you can type in any part of any command from history and thenpress chosen keys, such as the UP and DOWN arrows, to cycle through matches.
- ZSH 4.3 or newer
Using theHomebrew package manager:
brew install zsh-history-substring-searchecho 'source $(brew --prefix)/share/zsh-history-substring-search/zsh-history-substring-search.zsh' >> ~/.zshrcUsingFig:
Fig adds apps, shortcuts, and autocomplete to your existing terminal.
Installzsh-history-substring-search in just one click.
UsingOh-my-zsh:
Clone this repository in oh-my-zsh's plugins directory:
git clone https://github.com/zsh-users/zsh-history-substring-search ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-history-substring-searchActivate the plugin in
~/.zshrc:plugins=( [plugins...] zsh-history-substring-search)Run
exec zshto take changes into account:exec zsh
Usingzplug:
Add this repo to
~/.zshrc:zplug "zsh-users/zsh-history-substring-search", as: plugin
Usingantigen:
- Add the
antigen bundlecommand just beforeantigen apply, like this:
antigen bundle zsh-users/zsh-history-substring-searchantigen apply- Then,after
antigen apply, add the key binding configurations, like this:
# zsh-history-substring-search configurationbindkey '^[[A' history-substring-search-up # or '\eOA'bindkey '^[[B' history-substring-search-down # or '\eOB'HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE=1UsingZinit:
Use the
Oh-my-zshZinit snippet in~/.zshrc:zinit snippet OMZ::plugins/git/git.plugin.zsh`Load the plugin in
~/.zshrc:zinit load 'zsh-users/zsh-history-substring-search zinit ice wait atload'_history_substring_search_config'Run
exec zshto take changes into account:exec zsh
Load this script into your interactive ZSH session:
source zsh-history-substring-search.zshIf you want to usezsh-syntax-highlighting along with this script,then make sure that you load itbefore you load this script:
source zsh-syntax-highlighting.zshsource zsh-history-substring-search.zshBind keyboard shortcuts to this script's functions.
Users typically bind their UP and DOWN arrow keys to this script, thus:
Run
cat -vin your favorite terminal emulator to observe key codes. (NOTE: In some cases,cat -vshows the wrong key codes. If thekey codes shown bycat -vdon't work for you, press<C-v><UP>and<C-v><DOWN>at your ZSH command line prompt for correct key codes.)Press the UP arrow key and observe what is printed in your terminal.
Press the DOWN arrow key and observe what is printed in your terminal.
Press the Control and C keys simultaneously to terminate the
cat -v.Use your observations from the previous steps to create key bindings.For example, if you observed
^[[Afor UP and^[[Bfor DOWN, then:bindkey '^[[A' history-substring-search-upbindkey '^[[B' history-substring-search-downHowever, if the observed values don't work, you can try using terminfo:
bindkey "$terminfo[kcuu1]" history-substring-search-upbindkey "$terminfo[kcud1]" history-substring-search-downUsers have also observed that
[OAand[OBare correct values,even if these were not the observed values. If you are having troublewith the observed values, give these a try.You might also want to bind the Control-P/N keys for use in EMACS mode:
bindkey -M emacs '^P' history-substring-search-upbindkey -M emacs '^N' history-substring-search-downYou might also want to bind the
kandjkeys for use in VI mode:bindkey -M vicmd 'k' history-substring-search-upbindkey -M vicmd 'j' history-substring-search-down
Type any part of any previous command and then:
Press the
history-substring-search-upkey, which was configured instep 2 above, to select the nearest command that (1) contains your queryand (2) is also older than the current command in your command history.Press the
history-substring-search-downkey, which was configured instep 2 above, to select the nearest command that (1) contains your queryand (2) is also newer than the current command in your command history.Press
^Uthe Control and U keys simultaneously to abort the search.
If a matching command spans more than one line of text, press the LEFTarrow key to move the cursor away from the end of the command, and then:
Press the
history-substring-search-upkey, which was configured instep 2 above, to move the cursor to the line above the cursored line.When the cursor reaches the first line of the command, pressing thehistory-substring-search-upkey again will cause this script toperform another search.Press the
history-substring-search-downkey, which was configured instep 2 above, to move the cursor to the line below the cursored line.When the cursor reaches the last line of the command, pressing thehistory-substring-search-downkey, which was configured in step 2above, again will cause this script to perform another search.
This script defines the following global variables. You may override theirdefault values.
HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUNDis a global variable that defineshow the query should be highlighted inside a matching command. Its defaultvalue causes this script to highlight using bold, white text on a magentabackground. See the "Character Highlighting" section in the zshzle(1) manpage to learn about the kinds of values you may assign to this variable.HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUNDis a global variable thatdefines how the query should be highlighted when no commands in thehistory match it. Its default value causes this script to highlight usingbold, white text on a red background. See the "Character Highlighting"section in the zshzle(1) man page to learn about the kinds of values youmay assign to this variable.HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGSis a global variable that defineshow the command history will be searched for your query. Its default valuecauses this script to perform a case-insensitive search. See the "GlobbingFlags" section in the zshexpn(1) man page to learn about the kinds ofvalues you may assign to this variable.HISTORY_SUBSTRING_SEARCH_FUZZYis a global variable that defineshow the command history will be searched for your query. If set to a non-emptyvalue, causes this script to perform a fuzzy search by words, matching ingiven order e.g.ab cwill match*ab*c*HISTORY_SUBSTRING_SEARCH_PREFIXEDis a global variable that defines howthe command history will be searched for your query. If set to a non-emptyvalue, your query will be matched against the start of each history entry.For example, if this variable is empty,lswill matchls -landecho ls; if it is non-empty,lswill only matchls -l.HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUEis a global variable that defineswhether all search results returned areunique. If set to a non-emptyvalue, then only unique search results are presented. This behaviour is offby default. An alternative way to ensure that search results are unique isto usesetopt HIST_IGNORE_ALL_DUPS. If this configuration variable is offandsetopt HIST_IGNORE_ALL_DUPSis unset, thensetopt HIST_FIND_NO_DUPSis still respected and it makes this script skip duplicateadjacent searchresults as you cycle through them, but this does not guarantee that searchresults are unique: if your search results were "Dog", "Dog", "HotDog","Dog", then cycling them gives "Dog", "HotDog", "Dog". Notice that the "Dog"search result appeared twice as you cycled through them. If you wish toreceive globally unique search results only once, then use thisconfiguration variable, or usesetopt HIST_IGNORE_ALL_DUPS.HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_TIMEOUTis a global variable thatdefines a timeout in seconds for clearing the search highlight.
September 2009:Peter Stephenson originally wrote this script and itpublished to the zsh-users mailing list.
January 2011: Guido van Steen (@guidovansteen) revised this script andreleased it under the 3-clause BSD license as part offizsh, theFriendly Interactive ZSHell.
January 2011: Suraj N. Kurapati (@sunaku) extracted this script fromfizsh 1.0.1, refactored it heavily, and finally repackaged it as anoh-my-zsh plugin and as an independently loadableZSH script.
July 2011: Guido van Steen, Suraj N. Kurapati, and Sorin Ionescu(@sorin-ionescu)further developed it with Vincent Guerci (@vguerci).
March 2016: Geza Lore (@gezalore) greatly refactored it in pull request #55.
About
🐠 ZSH port of Fish history search (up arrow)
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.