terminal: Vim cursor can't be changed
Environment
Windows build number: 10.0.19041.21
Windows Terminal version : 0.8.10091.0
Steps to reproduce
- Open vim
- Set options:
set background=dark
colorscheme desert
highlight Cursor guifg=white guibg=black
Expected behavior
In old terminal you know when you’re in normal mode
Actual behavior
Cursor can’t be changed, so it’s hard to work
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 12
- Comments: 33 (8 by maintainers)
Commits related to this issue
- Add fixes for Vim inside Windows Terminal Fixes certain mouse controls and the cursor's shape when using vim on WSL inside a Windows Terminal. Fixes found at: microsoft/terminal#5454 microsoft/termi... — committed to zirajput/Dotfiles by zirajput 4 years ago
- Change cursor style References: https://vim.fandom.com/wiki/Configuring_the_cursor https://github.com/microsoft/terminal/issues/4335 — committed to bschiela/dotrc by bschiela 3 years ago
- :new: add cursor (apesar disso ele não muda no vim) https://github.com/microsoft/terminal/issues/4335 — committed to neninja/dotfiles by neninja 3 years ago
- (chore) put cursor commands inside au group (Issue) cursor does not reset on exiting from vim - https://github.com/microsoft/terminal/issues/4335 (Issue : #5) — committed to PrashanthaTP/dotfiles by PrashanthaTP 3 years ago
- (chore) put cursor commands inside au group (#5) (Issue) cursor does not reset on exiting from vim - https://github.com/microsoft/terminal/issues/4335 — committed to PrashanthaTP/dotfiles by PrashanthaTP 3 years ago
- (bugfix) set cursor shape to vertical bar on leaving vim why: to distinguish between command and normal modes of vi mode in powershell how: using VimLeave event and guicursor option misc: see :help... — committed to PrashanthaTP/dotfiles by PrashanthaTP 3 years ago
- (bugfix) fix #5, set cursor shape to vertical bar on leaving vim why: to distinguish between command and normal modes of vi mode in powershell how: using VimLeave event and guicursor option misc: s... — committed to PrashanthaTP/dotfiles by PrashanthaTP 3 years ago
It worked, thanks!
I’m leaving this code for the lost souls that might search it
How to change cursor in vim in windows terminal
Works for vim in wsl (not powershell)
In this example, normal mode is a block, insert mode is a vertical bar
Ah, it looks like the documentation here is confusing. The
guicursor
settings are for GUI vim (gvim, xvim, mvim, etc.). You’ll want the ones at the bottom that mentionxterm
andrxvt
, with thet_SI
andt_EI
control sequences.There may be an issue in the regular expression. My configuration looks like this:
I should note that for whatever reason using option 0 does not give blinking block in wsl and still outputs a vertical bar. So replace
let &t_EI .= "\e[0 q"
bylet &t_EI .= "\e[1 q"
and similarly when you exit vim. Here is the snippet that works for me.Closing this as solved is kind of frustrating, as the original issue was reported for Powershell sessions, while the “solution is” basically, “don’t use powershell, use linux”, which isn’t reaaally the answer I’d hope for. Ideally running vim in a powershell session would be comfortable without having to use WSL.
so this is the best solution:
So the config posted by @eduardoarandah works in vim but appears to have the side effect of leaving the cursor in whatever state it was when Vim exited (most likely a block in normal mode), so the terminal ends up with a block cursor rather than the default.
(This is in ubuntu running in WSL)
The use case permutations are very vast and most posters report on one instance only. Everybody posting solution is driven by good intention. However, please do bother to state what environment the solution you offer pertains to to save a brother an hour.
Solution
The following solution pertains to:
Create a file called .vimrc in your home directory C:\Users\Elmo with following content
6:vert-bar,4:underscore,2:block are the non-blinking cursors for the three vim modes.
This will make vim use the mode dependent cursors. Problem: upon exiting vim, the cursor you end up with in the Powershell prompt is block non-blinking. If that’s not what you want, edit your profile
Add the following function
That echo is a command to tell powershell to set the cursor to 5:blinking-vertical-bar in this case. Save. Exit.
Known Problem
When starting vim from Powershell through Windows Terminal the initial cursor should be block, non-blinking, however it’s the vertical bar blinking (whatever came from the prompt). Once you change modes, the expected vim cursors will be used. That is not the case if you use the standalone Powershell app.
Already Tried, Don’t Bother
Using the VimLeave event in .vimrc to execute set cursor
Like to mark this useful.
I had the same problem @lllama and @heaths had (cursor stuck as block in terminal). The code below from stackoverflow allowed me to recover the cursor without modifying my .bashrc as @dennisjlee suggested.
autocmd VimLeave * silent !echo -ne "\e[6 q"
To follow up on @robertsk50 , it looks like when we enter vim for the first time, the cursor shows a vertical bar instead of block even though we enter in normal mode. Luckily, the link in @robertsk50 provides a solution:
Here’s a workaround for resetting the cursor to a vertical bar after exiting vim.
Add this to your .bashrc so that it will set the cursor to a blinking bar every time it renders a prompt. (Other styles are listed in this StackOverflow answer)
export PROMPT_COMMAND='echo -e -n "\x1b[\x35 q"'
For me also none of the solutions work. This is the last issue I have with Vim on Windows (and well maybe that pasting from clipboard is not in line).
I encountered the problem that an escape sequence is output at starting Vim. I also encountered the problem that the terminal cursor even changed after suspending Vim with Ctrl+Z. I solved these problems by adding the following to previous solutions.
Confirmed what @lllama is seeing: when I exit vim from normal mode, the Terminal cursor is not a block cursor.
This works fine in conhost consoles (cmd, powershell, pwsh).
According to the Vim documentation, it should be applied after executing
set termguicolors
orset t_Co=256
:@iovuio Thank you for this information, but adding the following to my
.vimrc
doesn’t change the behavior of the cursor on Vim for Windows (run from cmd.exe or powershell).It does change the cursor on Vim on Linux/WSL though (I had already had it set).
I just thought that my setup may differ because I use clink. Maybe this is having an effect? I can’t think of any reason why this setting wouldn’t work for me if it is the correct one to be used on Vim for Windows when using Windows Terminal.
This worked for me, I’m using PowerShell: https://github.com/microsoft/terminal/issues/68#issuecomment-418438517
@eduardoarandah
I had the same issue. The easiest workaround is to set
cursorShape
under required profile insidesettings.json
. See this.However, this will affect the entire profile instead of just vim.
The solution pasted by @gao-lex does not work for me, is it only for WSL?
I’m using PowerShell and cmd.exe: Windows version: Windows 10 Version 2004 (OS Build 19041.630) Windows Terminal version: Version: 1.4.3243.0 Vim is the official one for windows: 8.2, and I’d like to run vim inside the console, by typing
vim.bat FILE
In PowerShell or cmd.exe, normal mode cursor is block, insert mode cursor is underscore. But even when I paste the content to my
.vimrc
, the cursor inside Windows Terminal is always a vertical bar. This is very confusing in Vim normal mode.I can be sure that
.vimrc
is correctly located, and sourced by Vim when it launches.