3

Because I'm on a german keyboard layout { and } are relatively hard to type.

As a workaround especially for latex I used inoremap <C-j> { and inoremap <C-l> } which works great. After internalizing these I also tried to use them in all other modes via noremap <C-j> { e.g. such that /<C-j> would search for { and ci<C-j> change in curly braces, but this doesn't work.

I also tried onoremap and lnoremap which both failed. But it does work in normal mode i.e. <C-j> moves a paragraph backward.

As far as I understand / and c should both go into "operator-pending" mode, so that map and omap should both work. I also couldn't find information that <C-j> is "special" like <C-h>, and I guess in that case imap shouldn't work either. Am I overlooking something?

I tested this on vim 7.4 with an empty config.

1 Answer 1

3

/<C-j>

/ doesn't enter operator-pending mode, it enters command-line mode.

For this, you want noremap! which applies to insert mode and command-line mode.

:help map-modes does imply that your attempt of lnoremap should have worked, but :help language-mapping, a bit further down in the file, explains why this is not the case:

Generally: Whenever a character is to be typed that is part of the text in the buffer, not a Vim command character.

[...]

[Language mappings] are only used for typed characters.

:help i_CTRL-j links to the insert-mode documentation's "Special keys" section: Ctrl-J is not a character which gets inserted into the buffer, but instead is a control character that tells Vim to begin a new line*.

ci<C-j>

c does enter operator pending mode, and you will find after having run onoremap <C-j> { as you describe, that c<c-j> works as expected.

The problem is ci{ works differently to how you are imagining. In this command the two keystrokes i{ are a text object (:help text-objects), and operator-pending mode maps don't apply in the middle of text objects. I think the fix for these will require a bit more verbosity:

onoremap i<c-j> i{
onoremap a<c-j> a{
" etc.

* If you're thinking that this is just a roundabout way of saying that a LF character is inserted when you type one, consider how Vim will save different bytes into the file depending on the current value of 'fileformat' — it's a subtle distinction, I agree.

1
  • Huh, I really never got that there is a difference between command characters and "typed characters". But of course otherwise stuff like <C-o> wouldn't work in insert mode. I thought c-j was simply mapped to enter in insert mode. And that mappings work on "syntactical units" instead simply on the character stream also never occured to me. Very informative, thanks!
    – syntonym
    Nov 14, 2017 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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