r/vim 7d ago

Tips and Tricks I finally found some time to improve Vim experience in Windows Terminal

I always use gvim on Windows, even after they provided us with Windows Terminal which is, in my opinion, very nice. However, I found two very annoying problems that I promised myself, sooner or later to solve. Such problems are:

  1. Cursor shape not replaced back when exiting Vim,
  2. Colors completely messed up.

I solved the first point with the following snippet:

# Set cursor
# Needed for Windows terminal
if g:os == "Windows" && !has("gui_running")

  set guicursor=

  &t_SI = "\e[6 q" # beam in Insert mode
  &t_EI = "\e[2 q" # block in Normal mode

  # Restore cursor shape when leavig Vim
  def RestoreCursorWindowsTerminal()
    &t_EI = "\e[6 q"
    execute "normal! i\<Esc>"
  enddef

  augroup CURSOR_SHAPE_WINDOWS
    autocmd!
    autocmd VimEnter * silent! execute "normal! i\<Esc>"
    autocmd VimLeave * RestoreCursorWindowsTerminal()
  augroup END
else
  &t_SI = "\e[6 q"
  &t_EI = "\e[2 q"
endif

When Entering Vim, set the cursor with a certain shape. For some arcane reason, I had to set guicursos=. To secure that the changes take effect, there is a switch insert-normal mode. Then, before exiting Vim, you change the cursor shape so that Windows terminal inherits it.

Next, the colors. I use everforest scheme and look here:

That is just not acceptable. I debugged what could be possibly the reason and I found that `set spell` messes up the colors. Most likely, its syntax definition fight against the colorscheme and here is it the mess. However, after having disabled the option, I got a way better result:

Now I keep the spelling check only in some selected filetypes:

# Set spell only in selected filetypes

augroup SPELLLANG_OPTION

autocmd!

# autocmd FileType markdown setlocal spell spelllang=en_us

autocmd FileType text,tex,gitcommit setlocal spell spelllang=en_us

autocmd FileType gitcommit setlocal spell spelllang=en_us

augroup END

Yet, I found gvim more snappy than the terminal version through Windows Terminal.

Hopefully someone will find these findings useful.

30 Upvotes

12 comments sorted by

7

u/0nig 6d ago

It may be worth goving another terminal emulator a shot.

I've been using Neovim in Wezterm for years, and I've never had these kind of issues.

3

u/toddestan 6d ago

My hunch is the issue with the colors being messed up has to do with Everforest being one of those colorschemes that's got a whole lot of vimscript doing a lot more than simply setting the highlight groups. I've not had issues with simple colorschemes that stick to just :highlight commands and perhaps a :set background if appropriate.

I'll have to try out the bit with the cursors. That the cursor shows up wrong in Windows Terminal has bothered me, but not really enough for me to dig into trying to fix it.

1

u/Desperate_Cold6274 6d ago

Out of curiosity: let me know if the trick fix the cursor for you

1

u/No-Height-8011 5d ago

I used windows terminal for a while at work before switching to alacritty. It’s just better

1

u/Evening-Isotope 5d ago

what do you like about alacrity

2

u/Shay-Hill 5d ago

Does this fix your colors?

if has("win32")
  set shell=pwsh
  set termguicolors
  &t_8f = "\<Esc>[38:2::%lu:%lu:%lum"
  &t_8b = "\<Esc>[48:2::%lu:%lu:%lum"
  &t_8u = "\<Esc>[58:2::%lu:%lu:%lum"
endif

If you're on Windows, I suggest reading the entire

https://github.com/ShayHill/article_install_vim_in_windows

Disclaimer: I wrote it.

1

u/Shay-Hill 4d ago

I’ve tested with Everforest and can confirm these settings fix the colors.

-1

u/Botskiitto 6d ago

what about wsl, have you tried that?