i have this in vimrc to highlight occurrences of word under cursor:
autocmd CursorMoved * execute 'match IncSearch' (getline('.')[col('.')-1] =~# '\w' ? '/\<' . expand('<cword>') . '\>/' : '//')
when i encounter these lines in :help command:
https://github.com/vim/colorschemes/blob/master/legacy_colors/
:edit $VIMRUNTIME/colors/README.txt
it reports error:
Error detected while processing CursorMoved Autocommands for "*":
E488: Trailing characters: /colors/README.txt\>/: match IncSearch /\<$VIMRUNTIME/colors/README.txt\>/
but when i open the help doc file directly in the vim editor and go to the lines, it's ok:
$ vi /usr/share/vim/vim90/doc/syntax.txt
:edit $VIMRUNTIME/colors/README.txt
or when i open a new file and edit text line these, it's also ok:
https://github.com/vim/colorschemes/blob/master/legacy_colors/
:edit $VIMRUNTIME/colors/README.txt
:edit /usr/share/vim/vim90/colors/README.txt
should be no error
9.0 (2022 Jun 28, compiled Oct 27 2023 01:00:56)
macOS 12.7.3 (21H1015)
Screen.Shot.2024-03-01.at.13.40.38.png (view on web)
No response
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
That is not a Vim bug, but your function does not handle the possibilities that the text could contain /
. The /
basically terminates your :match
command, so you need to escape it.
Something like this:
autocmd CursorMoved * execute 'match IncSearch' (getline('.')[col('.')-1] =~# '\w' ? '/\<' . escape(expand('<cword>'), '//') . '\>/' : '//')
Also, please note, it's probably better to use the matchadd()
function family instead of using :match
comand.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
autocmd CursorMoved * execute 'match IncSearch' (getline('.')[col('.')-1] =~# '\w' ? '/\<' . escape(expand('<cword>'), '//') . '\>/' : '//')
This works for me.
Thanks, Chris!
Also, please note, it's probably better to use the
matchadd()
function family instead of using:match
comand.
autocmd CursorMoved * call matchadd('Visual', getline('.')[col('.')-1] =~# '\w' ? '/\<' . escape(expand('<cword>'), '//') . '\>/' : '//')
Do I write this in matched function correctly? It has problems. I want to highlight occurrences of the word only when cursor in inside the word and clear highlight when cursor is outside the word.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
That is not a Vim bug, but your function does not handle the possibilities that the text could contain
/
. The/
basically terminates your:match
command, so you need to escape it.Something like this:
autocmd CursorMoved * execute 'match IncSearch' (getline('.')[col('.')-1] =~# '\w' ? '/\<' . escape(expand('<cword>'), '//') . '\>/' : '//')
Also, please note, it's probably better to use the
matchadd()
function family instead of using:match
comand.
Hi Chris,
It also reports error for ~
.
How do I escaple multiple special characters ~
, /
in one escape function ?
:!cp $VIMRUNTIME/colors/pablo.vim ~/.vim/colors
:edit ~/.vim/colors/pablo.vim
https://github.com/vim/colorschemes/blob/master/legacy_colors/
:edit $VIMRUNTIME/colors/README.txt
I also have another line for highlight occurrences of selected text.
How can i avoid those errors too?
autocmd CursorMoved * execute 'match IncSearch' (getline('.')[col('.')-1] =~# '\w' ? '/\<' . expand('<cword>') . '\>/' : '//')
vnoremap <expr> <CR> 'y:let @/ = "\\V" . substitute(escape(@", "/\\"), "\n", "\\n", "g") <bar> set hls<CR>'
Thanks!
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
you can add additional characters to the second argument of the escape()
. You also most likely want to use single-quotation marks instead of double quotes.
Also please ask user questions on vi.stackexchange.com or on the vim-use mailinglist.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
let's close it here then.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
Closed #14121 as not planned.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.