4

I want to define a command that jumps to the next closest Chinese period or Chinese comma and perhaps other punctuations, and another command that does the same but for the previous closest punctuation.

To simplify the situation, let us say I want to jump to the next/previous closest x, y, or z. So I defined in ~/.vimrc

map <silent> <C-k> ?[xyz]<CR> :noh<CR>
map <silent> <C-j> /[xyz]<CR> :noh<CR>

And try this in such line:

abcx1y2z3def abcx1y2z3def abcx1y2z3def

But when I place my cursor at a, press ctrl-j, the cursor jumps to 1; again press ctrl-j, then to 2; again press ctrl-j, then to 3.

It always jumps to the character on the right of the next match.

And when I place my cursor at f, and press ctrl-k, the cursor jumps to 3; again press ctrl-k many times, but no use. It only jumps to the character on the right of the previous match.

Despite that, when I press /[xyz]<CR> :noh<CR> or ?[xyz]<CR> :noh<CR> explicitly, I get desired behavior.

Why? Have I made some syntax errors?

1
  • Note that it works well without the :noh<CR> part.
    – nobe4
    Dec 20, 2016 at 17:39

1 Answer 1

3

You problem is at the center of your mapping:

map <silent> <C-k> ?[xyz]<CR> :noh<CR>

Indeed, the problem is ! It moves one char right in normal mode. You can fix your mapping with:

map <silent> <C-k> ?[xyz]<CR>:noh<CR>
map <silent> <C-j> /[xyz]<CR>:noh<CR>

Have a look at :h <Space>. As well as this post: https://vi.stackexchange.com/a/7723

I also highly recommend using the noremap form, to prevent recursive mapping:

nnoremap <silent> <C-j> /[xyz]<CR>:noh<CR>
1
  • Oh, I thought spaces were insensitive....>< It was stupid of me QQ Dec 21, 2016 at 6:42

Your Answer

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

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