How do I stop my cursor from moving in vim when executing a :noremap?

StackOverflow https://stackoverflow.com/questions/14365881

  •  16-01-2022
  •  | 
  •  

سؤال

I have a number of these in my .vimrc:

:noremap <F1> :set hls!<CR> :echo "hilight seach (hls) =" &hls<CR>

(It's nice to have things like autoindent, line numbering, ignorecase, etc., toggled at the press of a button.)

But there is one strange behavior when these actions are executed. The cursor moves to the right by one column. If I'm at the end of a line, the cursor moves to the beginning of the next line. Why is it doing this and how do I prevent it?

This is vim 7.3.429, but I also see it in 7.2.411.

Thanks

هل كانت مفيدة؟

المحلول

The problem is the space between the <CR> and the :echo. Remove it and it should work.

:noremap <F1> :set hls!<CR>:echo "hilight seach (hls) =" &hls<CR>

نصائح أخرى

gpojd is correct the space is causing your movement issues. Perhaps you might be interested in a simpler toggle mappings?

nnoremap <f1> :set hls! hls?<cr>
nnoremap <f2> :set spell! spell?<cr>
nnoremap <f3> :set list! list?<cr>

Using set hls? will display the current value of 'hlsearch'. Because :set can set multiple settings at once you are toggling the value and then asking for the current value to be displayed.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top