microsoft / terminal

The new Windows Terminal and the original Windows console host, all in the same place!
MIT License
94.53k stars 8.17k forks source link

No cursor after exiting vim #10754

Open polluks opened 3 years ago

polluks commented 3 years ago

Windows Terminal version (or Windows build number)

1.8.1521.0

Other Software

vim 7.4.576 (via SSH inside WSL)

        linux-vdso.so.1 (0x00007ffffd7f6000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f151d9c1000)
        libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007f151d797000)
        libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f151d572000)
        libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007f151d369000)
        libgpm.so.2 => /usr/lib/x86_64-linux-gnu/libgpm.so.2 (0x00007f151d163000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f151cf5f000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f151cbb4000)
        libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f151c946000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f151e0ef000)
        libattr.so.1 => /lib/x86_64-linux-gnu/libattr.so.1 (0x00007f151c741000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f151c524000)

Steps to reproduce

A few times start vim, switch the window and wait...

Expected Behavior

Everytime a flashing cursor after switching back.

Actual Behavior

Sometimes the cursor disappears, reset(1) helps.

DHowett commented 3 years ago

Looks like vim never restores the cursor blink status. This "regressed" due to the fix for #10543. Terminal used to ignore the request for a non-blinking cursor. Now it respects it, but vim never turns blinking back on.

There may also be an issue where we let them turn off blinking while the cursor is off.

polluks commented 3 years ago

@DHowett In November vim's 30th anniversary takes place. I suppose it's pretty stable. Indeed, the issue depends on the cursor state. Maybe it's related to this message: E72: Close error on swap file. In VS Code's xterm.js it works.

Nosamdaman commented 3 years ago

It's worth noting that I'm seeing a similar issue with tig. For both vim and tig, if the cursor stops blinking once I exit the application.

polluks commented 3 years ago

Fun fact: The cursor is off, I start vim again and see the cursor! I leave vim and the cursor is off again...

j4james commented 3 years ago

In the case of vim, you can get it to reset the cursor on exit by tweaking the t_ti and t_te options. For example, I have these lines in my .vimrc file (note that ^[ is Ctrl+[).

set t_ti=^[[1049h
set t_te=^[[1049l^[[0\ q

On startup that switches to the alt buffer (CSI 1049h), and on exit it switches back to the main buffer (CSI 1049l), and resets the cursor (CSI 0 q). I don't know if this is the best solution, but it works for me.

It's worth noting that I'm seeing a similar issue with tig.

In the case of tig, the current behaviour is also "by design". They're essentially asking for the cursor blinking to be disabled on shutdown. They're executing the cursor_normal terminfo entry, which for xterm-256color is \E[?12l\E[?25h (i.e. cursor blink off and visibility on). You'll get the same result in XTerm. I don't know if there's a way to work around that, but you should probably raise the issue with tig.

polluks commented 2 years ago

@j4james It works, but you are just fixing the symptoms.

j4james commented 2 years ago

Well if you want to fix the root cause then you'll probably need to take this up with vim. They're the ones that are turning the blinking off. I don't think it's anything specific to Windows Terminal, because I've seen the exact same behaviour in several other terminals, including XTerm , Rxvt, and Alacritty. The only terminals I've seen that weren't affected were those that simply don't support the cursor blinking mode, so they're only "working" accidentally.

polluks commented 2 years ago

Well, at least Xterm.js supports DECSCUSR https://github.com/xtermjs/xterm.js/blob/e746cd55ddafb9120e3f00cf00e4360d14a531a4/src/common/InputHandler.ts#L176 BTW there are users who use real blinking hardware https://www.reddit.com/r/commandline/comments/onagx8/adapt_vim_syntax_highlight_to_vt100/

j4james commented 2 years ago

Well, at least Xterm.js supports DECSCUSR

Yeah but vim is using private mode 12 to disable the cursor blinking, and Xterm.js doesn't appear to support that. https://github.com/xtermjs/xterm.js/blob/e746cd55ddafb9120e3f00cf00e4360d14a531a4/src/common/InputHandler.ts#L1948-L1950

Technically I suspect it's just using the cursor_normal terminfo entry, the same as tig, and as I pointed out above, that disables mode 12 and enables mode 25 for xterm-256color.

polluks commented 2 years ago

Just for the record https://invisible-island.net/ncurses/ncurses.faq.html#emacs_cvvis

TrT-TOT commented 2 years ago

Well if you want to fix the root cause then you'll probably need to take this up with vim. They're the ones that are turning the blinking off. I don't think it's anything specific to Windows Terminal, because I've seen the exact same behaviour in several other terminals, including XTerm , Rxvt, and Alacritty. The only terminals I've seen that weren't affected were those that simply don't support the cursor blinking mode, so they're only "working" accidentally.

I use Xshell previously and the cursor blinks properly before and after i use vim. So I hope if someone could find a way to solve the problem...

tyjyang commented 2 years ago

Since this issue has been moved to the backlog and we could wait a while before an official fix comes out, I worked out a hacky solution for vim. Here is how it works (lines to be put in .vimrc):

" set cursor to be a solid box in normal mode
let &t_EI .= "\<Esc>[2 q"
" set cursor to be a blinking box in replace mode
let &t_SR .= "\<Esc>[1 q"
" set cursor to replace mode when leaving buffer in all windows
" revert cursor back to normal mode when entering buffer
autocmd BufEnter * execute 'silent !echo -ne "' . &t_EI . '"'
autocmd BufWinLeave * execute 'silent !echo -ne "' . &t_SR . '"'

The idea here is that the terminal will take the last cursor style in vim before exit. So I set a blinking cursor for the replace mode, and use autocmd to start that mode when the buffer is exited on all windows. In order to keep the cursor solid after switching to another file without exiting to the terminal, I also start the normal mode with autocmd when entering a buffer.

This is not meant to be perfect. Let me know if you find any problem with this, or if you have better solutions.

zadjii-msft commented 2 years ago

From @j4james in #12372

I think I know what's happening now. It looks like Mintty ignores the blinking escape sequence (private mode 12) if you've set your default cursor preference to be blinking, so that's why it appears to be working. Emacs is still requesting that the cursor stop blinking, but Mintty is simply ignoring that request.

BTW, after I quit Emacs (and cursor stops blinking), restarting Emacs the cursor returns to blinking in Emacs; exiting one more time, it returns to stop blinking...

That is exactly what I would expect. They're likely using the cvvis terminfo capability on startup (which enables cursor blinking), and the cnorm capability on shutdown (which disables blinking).

The bottom line is that this is a duplicate of #10754 and is really an Emacs problem. You can probably work around it by editing your terminfo if you don't ever want applications to stop your cursor blinking.

The mintty approach sure is an interesting one, but probably not the most broadly compatible experience

angelog0 commented 1 year ago

For completeness (see #12372), I just discovered that MinTTY people has enabled by default the MinTTY option

SuppressDEC=12

See https://github.com/mintty/mintty/wiki/Tips#blinking-cursor-reset

gtirloni commented 5 months ago

This is not exclusive to vim. Running top also causes the cursor to stop blinking.

As much as I agree the issue is with the apps themselves and Microsoft Terminal is doing the right thing here, maybe an option to ignore that request like it was traditionally done would be nice.

Terminal 1.12.10983.0 Ubuntu 22.04.4 (Jammy) Vim 8.2 procps-ng 3.3.17