How to Make Zsh Widget To Retrieve Cursor Row? I Can Only Make This Work Manually Entering A Oneliner

I’m facing an issue creating a Zsh widget, I want it supposed to print the current terminal row when I enter a key combination.

However I can’t get it to work as a zsh widget, I can only get it to work as a oneliner.

I suspect the issue is because the escape characters have to be manually entered in.

The Code:


function print-current-terminal-row-widget() {

    echo -en "\e[6n"

    read -sdR CURPOS

    CURPOS=${CURPOS#*\[}

    echo "Current terminal row: ${CURPOS%;*}"

}

zle -N print-current-terminal-row-widget

bindkey '^X' print-current-terminal-row-widget

It doesn’t work. When using the above I get this output:


Current terminal row:

;6R

Working Example:

When I run the following command directly in the terminal, it works and returns the number of the current cursor row:


echo -en "\e[6n"; read -sdR CURPOS; CURPOS=${CURPOS#*\[}; echo ${CURPOS%;*}

Is there another way to get the current cursor row without manually typing in the escape characters, so that a widget can retrieve the Row number in the background?

  • I don’t think there is a problem with the escape character; after all, your read does get some response. Is the answer ALWAYS ;6R, independent of the line the prompt happens to be?

    – 

Leave a Comment