Binding Ctrl+Tab and Ctrl+Shift+Tab difficulties with Vim and PuTTY.

1,512 views
Skip to first unread message

Danny

unread,
Apr 29, 2009, 12:25:21 AM4/29/09
to vim_use
I have been attempting to bind Ctrl+Tab and Ctrl+Shift+Tab as
the :tabnext and :tabprev commands in vim. From what I have learned,
PuTTY does not send these commands. I found a post describing how to
patch putty to send these keys along. (http://scnr.net/blog/index.php/
archives/61). However, the keycodes it suggests are not ones that vim
is expecting. I'm assuming there is some set of keycodes that would be
appropriate, however I do not know what they are. I'm hoping someone
here might be able to shed some light on this.

I have looked through this mailing list, and found a similiar question
"Problems with mappings using the Shift in the terminal" (http://
groups.google.com/group/vim_use/browse_thread/thread/8449c75c87c7ef4).
I investigated one of the responses that suggested using ^V to see
what vim recieves from the terminal. From what I can tell the tab key
comes through as well, a tab. Not very helpful. I'm not sure how to go
about dealing with this.

I've also asked this question on SO a couple of days ago which is
where I was able to get some leads (http://stackoverflow.com/questions/
736004/creating-a-ctrltab-keybinding-in-putty).

If anyone is able to lend more information, I'd love to hear it!

Thanks.

Danny

unread,
May 21, 2009, 1:21:46 PM5/21/09
to vim_use
I have yet to find a solution for this problem. If anyone is able to
direct me to any other resources where I might find some answers it'd
be much appreciated!

Reformatted the links to save them from being mangled?
Patching putty to add key codes:
http://scnr.net/blog/index.php/archives/61
Stack Overflow question:
http://stackoverflow.com/questions/736004/creating-a-ctrltab-keybinding-in-putty
Problems with mappings using the Shift in the terminal from this
vim_use mailing list:
http://groups.google.com/group/vim_use/browse_thread/thread/8449c75c87c7ef4

Thanks.

Gary Johnson

unread,
May 21, 2009, 2:04:22 PM5/21/09
to vim_use
On 2009-05-21, Danny wrote:

I'm not sure what information you're missing. No, I didn't read all
the references. As I understand it, you have patched PuTTY to emit
unique character sequences for each of Tab, Ctrl+Tab and
Ctrl+Shift+Tab. The next step is to run vim in that PuTTY, enter
insert mode, type Ctrl-V, then type Tab, then Enter. Repeat this
for Ctrl+Tab, Ctrl+Shift+Tab, and for my own curiosity, Shift+Tab.
The result should be four lines of character sequences. You may
need to ":set list" to see the Tab in the first line. The four
lines should contain unique sequences. If they're not unique, vim
will have no way to distinguish among them. That problem would have
to be fixed within PuTTY.

The terminal I'm using at the moment generates the following
sequence when I type Ctrl-V followed by Shift-Tab:

