I need a zle function that will print the character before and after the cursor.
print-char-before-after() {
# Get the position of the cursor
local cursor_pos=$CURSOR
# Get the text in the current line
local line_text=${BUFFER}
}
# Bind the function to a key combination (for example, Ctrl+P)
zle -N print-char-before-after
bindkey '^P' print-char-before-after
Considering the pipe to be the cursor, if input is:
This is a inp|ut
The output will be pu
It needs to work on beginning and end of line and also on multiline buffer.
if input is:
|This is a input
The output will be T
if input is:
This is a input|
The output will be t
How can I do that?
UPDATE 1:
The code that sort of solves my problem is:
print-char-before-after() {
# Get the characters from the left and right buffers
local before_char=""
local after_char=""
# Check if the cursor is not at the beginning of the line
if [[ -n "$LBUFFER" ]]; then
before_char=${LBUFFER[-1]} # Last character of LBUFFER
fi
# Check if the cursor is not at the end of the line
if [[ -n "$RBUFFER" ]]; then
after_char=${RBUFFER[1]} # First character of RBUFFER
fi
# Print the characters before and after
echo "${before_char}${after_char}"
}
# Bind the function to a key combination (for example, Ctrl+P)
zle -N print-char-before-after
bindkey '^P' print-char-before-after
INP|UT, you would haveIN🄿UT. So should the output benpu?printf '\33[5 q\33]12;green\a'in xterm-like terminals for instance.