Last active
September 13, 2020 13:39
-
-
Save teramako/31a65733a44f919ffa92d8317ef1cbef to your computer and use it in GitHub Desktop.
pecoで ls -l の結果からファイル名部分のみを検索できるようにCustomFilterを設定
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"CustomFilter": { | |
"ls-l": { | |
"Cmd": "perl", | |
"Args": ["/Users/teramako/bin/ls-l-filter.pl", "$QUERY"], | |
"BufferThreshold": 100 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
my $pattern = shift or die "usage $0 [pattern]"; | |
while (my $line = <STDIN>) { | |
chomp $line; | |
my @cols = split(/\s+/, $line); | |
next if !$cols[8]; | |
print $line, "\n" if index(lc($cols[8]), lc($pattern)) >= 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## zshrc | |
function peco-find-files() { | |
local LS=/bin/ls | |
local P=${LBUFFER##* } | |
local LEFT="${LBUFFER% *} " | |
[ "$P" = "${LBUFFER}" ] && LEFT="" | |
local pecoPrompt="find files> " | |
local FILE="" | |
if [ -n "$P" ]; then | |
if [[ "$P" = *=* ]]; then | |
LEFT="${LEFT}${P%%=*}=" | |
P=${P#*=} | |
fi | |
if [[ "$P" = \"* ]]; then | |
LEFT="${LEFT}\"" | |
P=${P#\"} | |
elif [[ "$P" = \'* ]]; then | |
LEFT="${LEFT}\'" | |
P=${P#\'} | |
fi | |
local D="" | |
local F="" | |
if [[ "$P" = */* ]]; then | |
D="${P%/*}/" | |
F=${P##*/} | |
else | |
F=$P | |
fi | |
pecoPrompt="find: ${D:-.}> " | |
local LINE=$(${LS} ${D:-.} | /usr/local/bin/peco --prompt="${pecoPrompt}" --select-1 --on-cancel error --initial-filter=ls-l --query="$F") | |
RC=$? | |
LEFT="${LEFT}${D}" | |
else | |
local LINE=$(${LS} | /usr/local/bin/peco --prompt="${pecoPrompt}" --select-1 --on-cancel error --initial-filter=ls-l) | |
RC=$? | |
fi | |
if [ $RC -ne 0 ] || [ -z "${LINE}" ];then | |
zle reset-prompt | |
return 0 | |
fi | |
FILE=$(echo "${LINE}" | awk '{if(substr($1,0,1)=="l"){print $(NF-2);}else{print $NF}}') | |
LEFT="${LEFT}${FILE}" | |
BUFFER="${LEFT}${RBUFFER}" | |
CURSOR=$#LEFT | |
zle reset-prompt | |
} | |
zle -N peco-find-files | |
bindkey '^f' peco-find-files |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment