6

Before you mark this question as a double, hear me out because I've looked around and I can't find anything that'll fix my issue.

In Vim for Windows (accessed through Powershell running from CMD in a console), backspace works fine. Pressing Shift (or Ctrl) + Backspace prints a weird looking I to the screen and presses Ctrl+C.

Using :set to see what the keycode for the backspace is on my Vim yields this: "Î^Cx". This makes sense considering the behavior described above. The interesting part is that this keycode obviously is for the normal backspace key. My problem however, doesn't appear when pressing backspace normally, which doesn't make sense to me considering that the normal keycode is this weird collection of characters. It only appears when combining backspace with the shift key.

I don't understand why the backspace key doesn't send ^H or ^?. I've heard that those two options are popular. I assume my terminal is messing this up in some way, but I don't understand why it would be sending a different keycode for backspace when the shift key is pressed.

Thanks in advance.

5
  • 1
    What do you expect Shift+Backspace to do? Do you have the same issue in other terminal emulators? Other shells?
    – romainl
    Commented May 7, 2021 at 18:40
  • @romainl The interesting part is that I don't have the same issue anywhere else. It works fine when SSHing to my linux machine from powershell and it works fine when I normally do it in the terminal. It even works fine in other text editors like micro. I don't know why vim doesn't work.
    – Nik Tedig
    Commented May 10, 2021 at 5:54
  • And I have no idea what "works" means for you because Shift+Backspace is not supposed to do anything in Vim, where it is indistinguishable from Backspace. So I'm asking again, a bit differently: why are you pressing Shift+Backspace to begin with? As for the other question, if you have the desired behaviour in other contexts, then it means that the undesired behaviour is caused by something in your current context and the right course of action is to bisect your setup. Also, it is pretty obvious that, in this specific context, Vim is receiving unexpected input.
    – romainl
    Commented May 10, 2021 at 6:52
  • @romainl I use Shift+Backspace a lot when I'm setting up defines in C++. I just hold down shift and type something, and if I make a mistake, I don't remove my finger from shift before deleting the thing that was wrong. This causes a weird looking I to be added to the line, which is very annoying.
    – Nik Tedig
    Commented May 11, 2021 at 6:04
  • @romainl Everyone presses Shift+Backspace when they want to delete a capital letter they just typed. It's quite counterintuitive to have to release the shift key before that.
    – user32882
    Commented Apr 25, 2022 at 9:36

3 Answers 3

3

I solved this issue like this:

imap Î^Cy <BS>

In my (Windows) terminals, Î^Cy is shift-backspace and Î^Cz is ctrl-backspace, and I have remapped them both. If Î^Cx is shift-backspace in your terminal, which it seems like it is, try adding this to your vimrc file:

imap Î^Cx <BS>
3
  • 1
    Thx a lot for this. Everything works great now. I wonder why Windows chooses to send these weird combinations to the terminal instead of just the normal backspace keycodes. The fact that terminal programs can differentiate between Shift + BS and Ctrl + BS and just BS is strange as well, is the same thing possible on Linux for example? If so, how does Linux handle that?
    – Nik Tedig
    Commented Jan 16, 2022 at 22:06
  • Great to hear it worked. This was a frustrating problem for me, too. Terminals in Windows all vary slightly in their implementations, and interpretation of keyboard-input is unfortunately one of those differences. In my experience, CMD.exe, powershell, GitBash terminal, vim (windows) terminal, and Gvim all give different behaviour for certain keys. Linux and MacOS have a native BASH terminal, so there doesn't need to be different emulations made at different times by different people for different purposes. One shell, one standard (one of the reasons developers tend to prefer unix systems).
    – KamRa
    Commented Jan 18, 2022 at 20:03
  • When I press Shift+Backspace in my keyboard I get Îy. I tried adding imap Îy <BS> in my vimrc file but it doesn't help...
    – user32882
    Commented Apr 25, 2022 at 9:22
0

I don't know why this happened in your configuration, but you should be able to use inoremap to make it do what you want.

Interactively, you can test my idea by typing this

:inoremap <hit control-V><hit shift-backspace> <hit control-v><hit backspace>

What this will do is create a non-recursive map in insert mode for the escape sequence generated by <shift-backspace> to the escape sequence generated by <backspace>.

In my case, the result looked like

:inoremap ^? ^?

because my <shift-backspace> produces the same sequence as <backspace>, but in your case it should produce something more useful for you.

Once you've figured this out, move it to your .vimrc without the colon, and that will hopefully fix your issue in a lasting way.

2
  • @ I've now tried this and it doesn't work either. After hitting Ctrl + V on the keyboard, pressing shift + backspace (or just backspace) just exits command mode. I don't remember how, but I did discover what characters actually get sent when pressing backspace and shift+backspace (they were "Î^Cx"). The problem is that both shift+backspace and backspace both send these characters. I don't know how vim even knows that I pressed shift, the keycodes are identical. Even so, even if there was something to fix, vim doesn't let me finish the command because as I said, it just exits command mode.
    – Nik Tedig
    Commented May 19, 2021 at 18:55
  • Wow, I guess all I have to say now is that I don't understand what's going on in your configuration! Sorry, I don't think I can help you actually solve it, with what you're saying here.
    – joanis
    Commented May 19, 2021 at 20:10
0

Are you having the same issue as me I wonder where this happens in Windows Powershell but doesn't happen in gvim or when you run vim under cmd. If so I think the other responders are correct that vim does nothing different with shift+BS than BS but the Powershell command running it is. It's not an answer, I don't know a way around it other than use gvim. But if you're trying to dig further I'd suggest it's Windows Powershell that's the issue (unless I'm wrong of course) rather than vim.

8
  • Answer post should contain relevant answer to the prior issue. Relevant suggestion need to included under comment section
    – cursorrux
    Commented Aug 26, 2021 at 17:17
  • Please add further details to expand on your answer, such as working code or documentation citations.
    – Community Bot
    Commented Aug 26, 2021 at 17:17
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question.
    – Ilario
    Commented Aug 27, 2021 at 12:52
  • You're all right this is not an answer (as clearly stated) however I don't have any SO reputation so can't comment and I spent a lot of time debugging this problem so if anyone else finds the same situation this may help. If someone can move it to a comment that would be better, I cannot.
    – Strottos
    Commented Oct 5, 2021 at 16:48
  • @Strottos then delete it so it doesn't keep getting downvoted and you lose even more reputation
    – user32882
    Commented Apr 25, 2022 at 9:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.