^[[Z

where the leading ^[ pair represents the single character Escape and
appears in blue on my terminal.

Assuming at this point that PuTTY does generate sequences of
characters when Ctrl+Tab and Ctrl+Shift+Tab are typed and that these
sequences are different from those generated by any other key
combination you care about, here's how you would map the first to
the :tabnext command. First type

:nnoremap

followed by a space, then Ctrl-V, then Ctrl+Tab, another space, then

:tabnext<CR>

That should do it. Mapping Ctrl+Shift+Tab is done the same way.

HTH,
Gary


Danny

unread,
May 21, 2009, 2:47:44 PM5/21/09
to vim_use
Yup, those suggestions helped! Thanks for the guidance there, I had
the pieces I just wasn't connecting them together properly. I was
having difficulty with the fact that the scnr.net link used "^
[[27;5;9~" as an escape sequence. As far as I know Vim was only
recognizing up to the first set of digits. Changing it to something
like "^[[1337" works.

1 ^I$ #tab
2 ^[[Z$ #shift-tab
3 ^[[1337$ #ctrl-tab
4 ^[[1334$ #ctrl-shift-tab

My other question though I am still wondering about. What is the
"correct" escape sequence to duplicate those keyboard commands. I
mean, Vim already has representations for <C-Tab> and <C-S-Tab>, but
how do I figure out what those are? Although not necessary, I'd like
to be able to send the keybindings that vim is actually expecting.

Thanks.
> >http://stackoverflow.com/questions/736004/creating-a-ctrltab-keybindi...
> > Problems with mappings using the Shift in the terminal from this
> > vim_use mailing list:
> >http://groups.google.com/group/vim_use/browse_thread/thread/8449c75c8...

Gary Johnson

unread,
May 21, 2009, 3:53:36 PM5/21/09
to vim_use
On 2009-05-21, Danny wrote:

> My other question though I am still wondering about. What is the
> "correct" escape sequence to duplicate those keyboard commands. I
> mean, Vim already has representations for <C-Tab> and <C-S-Tab>, but
> how do I figure out what those are? Although not necessary, I'd like
> to be able to send the keybindings that vim is actually expecting.

That I don't know. I thought that

:set termcap

might show it, but it doesn't. Using helpgrep didn't turn up
anything useful in the Vim documentation, either. I looked in the
source code and found various definitions for <S-Tab> in the
builtin_termcaps[] array in term.c, but nothing for <C-Tab> or
<C-S-Tab>. You might find a "standard" sequence for those keys in
the documentation for ncurses or xterm (e.g., ctlseqs.txt in the
xterm source directory), but I didn't find anything explicit in the
little bit of searching that I did and I didn't take the time to
read any of it thoroughly.

> Thanks.

You're welcome.


BTW, the convention on this list is to bottom post.

Regards,
Gary


Danny

unread,
May 21, 2009, 4:06:40 PM5/21/09
to vim_use
On May 21, 3:53 pm, Gary Johnson <garyj...@spocom.com> wrote:
> That I don't know.  I thought that
>
>     :set termcap
>
> might show it, but it doesn't.  Using helpgrep didn't turn up
> anything useful in the Vim documentation, either.  I looked in the
> source code and found various definitions for <S-Tab> in the
> builtin_termcaps[] array in term.c, but nothing for <C-Tab> or
> <C-S-Tab>.  You might find a "standard" sequence for those keys in
> the documentation for ncurses or xterm (e.g., ctlseqs.txt in the
> xterm source directory), but I didn't find anything explicit in the
> little bit of searching that I did and I didn't take the time to
> read any of it thoroughly.

Well, if anything it gives me some ideas/places to look. At least the
solution works even if its not "proper".

Danny.

Matt Wozniski

unread,
May 21, 2009, 10:33:01 PM5/21/09
to vim...@googlegroups.com
On Thu, May 21, 2009 at 2:47 PM, Danny wrote:
> On May 21, 2:04 pm, Gary Johnson wrote:
>> On 2009-05-21, Danny wrote:
>>> On Apr 29, 12:25 am, Danny wrote:
>>>> I have been attempting to bind Ctrl+Tab and Ctrl+Shift+Tab as
>>>> the :tabnext and :tabprev commands in vim. From what I have learned,
>>>> PuTTY does not send these commands. I found a post describing how to
>>>> patch putty to send these keys along. (http://scnr.net/blog/index.php/
>>>> archives/61). However, the keycodes it suggests are not ones that vim
>>>> is expecting. I'm assuming there is some set of keycodes that would be
>>>> appropriate, however I do not know what they are. I'm hoping someone
>>>> here might be able to shed some light on this.
>>>> If anyone is able to lend more information, I'd love to hear it!
>>>
>>> I have yet to find a solution for this problem. If anyone is able to
>>> direct me to any other resources where I might find some answers it'd
>>> be much appreciated!
>>

Hm. One comment would be that using a mapping for this isn't such
a good idea; it would be much better to use the method mentioned at
:help :set-termcap

The examples here might be very helpful for that:
http://vim.wikia.com/wiki/VimTip1272

> Yup, those suggestions helped! Thanks for the guidance there, I had
> the pieces I just wasn't connecting them together properly. I was
> having difficulty with the fact that the scnr.net link used "^
> [[27;5;9~" as an escape sequence. As far as I know Vim was only
> recognizing up to the first set of digits. Changing it to something
> like "^[[1337" works.
>
> 1 ^I$ #tab
> 2 ^[[Z$ #shift-tab
> 3 ^[[1337$ #ctrl-tab
> 4 ^[[1334$ #ctrl-shift-tab

No such limitation exists. Vim has no problem reading past a semicolon,
in fact, lots of well-recognized keycodes will have semicolons in them.
For instance, ^[[1;5D is ctrl-left.

> My other question though I am still wondering about. What is the
> "correct" escape sequence to duplicate those keyboard commands. I
> mean, Vim already has representations for <C-Tab> and <C-S-Tab>, but
> how do I figure out what those are? Although not necessary, I'd like
> to be able to send the keybindings that vim is actually expecting.

Terminal vim doesn't necessarily expect the right thing. That being
said, for <Tab>, only ^I is correct. For <S-Tab>, either ^[[Z or
^[[27;2;9~ is correct. For <C-Tab>, only ^[[27;5;9~ is correct, and for
<C-S-Tab>, only ^[[27;6;9~ is correct (vim recognizes neither of these).

In general, the most robust method for representing any keystroke is
CSI 27 ; (modifier mask + 1) ; (decimal number of modified key) ~
where the modifier mask is +1 for shift, +2 for alt, +4 for ctrl - but
vim can't understand keycodes of that type.

~Matt

Reply all
Reply to author
Forward
0 new messages