Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
soheilpro committed Apr 29, 2016
0 parents commit 6a69678
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2014 Soheil Rashidi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# zsh-vi-search
Adds support for searching the current line (in normal vi mode) to zsh.

## Install
Download [zsh-vi-search.zsh](https://raw.githubusercontent.com/soheilpro/zsh-vi-search/master/src/zsh-vi-search.zsh) and source it somewhere in your .zshrc file.

## Usage
+ <kbd>/</kbd> Search forward
+ <kbd>?</kbd> Search backward
+ <kbd>n</kbd> Repeat last search
+ <kbd>N</kbd> Repeat last search in the opposite direction

## Version History
+ 1.0
+ Initial release.

## Author
Soheil Rashidi

+ http://soheilrashidi.com
+ http://twitter.com/soheilpro
+ http://github.com/soheilpro

## Copyright and License
Copyright 2016 Soheil Rashidi

Licensed under the The MIT License (the "License");
you may not use this work except in compliance with the License.
You may obtain a copy of the License in the LICENSE file, or at:

http://www.opensource.org/licenses/mit-license.php

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
73 changes: 73 additions & 0 deletions src/zsh-vi-search.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
autoload -U read-from-minibuffer

function _index-of {
local STR=$1
local STRLEN=${#STR}
local SUBSTR=$2
local SUBSTRLEN=${#SUBSTR}
local START=${3:-0}
local DIRECTION=${4:-1}

[[ $STRLEN -ge 0 ]] || return 1
[[ $SUBSTRLEN -ge 0 ]] || return 2
[[ $START -ge 0 ]] || return 3
[[ $START -lt $STRLEN ]] || return 4
[[ $DIRECTION -eq 1 || $DIRECTION -eq -1 ]] || return 5

for ((INDEX = $START; INDEX >= 0 && INDEX < $STRLEN; INDEX = INDEX + $DIRECTION)); do
if [[ "${STR:$INDEX:$SUBSTRLEN}" == "$SUBSTR" ]]; then
echo $INDEX
return
fi
done

return -1
}

function _vi-search-forward {
read-from-minibuffer
INDEX=$(_index-of $BUFFER $REPLY $CURSOR) && CURSOR=$INDEX || INDEX=$(_index-of $BUFFER $REPLY 0) && CURSOR=$INDEX
export VISEARCHSTR=$REPLY
export VISEARCHDIRECTION=1
}

function _vi-search-forward-repeat {
INDEX=$(_index-of $BUFFER $VISEARCHSTR $(($CURSOR + 1))) && CURSOR=$INDEX || INDEX=$(_index-of $BUFFER $VISEARCHSTR 0) && CURSOR=$INDEX
}

function _vi-search-backward {
read-from-minibuffer
INDEX=$(_index-of $BUFFER $REPLY $CURSOR -1) && CURSOR=$INDEX || INDEX=$(_index-of $BUFFER $REPLY $((${#BUFFER} - 1)) -1) && CURSOR=$INDEX
export VISEARCHSTR=$REPLY
export VISEARCHDIRECTION=-1
}

function _vi-search-backward-repeat {
INDEX=$(_index-of $BUFFER $VISEARCHSTR $(($CURSOR - 1)) -1) && CURSOR=$INDEX || INDEX=$(_index-of $BUFFER $VISEARCHSTR $((${#BUFFER} - 1)) -1) && CURSOR=$INDEX
}

function _vi-search-repeat {
if [[ $VISEARCHDIRECTION -eq 1 ]]; then
_vi-search-forward-repeat
else
_vi-search-backward-repeat
fi
}

function _vi-search-repeat-reverse {
if [[ $VISEARCHDIRECTION -eq 1 ]]; then
_vi-search-backward-repeat
else
_vi-search-forward-repeat
fi
}

zle -N vi-search-backward _vi-search-backward
zle -N vi-search-forward _vi-search-forward
zle -N vi-search-repeat _vi-search-repeat
zle -N vi-search-repeat-reverse _vi-search-repeat-reverse

bindkey -M vicmd '?' vi-search-backward
bindkey -M vicmd '/' vi-search-forward
bindkey -M vicmd 'n' vi-search-repeat
bindkey -M vicmd 'N' vi-search-repeat-reverse

0 comments on commit 6a69678

Please sign in to comment.