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} # 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
Asked by Ahmad Ismail
(2998 rep)
Sep 25, 2024, 11:47 AM
Last activity: Sep 26, 2024, 06:58 AM
Last activity: Sep 26, 2024, 06:58 AM