4

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

To simplify situation, let us say I want to jump to 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 jump to the character on the right of next match.

And when I place my cursor at f, press ctrl-k, the cursor jumps to 3; again press ctrl-k many times, but no use. It only jump to the character on the right of 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 error?

  • Note that it works well without the :noh<CR> part. – nobe4 Dec 20 '16 at 17:39
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>
  • Oh, I thought spaces were insensitive....>< It was stupid of me QQ – Violapterin Dec 21 '16 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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