[vim/vim] E488: Trailing characters in vim help buffer (Issue #14121)

68 views
Skip to first unread message

lijh8

unread,
Mar 1, 2024, 12:42:31 AM3/1/24
to vim/vim, Subscribed

Steps to reproduce

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

Expected behaviour

should be no error

Version of Vim

9.0 (2022 Jun 28, compiled Oct 27 2023 01:00:56)

Environment

macOS 12.7.3 (21H1015)

Screen.Shot.2024-03-01.at.13.40.38.png (view on web)

Logs and stack traces

No response


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/14121@github.com>

Christian Brabandt

unread,
Mar 1, 2024, 2:16:16 AM3/1/24
to vim/vim, Subscribed

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.Message ID: <vim/vim/issues/14121/1972634553@github.com>

lijh8

unread,
Mar 1, 2024, 3:17:24 AM3/1/24
to vim/vim, Subscribed

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.Message ID: <vim/vim/issues/14121/1972719618@github.com>

lijh8

unread,
Mar 1, 2024, 1:47:21 PM3/1/24
to vim/vim, Subscribed

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.Message ID: <vim/vim/issues/14121/1973739967@github.com>

Christian Brabandt

unread,
Mar 1, 2024, 5:14:52 PM3/1/24
to vim/vim, Subscribed

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.Message ID: <vim/vim/issues/14121/1974004596@github.com>

Christian Brabandt

unread,
Mar 4, 2024, 1:57:54 PM3/4/24
to vim/vim, Subscribed

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.Message ID: <vim/vim/issues/14121/1977253165@github.com>

Christian Brabandt

unread,
Mar 4, 2024, 1:58:02 PM3/4/24
to vim/vim, Subscribed

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.Message ID: <vim/vim/issue/14121/issue_event/12003453865@github.com>

Reply all
Reply to author
Forward
0 new messages