我需要一个 zle 函数来打印光标前后的字符。
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
假设管道是光标,如果输入是:
This is a inp|ut
输出将是pu
它需要在行首和行末以及多行缓冲区上进行工作。
如果输入是:
|This is a input
输出将是T
如果输入是:
This is a input|
输出将是t
我怎样才能做到这一点?
更新 1:
解决我的问题的代码是:
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
将会把它们打印为
M
提示下方的消息。