Help needed on terminal mappings

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Help needed on terminal mappings

Lifepillar
I have my terminal (Terminal.app on macOS) configured with the
following mappings:

Shift+Left_arrow  \033[1;2D
Shift+Right_arrow \033[1;2C
Alt+Left_arrow    \033b
Alt+Right_arrow   \033f

The latter two, in particular, allow me to jump between words in
a shell using Alt+arrows.

Besides, in my vimrc, I have the following:

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

(^[ is literal Esc). With those, I can use Shift/Alt + arrows to
jump between words in the command line and in buffers.

In Vim's terminal, however, Alt+arrows send ^[1;2C and ^[1;2D, like
Shift+arrows. How can I configure Vim so that Alt+arrows behave the
same as in my shell (i.e., jump between words)?

I have tried:

tmap <a-left> ^[b
tmap <s-left> ^[b

to no avail. If I remove those two lines above from my vimrc, then
Alt+arrows correctly send ^[b and ^[f in Vim's terminal, but I lose
the ability to use Alt+arrows elsewhere.

Thanks,
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Help needed on terminal mappings

Bram Moolenaar

Lifepillar wrote:

> I have my terminal (Terminal.app on macOS) configured with the
> following mappings:
>
> Shift+Left_arrow  \033[1;2D
> Shift+Right_arrow \033[1;2C
> Alt+Left_arrow    \033b
> Alt+Right_arrow   \033f
>
> The latter two, in particular, allow me to jump between words in
> a shell using Alt+arrows.
>
> Besides, in my vimrc, I have the following:
>
>      set <s-left>=^[b
>      set <s-right>=^[f
>
> (^[ is literal Esc). With those, I can use Shift/Alt + arrows to
> jump between words in the command line and in buffers.
>
> In Vim's terminal, however, Alt+arrows send ^[1;2C and ^[1;2D, like
> Shift+arrows. How can I configure Vim so that Alt+arrows behave the
> same as in my shell (i.e., jump between words)?
>
> I have tried:
>
> tmap <a-left> ^[b
> tmap <s-left> ^[b
>
> to no avail. If I remove those two lines above from my vimrc, then
> Alt+arrows correctly send ^[b and ^[f in Vim's terminal, but I lose
> the ability to use Alt+arrows elsewhere.

This runs into a few limitations, main problem is that these keys are
not supported by termcap.  Vim does not recognize them as specific keys,
but you can map the escape sequence.  However, that doesn't work for
passing the code on to the terminal.

Ideally Vim would recognize the key, but that requires a lot of work.
And then libvterm needs to be able to handle it (a quick check is that
this isn't done properly, it mixes up ESC and CSI).

Assuming you just want to make this work for your specific setup and
accept that it's not portable: We can define escape sequences that Vim
passes on literally to the program runnin in the terminal window.
That's very flexible, but not portable.

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.

--
It might look like I'm doing nothing, but at the cellular level
I'm really quite busy.

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Help needed on terminal mappings

arocker

> --
> It might look like I'm doing nothing, but at the cellular level
> I'm really quite busy.
>

Even if I wasn't a Vim fan, I think I'd subscribe just for Bram's sigs. :-)*

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Help needed on terminal mappings

Lifepillar
In reply to this post by Bram Moolenaar
On 08/11/2017 22:15, Bram Moolenaar wrote:

>
> Lifepillar wrote:
>
>> I have my terminal (Terminal.app on macOS) configured with the
>> following mappings:
>>
>> Shift+Left_arrow  \033[1;2D
>> Shift+Right_arrow \033[1;2C
>> Alt+Left_arrow    \033b
>> Alt+Right_arrow   \033f
>>
>> The latter two, in particular, allow me to jump between words in
>> a shell using Alt+arrows.
>>
>> Besides, in my vimrc, I have the following:
>>
>>       set <s-left>=^[b
>>       set <s-right>=^[f
>>
>> (^[ is literal Esc). With those, I can use Shift/Alt + arrows to
>> jump between words in the command line and in buffers.
>>
>> In Vim's terminal, however, Alt+arrows send ^[1;2C and ^[1;2D, like
>> Shift+arrows. How can I configure Vim so that Alt+arrows behave the
>> same as in my shell (i.e., jump between words)?
>>
>> I have tried:
>>
>> tmap <a-left> ^[b
>> tmap <s-left> ^[b
>>
>> to no avail. If I remove those two lines above from my vimrc, then
>> Alt+arrows correctly send ^[b and ^[f in Vim's terminal, but I lose
>> the ability to use Alt+arrows elsewhere.
>
> This runs into a few limitations, main problem is that these keys are
> not supported by termcap.  Vim does not recognize them as specific keys,
> but you can map the escape sequence.  However, that doesn't work for
> passing the code on to the terminal.
>
> Ideally Vim would recognize the key, but that requires a lot of work.
> And then libvterm needs to be able to handle it (a quick check is that
> this isn't done properly, it mixes up ESC and CSI).
>
> Assuming you just want to make this work for your specific setup and
> accept that it's not portable: We can define escape sequences that Vim
> passes on literally to the program runnin in the terminal window.
> That's very flexible, but not portable.
>
> 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!

Thanks,
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
ds
Reply | Threaded
Open this post in threaded view
|

Re: Help needed on terminal mappings

ds
In reply to this post by Bram Moolenaar
Speaking of <A-Arrows>... My terminal (iTerm.app on macOS) sure does distinguish <A-Left> from <Left> (cat reports ^[[1;9D for the former, and ^[[D for the latter), my Vim does understand those keys, too, and I have the following in my $VIMRC (which works perfectly):

```
" go to the previous word
nnoremap <Esc>[1;9D <S-Left>
inoremap <Esc>[1;9D <S-Left>
xnoremap <Esc>[1;9D <S-Left>
cnoremap <Esc>[1;9D <S-Left>
" go to the next word
nnoremap <Esc>[1;9C <S-Right>
inoremap <Esc>[1;9C <S-Right>
xnoremap <Esc>[1;9C <S-Right>
cnoremap <Esc>[1;9C <S-Right>
```

But unfortunately :terminal fails to separate those, and cat reports ^[[D for both <A-Left> and <Left>.

Having that in mind, the solution you provided allows to map <C-Arrows> or <S-Arrows> but not <A-Arrows>. What do you think, is there any way to fix that?


Thanks,


--
ds

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Help needed on terminal mappings

Lifepillar
In reply to this post by Lifepillar
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.