Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why VI always starts in insert mode?

At some point, whenever I use vi from the command line (windows 10/WSL) it starts up in insert mode, meaning that everything I usually do to navigate ends up adding stuff to the file, wasting time having to clean it up.

I didn't have a vimrc file, and there doesn't seem to be anything in my bash rc files to modify vi behavior. Any ideas what I may have done or any ideas how to stop this behavior? I'm using Ubuntu-20.04

FWIW, adding a .vimrc file with tab related settings didn't change it's behavior. I looked at the /etc/vimrc file, and nothing inside it seemed relevant.

like image 721
user632657 Avatar asked Nov 27 '25 10:11

user632657


2 Answers

I have been a long-suffering victim of this problem, but today decided to try to find a solution for it, and found this thread. Since it did not have a real good solution, and other pages/threads did not either, I looked into the problem and found a solution I thought was worth sharing.

The short answer: set your TERM variable to vt100

The default value is TERM=xterm-256color. Either change TERM from your command line, or in your .bashrc or .bash_aliases file, just add the line

export TERM=vt100

The "export" is probably implied, but does not hurt to use it.

I actually just guessed that might work and so found this solution quickly. But then I spent a lot more time investigating to see if there was a solution even better than that, or that explained why it worked, or why the problem occurred, and learned: (1) The terminal emulator that runs with debian-on-WSL1 is a Windows Console, which was specifically designed to emulate a vt100, which an xterm is based on, but is missing some capabilities and as we can see, does not fully emulate the xterm that terminfo is targeting; (2) Windows Terminal does not have the problem, it must have an improved design and does emulate an xterm more completely, so you could alternately start a Windows Terminal and run "wsl" or "debian" in a tab, and then would not have to change TERM to vt100; (3) The bad behavior results from an unintentional "2R" vim command, as the bottom status bar shows "REPLACE" not "INSERT", and the "2" must be there because text typed in gets replicated once; so the "2R" probably is part of a control string that Windows Console does not support.

like image 140
skt Avatar answered Nov 29 '25 09:11

skt


After plowing through google search and trying everything I understand about configuring 'vim' and doing comparison tests, I think:

  1. This behavior is specific to Windows Terminal when opening a WSL terminal. Using WSL's "native" terminal (i.e., clicking the "Ubuntu 20.04 LTS" menu in the "Start" menu) doesn't have this problem.
  2. My original motivation for switching to Windows Terminal is for its multi-tab feature. But this new behavior is crazy -- it works against years of my muscle memory of using "vi", and I'm almost certain that one day I'll accidentally update some configuration file while reading it in "vi". And, I cannot re-train a new muscle memory because all the rest of the UNIX world (e.g., when I SSH into a remote server) hasn't changed. This is like constantly switching between a Mac keyboard and a PC keyboard where the Ctrl key, etc., are in different places.

My solution: I switched to MobaXterm. It has multi-tab support, and is actually richer in features compared to Windows Terminal.

like image 41
Vincent Yin Avatar answered Nov 29 '25 09:11

Vincent Yin