6

My Vim on MinGW 4.6.2 is behaving weirdly, for example, pressing Backspace in insert mode deletes characters, but I have to move the cursor with arrow keys before the deleted characters disappear, and it also takes me out of insert mode.

Another example, pressing Del to delete characters sometimes generates weird characters like changing lowercase characters to uppercase, or corrupts the next character that I key in, for example pressing 'S' gives me '$' initally.

Am I using Vim wrong or something? I'm not too unfamiliar with Vim so I'm not sure if this is default behaviour, but the typical Vim behaviour as I understand is like the Vim in Git Bash, where Backspace and Del work like they do in NotePad.

Cardin
  • 5,148
  • 5
  • 36
  • 37
  • 1
    Which terminal program are you using? Can you use `gvim` instead? That is sometimes easier than fighting the terminals... – sarnold Jun 06 '12 at 01:52
  • what do you mean terminal program? I think the one I'm used to in Git Bash is msys, since the font/color looks identical to MinGW... I would prefer to use vim for the challenge haha, there's no lack of higher-level editors if I really wanted to.. – Cardin Jun 06 '12 at 08:06

4 Answers4

13

I also have problems with using vim in MinGW, specifically having to do with mouse wheel scrolling. But the problem you describe sounds like vi compatibility.

Have you tried the following settings in vimrc?

set nocompatible
set backspace=2
allenylzhou
  • 1,431
  • 4
  • 19
  • 36
4

The thing is, backspace is often ^H and delete is often ^? -- and some idiot terminals swap the two or have both provide the same character. When some terminals are really wrong, a keypress will generate ESC~<something> and the ESC will put vim into command mode, then the ~ will swap the case of characters, and so on.

You've also got the terminfo or termcap databases of terminal capabilities to contend with -- if your TERM environment variable does not match the running terminal exactly, the wrong capabilities will be selected and the application will generate the wrong escape sequences to properly control the terminal.

Of course, running on Windows further complicates everything because terminals aren't very native to the platform -- CMD.EXE does its own thing, MSYS does another, and vim is probably expected bog-standard CMD.EXE just because it is quite common.

All these are reasons why I'd recommend using gvim; it starts a GUI window that behaves almost exactly like the text-mode vim, but it is much more predictable in its behavior and you can configure it entirely within gvim -- fixing vim's behavior might actually mean changing the MSYS configuration, which may break other programs. I dislike the way gvim starts a "new context" rather than just editing in the environment I'm already using but gvim feels way less awkward under Windows.

sarnold
  • 102,305
  • 22
  • 181
  • 238
  • Okay, I just test-runned gvim and it is indeed much easier to use! I liked how it's basically just a white background vim with menu bars. And in addition, thanks for the explanations, I didn't quite get it but I feel more knowledgeable already haha. It's weird that something so fundamental as vim still has issues with cross-platform input schemes. :) – Cardin Jun 07 '12 at 14:40
  • It doesn't even need to have the menu and the toolbar, if you do `set guioptions-=mT` :) – Amadan Jun 07 '12 at 14:58
2

Your vim is behaving correctly - just in a mode where it detected a low-capability terminal, and doesn't want to touch the screen too much (the S issue - $ marks the end of the region that will be deleted, but it waits till you're done inserting so it only needs to repaint the rest of the line once). Bad terminal emulation can also send rubbish when you press certain keys.

gvim, that @sarnold suggested, is just a build of vim which works in a window, not another editor. It is still full vim, and there is still the challenge. If you're stuck on a Windows box, this is a much better choice than a terminal vim, since I am not aware of a single terminal I liked working in (and I'm including Cygwin).

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • I tried standalone gvim and indeed it's similar to vim, so that's good. Bad thing is, I'm using vim to practice using make and gcc on the MinGW toolchain. So while I could use Cygwin's gvim or the standalone gvim, it really defeats the purpose of not ever leaving the MinGW terminal while messing with gcc. It's either MinGW's vim or nothing. :( So I've decided to just use NotePad++ instead. Which is kinda sad.. but at least I can still use vim while doing Git source control using its bundled vim on msys. :) – Cardin Jun 07 '12 at 14:36
0

You can check the characters sent to Vim the following way:

  1. start command mode by pressing :
  2. push Ctrl + v
  3. push the desired key (Backspace or Del in your case.)

So you will see the magical character sequence sent by the terminal to vim.

For example, in my case I saw <C-Del> when I pushed Backspace, so I could repair my backspace key by inserting the following code into my .vimrc:

"eg. in case of xterm terminal
if &term="xterm" "repair backspace: inoremap nnoremap fi

Mattia72
  • 825
  • 1
  • 8
  • 23