Saturday, December 23, 2017

Re: Help needed on terminal mappings

On 09/11/2017 12:36, Lifepillar wrote:
> On 08/11/2017 22:15, Bram Moolenaar wrote:

>> Hmm, perhaps you could try something like this:
>>
>>     :tmap <expr> <Esc>b SendToTerm("\<Esc>b")
>>     func SendToTerm(what)
>>       call term_sendkeys('', a:what)
>>       return ''
>>     endfunc
>>
>> No idea if this works, haven't tried it.
>
> That appears to work fine!

A follow-up on this: I have found that a drawback of your solution is
that, in an instance of Vim run inside a Vim terminal window, I need to
type Esc twice to return to Normal mode (I often happen to run Vim
inside Vim e.g., when I `git commit`).

What I am using now instead is this:

tnoremap <s-left> <esc>b
tnoremap <s-right> <esc>f

The first time I tried, I used ^[b and ^[f (with literal Esc), but that
did not work (^[b and ^[f on the rhs were translated into <s-left> and
<s-right> because of my settings). With <esc>, though, it works as
expected.

To recap, my configuration is as follows:

1. My terminal sends ^[b and ^[f when I type alt-left/right arrows.

2. In my vimrc, terminal keycodes ^[b and ^[f are tied to Vim
keycodes <s-left>/<s-right> using:

set <s-left>=^[b
set <s-right>=^[f

This makes alt-arrows work as shift-arrows in Insert and Command
mode, which means that I can jump between words with those key
combinations. A Vim terminal window, though, now sees \033[1;2D
and \033[1;2D when I type alt-arrows (because Shift-arrows are
mapped to those sequences by my terminal).

3. To fix that, I remap <s-left> and <s-right> in Terminal mode to send
the original sequence:

tnoremap <s-left> <esc>b
tnoremap <s-right> <esc>f

So, now I have alt-arrows work consistently in all modes, even in Vim
instances run inside a terminal window run inside a Vim instance run
inside etc... and Esc returns to Normal mode immediately under all
circumstances.

I don't know whether this strategy has any drawback (it seems fine so
far for me), and I may be missing something.

But if it is useful, you might want to update the terminal-special-key
section in the manual.

Life.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment