Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cursor styling changes when changing tmux windows #23

Closed
kyounger opened this issue May 12, 2020 · 22 comments
Closed

Cursor styling changes when changing tmux windows #23

kyounger opened this issue May 12, 2020 · 22 comments

Comments

@kyounger
Copy link

Not really sure which side of the zsh/tmux fence this sits on, but wonder if it could be handled here at least.

Note: Normal mode==block cursor, insert mode==bar cursor

I can get the cursor styling working, but if I Escape into Normal mode in one tmux window pane and then switch to another pane that is in insert mode, my cursor styling is overriden and I get a block cursor on the pane that is in insert mode. Or, vice versa if I'm in insert mode and switch to a pane that is in normal mode.

Maybe this is a tmux thing that needs to trigger zle to do something? Or maybe tmux is and it's just not handled in zsh-vim-mode? Not really sure where to start debugging this. Please feel free to tell me to go ask in tmux/tmux ;)

For testing this is my .zshrc:

HISTFILE="${ZDOTDIR:-$HOME}/.zhistory"
HISTSIZE=290000
SAVEHIST=290000

PS1="READY > "

export MODE_CURSOR_VIINS="#00ff00 blinking bar"
export MODE_CURSOR_REPLACE="$MODE_CURSOR_VIINS #ff0000"
export MODE_CURSOR_VICMD="green block"
export MODE_CURSOR_SEARCH="#ff00ff steady underline"
export MODE_CURSOR_VISUAL="$MODE_CURSOR_VICMD steady bar"
export MODE_CURSOR_VLINE="$MODE_CURSOR_VISUAL #00ffff"

source /Users/kyounger/code/testzsh/zsh-vim-mode/zsh-vim-mode.plugin.zsh
@softmoth
Copy link
Owner

Thanks for reporting this. I believe the most this plugin could do is send the cursor styling sequence to the terminal with every keypress. Would you please test if this provides a better experience for you: 3d59a02

If it does, I'd like to run only the cursor styling hook every keypress, so that will take some extra coding.

@softmoth
Copy link
Owner

By the way, I don't think tmux can handle this. Not cleanly, anyways. It could intercept the special sequence that changes the cursor style, and store that for each pane, and send it each time you switch panes. Maybe that would be a decent solution (and it would be more accurate than what this plugin can do alone), but I think it might be messy if multiple users are observing the same tmux session or in other complex situations.

I could see it working, but it might be complicated. Doing what we can here should be relatively simple and self-contained.

One other thing tmux might be able to do is send some signal to ZSH when it focuses on a pane. This feels very error prone, though, as it would need to ensure that the pane contains a ZSH that is sitting at a prompt (not running some batch commands or something). But those concerns aside, it might be possible to trap the signal with this plugin and spit out the cursor styling sequence without waiting for a key press. This feels way too brittle to pursue, but I wanted to mention it in case it prompts another reader to have a much better idea.

@nicm
Copy link

nicm commented May 13, 2020

tmux has supported per-pane cursor styles since tmux 1.5. Focus events are supported since tmux 1.8, but I don't know if zsh can handle them.

@softmoth
Copy link
Owner

Thanks, nicm! I'm behind the times. Looks like I've got some reading to do.

@nicm
Copy link

nicm commented May 13, 2020

Check if TERM outside tmux has Ss and Se (infocmp -x|grep S[se]) and if not add this to .tmux.conf and restart tmux:

set -as terminal-overrides ',*:Ss=\E[%p1%d q:Se=\E[2 q'

Then obviously don't use the DCS tmux; wrapper sequence or tmux won't know about the escape sequences.

@softmoth
Copy link
Owner

OK, I think the main lesson here is, never say tmux can't do something. All that's needed is to disable my old workaround, and per-pane cursor styling works very nicely. I'm grateful for your help, @nicm!

@kyounger
Copy link
Author

Sorry, I didn't follow the fix exactly. Was there something I needed to update in my tmux config? I pulled the latest commits from zsh-vim-mode, but am still seeing the issue.

I tried adding what @nicm posted in the comment above to my .tmux.conf, but that didn't seem to help either.

@softmoth softmoth reopened this May 13, 2020
@nicm
Copy link

nicm commented May 13, 2020

Did you restart tmux entirely after changing .tmux.conf? tmux kill-server

@kyounger
Copy link
Author

Ok, I checked and it doesn't seem iTerm2 is supported for this, unless I'm confused by something. (That does seem a bit odd, it usually supports everything)

infocmp -x|grep S[se] returns nothing.

@nicm
Copy link

nicm commented May 13, 2020

Right, that is why you use terminal-overrides to tell tmux it can do it even though terminfo doesn't have the entries.

@kyounger
Copy link
Author

Ok, I added that line to my .tmux.conf, but no dice. Are there escape sequences in it that I need to ensure are encoded properly vs just the ascii chars?

@nicm
Copy link

nicm commented May 13, 2020

What does tmux info|grep S[se]: show inside tmux now?

@kyounger
Copy link
Author

Same. No matches.
image

@nicm
Copy link

nicm commented May 13, 2020

But that isn't what I asked you to try?

@kyounger
Copy link
Author

Whoops, sorry. Still missing:
image

@nicm
Copy link

nicm commented May 13, 2020

Did you restart tmux? Do this: tmux kill-server.

If it is still the same, show me the output of tmux show -s terminal-overrides.

@softmoth
Copy link
Owner

@kyounger, ensure that $TMUX_PASSTHROUGH is not set to 1 in the shell. That's all that should be needed on the .zshrc side.

You can open a new terminal window (without tmux running) and run infocmp -x|grep 'S[se]'. For me, that shows:

12:52 ~ $ infocmp -x | grep 'S[se]'
	Ms=\E]52;%p1%s;%p2%s\007, Se=\E[2 q, Ss=\E[%p1%d q,

Make sure you're quoting the arg to grep: in your previous response it was zsh which reported the "no matches found", which is because the 'S[se]' wasn't quoted from the shell.

Only if yours doesn't do you need to modify the .tmux.conf to set terminal-overrides.

@nicm
Copy link

nicm commented May 13, 2020

OS X's terminfo database is ancient and definitely doesn't have Ss/Se in xterm-256color, he will need terminal-overrides.

@nicm
Copy link

nicm commented May 13, 2020

Or to build tmux from master where it can automatically detect iTerm2 :-).

@kyounger
Copy link
Author

That was it!!! I had TMUX_PASSTHROUGH set. Unsetting it solved this and made it all work. 🎉

Holy smokes that has been a thorn in my side for so long. Constantly niggling me. HUGE thanks for this fix and educating me a bit on all this.

@nicm
Copy link

nicm commented May 13, 2020

Great stuff.

@softmoth
Copy link
Owner

Yay! Thanks for sticking with it, @kyounger, and I hadn't realized how much this annoys me until it's now fixed. It feels so much more solid now! Thanks for the excellent bug report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants