I need a way to use the current line which the users typed into as variable for a shell function.
my current code, which can be called by ctrl+r
zle -N search
bindkey "^R" search
search () {
read str;
fc -ln -30 | grep $(printf "%q\n" "$str");
}
or simply, to call it as a function
search () {
fc -ln -30 | grep $(printf "%q\n" "$1");
}
updated: target pseudo code, to call as a function called by ctrl+r that needs no further input prompt
zle -N search
bindkey "^R" search
search ()
echo ""; #for better formatting because ctrl+R is not enter so the BUFFER(current line) gets corrupted and looks messy and the current input is not correctly shown
fc -ln -30 | grep $(printf "%q\n" "$BUFFER"); #edited to be the solution where $BUFFER is the current terminal line
}
Best Answer
In zle widgets, the contents of the editing buffer is made available in the
$BUFFER
variable.$LBUFFER
contains what's left of the cursor (same$BUFFER[1,CURSOR-1]
) and$RBUFFER
what's to the right (same as$BUFFER[CURSOR,-1]
).For a widget that prints a report of the previous command lines that contain the string entered so far (among the 30 most recent ones), you could do something like: