1

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
4
  • 2
    The cursor covers one character, so you wouldn't have INP|UT, you would have IN🄿UT. So should the output be npu? Commented Sep 25, 2024 at 11:53
  • Can i use last char of LBUFFER and first char of RBUFFER? Commented Sep 25, 2024 at 12:13
  • 2
    I don't know, it is your question, you need to tell us what you want. I was just pointing out that your description doesn't make sense since the cursor actually covers a character. Now, please edit the question and clarify what you need. Commented Sep 25, 2024 at 12:21
  • @terdon, whether the cursor is a vertical bar, underscore, or block or square, blinking, reverse video, coloured or not depends on the terminal. Most also support changing the shape and attributes with escape sequences. Try printf '\33[5 q\33]12;green\a' in xterm-like terminals for instance. Commented Sep 26, 2024 at 9:52

1 Answer 1

3
print-char-before-after() zle -M -- $BUFFER[CURSOR,CURSOR+1]
zle -N print-char-before-after
bindkey '^P' print-char-before-after

I use zle -M to print those up to two characters as a Message below the prompt instead of print -r -- $... or echo -E - $... as those would write their output to stdout, going to the same terminal the zsh line editor is interacting with, independently of zle and would confuse the line editor which would no longer know where the cursor actually is.

2
  • I tested it works. I do not see a print or echo, still it is printing. how? Commented Sep 25, 2024 at 17:29
  • 1
    @AhmadIsmail, see edit. Commented Sep 26, 2024 at 6:59

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.