How do I find the length of readline's mode string (eg @ or (cmd) or (ins)) which is printed in front of the bash prompt? This mode string is activated by set show-mode-in-prompt on in .inputrc.

I want to determine the length of this string from within $PROMPT_COMMAND.

I'm trying to do some cursor movement with tput inside the prompt to print some info on the terminal's right hand side (so that it can be overwritten if need be), then resume printing the prompt on the left, but I need to take into account the length of the mode string.

Is $PROMPT_COMMAND called again and the prompt reprinted if changing mode (e.g) emacs-mode-string to vi-ins-mode-string as the prefix?

  • 1
    As a workaround, I believe you can print a screen control code to remember the current cursor position, then pop back to there once you are finished with your digression. I don't remember the terminology used in tput for this, though.
    – tripleee
    Apr 25, 2017 at 13:24
  • I found this in an old answer of mine: tput sc to capture the current cursor position andètput rc` to return back there.
    – tripleee
    May 1, 2017 at 14:48
  • Or using ANSI CSI codes: \e[s and \e[u.
    – Tom Hale
    May 2, 2017 at 4:51

1 Answer 1


Not really an answer to the question regarding mode string length, but a workaround:

I ended up using ANSI CSI codes to save and restore the position of the cursor. (Thanks @tripleee for the pointer).

I was still unable to find the length of the mode string, even when tying to get the cursor position using PS1=$(getColumn) where getColumn was various things I tried based on:

My work-around using ANSI CSI codes can be found as in answer to "Right align part of prompt".

I'm pretty pleased with the work-around as I manged to do it without a single sub-process, and make it shellcheck-clean.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .