1

I want to create a vimrc mapping that lets me resize my vertical splits a little more quickly. I thought about doing this:

noremap <c-[> :vertical resize +5<cr>
noremap <c-]> :vertical resize -5<cr>

and it works pretty well. The only problem is, because I am mapping (control and left square bracket), when I hit Escape in normal mode it causes the splits to be resized as well. I assume this is because you can normally hit as an alternative to escape.

So the question is, can I map that key sequence but not map Escape? Am I missing something dumb here?

Matt Dodge
  • 10,833
  • 7
  • 38
  • 58

1 Answers1

5

What you are trying to do is a tricky thing.

There is a relatively modern idea that what the user types is a keycode, with a "chord" of modifiers; thus Ctrl+[ is the keycode for [ combined with holding down the Ctrl key. If you were writing a native Windows application, or a native GNOME application or whatever, then you could gain access to this level of information.

However, old "dumb terminals" only sent pure ASCII, and I'm pretty sure that the library functions vim uses deal in ASCII characters. vim sees a stream of characters as the user types them, with no ability to check on the keycodes or modifiers.

You might be able to do what you want with one of the GUI flavors of vim, but my guess is that the character-based ones won't do it.

But I think it's best to do something else anyway. For one thing, Ctrl+] already has a useful function in vim.

Here's what I suggest: bind a sequence that starts with [ or ]. In vi and vim, [[ and ]] are valid navigation key sequences, and you can bind similar sequences without blocking their use.

For example, you could bind [v and ]v for the two, as "vertical resize" starts with a v.

In classic vi, I am certain that [v and ]v are not bound to anything by default. In vim, I am pretty sure they aren't bound to anything either. (vim has so many features I hesitate to make an absolute pronouncement! But I didn't find anything in the manual and vim just beeps when I try typing those.)

As another alternative, you could bind function keys. My function keys are all unused in vim.

Note that vi lets you use function keys even when the keyboard doesn't have any; you can type #2 and it invokes the macro bound to function key F2. (I used to use a dumb terminal, the ADM-3A, that didn't have function keys.)

vahid abdi
  • 9,636
  • 4
  • 29
  • 35
steveha
  • 74,789
  • 21
  • 92
  • 117