1

I would like to set up a ZSH hook to set the terminal window title to contain information such as the previously entered command and the PWD. The naïve window title-setting shell function would look like this:

title() { printf "\033]0;%s\007" "$*" }

But, in basic usage, this proves to be completely inadequate. It is very easy to interrupt the ANSI escape sequence with various special characters, such as ;. I have tried explicitly substituting ; with \; which does not work. How can I escape arbitrary sequences of characters so that a valid escape sequence can be generated to set the window title correctly?

EDIT: This problem actually seems to be a quirk of st. I will post the solution I figured out below.

2
  • 1
    What terminal emulator are you using? ; is fine for me with xterm, screen, gnome-terminal, Eterm and rxvt here. Commented Nov 10, 2020 at 5:57
  • I am using st.
    – lemonhuman
    Commented Nov 10, 2020 at 18:44

1 Answer 1

1

st interprets the ; as delimiting another argument in the escape sequence. st sets the window title to the contents of the first argument only. To fix this, I have replaced the code handling the specific escape sequence from this:

if (narg > 1)
    xsettitle(strescseq.args[1]);

To this:

if (narg > 1) {
    char *buf = malloc(1);
    buf[0] = '\0';
    int n = 0;
    for (int i=1;i<narg;i++) {
        buf = realloc(buf, n+=(strlen(strescseq.args[i]) + 1));
        strcat(buf, strescseq.args[i]);
        if (i + 1 != narg)
            strcat(buf, ";");
    }
    xsettitle(buf);
    free(buf);
}

This may or may not be a good solution.

You must log in to answer this question.

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