[vim/vim] All text is highlighted when starting a substitute with very magic when hlsearch and incsearch are active (#3363)

71 views
Skip to first unread message

Ram-Z

unread,
Aug 22, 2018, 6:08:26 AM8/22/18
to vim/vim, Subscribed

This is very similar to #2337 but applies to :s. I believe it is a regression introduced after the patch for #2337.

When starting a new substitute none of the text is highlighted as expected. Then enable very magic (\v) and suddenly all text is highlighted. I'd expect this to behave the same way as starting a new substitute.

Steps to reproduce:

$ vim -N -u NONE -c "set hlsearch incsearch" /path/to/file

Then type:

:s/\v

Expected result: nothing is highlighted
Actual result: entire line is highlighted

It's even more obvious when a full buffer substitute is started, in that case the whole buffer is highlighted:

:%s/\v
VIM - Vi IMproved 8.1 (2018 May 18, compiled Aug 13 2018 14:59:01)
Included patches: 1-279
Compiled by Arch Linux
Huge version with GTK3 GUI.  Features included (+) or not (-):
+acl               +extra_search      +mouse_netterm     +tag_old_static
+arabic            +farsi             +mouse_sgr         -tag_any_white
+autocmd           +file_in_path      -mouse_sysmouse    +tcl/dyn
+autochdir         +find_in_path      +mouse_urxvt       +termguicolors
-autoservername    +float             +mouse_xterm       +terminal
+balloon_eval      +folding           +multi_byte        +terminfo
+balloon_eval_term -footer            +multi_lang        +termresponse
+browse            +fork()            -mzscheme          +textobjects
++builtin_terms    +gettext           +netbeans_intg     +timers
+byte_offset       -hangul_input      +num64             +title
+channel           +iconv             +packages          +toolbar
+cindent           +insert_expand     +path_extra        +user_commands
+clientserver      +job               +perl/dyn          +vartabs
+clipboard         +jumplist          +persistent_undo   +vertsplit
+cmdline_compl     +keymap            +postscript        +virtualedit
+cmdline_hist      +lambda            +printer           +visual
+cmdline_info      +langmap           +profile           +visualextra
+comments          +libcall           +python/dyn        +viminfo
+conceal           +linebreak         +python3/dyn       +vreplace
+cryptv            +lispindent        +quickfix          +wildignore
+cscope            +listcmds          +reltime           +wildmenu
+cursorbind        +localmap          +rightleft         +windows
+cursorshape       +lua/dyn           +ruby/dyn          +writebackup
+dialog_con_gui    +menu              +scrollbind        +X11
+diff              +mksession         +signs             -xfontset
+digraphs          +modify_fname      +smartindent       +xim
+dnd               +mouse             +startuptime       -xpm
-ebcdic            +mouseshape        +statusline        +xsmp_interact
+emacs_tags        +mouse_dec         -sun_workshop      +xterm_clipboard
+eval              +mouse_gpm         +syntax            -xterm_save
+ex_extra          -mouse_jsbterm     +tag_binary        
   system vimrc file: "/etc/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  system gvimrc file: "/etc/gvimrc"
    user gvimrc file: "$HOME/.gvimrc"
2nd user gvimrc file: "~/.vim/gvimrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
    system menu file: "$VIMRUNTIME/menu.vim"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_GTK  -I/usr/include/gtk-3.0 -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/fribidi -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -I/usr/include/uuid -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/libdrm -I/usr/include/atk-1.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1.0 -I/usr/lib/dbus-1.0/include -pthread  -D_FORTIFY_SOURCE=2  -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fno-plt -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1       
Linking: gcc   -L. -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -fstack-protector -rdynamic -Wl,-export-dynamic -Wl,-E -Wl,-rpath,/usr/lib/perl5/5.28/core_perl/CORE  -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -L/usr/local/lib -Wl,--as-needed -o vim   -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0  -lSM -lICE -lXt -lX11 -lXdmcp -lSM -lICE  -lm -ltinfo -lelf -lnsl    -lacl -lattr -lgpm -ldl   -Wl,-E -Wl,-rpath,/usr/lib/perl5/5.28/core_perl/CORE -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -fstack-protector-strong -L/usr/local/lib  -L/usr/lib/perl5/5.28/core_perl/CORE -lperl -lpthread -ldl -lm -lcrypt -lutil -lc   -L/usr/lib -ltclstub8.6 -ldl -lz -lpthread -lm     

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub

Christian Brabandt

unread,
Aug 22, 2018, 6:43:31 AM8/22/18
to vim/vim, Subscribed

No, it happens because incsearch did until very recently not highlight patterns for the :s command.

I think we would need a patch like this:

diff --git a/src/ex_getln.c b/src/ex_getln.c
index b0da5d80e..d6e53089d 100644
--- a/src/ex_getln.c
+++ b/src/ex_getln.c
@@ -562,7 +562,7 @@ may_do_incsearch_highlighting(
     {
        next_char = ccline.cmdbuff[skiplen + patlen];
        ccline.cmdbuff[skiplen + patlen] = NUL;
-       if (empty_pattern(ccline.cmdbuff))
+       if (empty_pattern(ccline.cmdbuff + skiplen))
            set_no_hlsearch(TRUE);
        ccline.cmdbuff[skiplen + patlen] = next_char;
     }

Ram-Z

unread,
Aug 22, 2018, 6:53:02 AM8/22/18
to vim/vim, Subscribed

I believe it is a regression introduced after the patch for #2337.

I guess that was a bit ambiguous. 🤔 I meant that a commit after the fix for #2337 had introduced this behaviour.

Let me give that patch a go.

Ram-Z

unread,
Aug 22, 2018, 7:26:49 AM8/22/18
to vim/vim, Subscribed

With this patch the highlighting doesn't happen anymore, however the cursor will jump to beginning of the line (:s/\v) or the beginning of the buffer (:%s/\v).

Christian Brabandt

unread,
Aug 22, 2018, 7:57:38 AM8/22/18
to vim/vim, Subscribed

however the cursor will jump to beginning of the line (:s/\v) or the beginning of the buffer (:%s/\v).

I might be missing something, but that is how incsearch works. And it did that even before my patch.

Ram-Z

unread,
Aug 22, 2018, 8:22:38 AM8/22/18
to vim/vim, Subscribed

Not if search hasn't started yet, and \v isn't really searching for anything, it's a modifier for the rest of the search.

The current behaviour of search / with incsearch and hlsearch

/   " nothing to search, nothing is highlighted and the cursor doesn't move
/\  " searches for `\`, moves cursor to next match and highlights it¹
/\v " enables very magic, but has nothing to search therefore nothing is highlighted and the cursor jumps back to starting position.

The behaviour of substitute :s is currently inconsistent with the above.

¹ I find this a bit odd, \ is used to escape the next char \\ is used to search for a literal \. Maybe it shouldn't jump/highlight on this char? I never noticed before as I have mappings that will automatically siwtch to very magic mode on :s/ and /.

Christian Brabandt

unread,
Aug 22, 2018, 8:58:21 AM8/22/18
to vim/vim, Subscribed

The current behaviour of search / with incsearch and hlsearch

/   " nothing to search, nothing is highlighted and the cursor doesn't move
/\  " searches for `\`, moves cursor to next match and highlights it¹
/\v " enables very magic, but has nothing to search therefore nothing is highlighted and the cursor jumps back to starting position.

That is not what I observe:

vim --clean -c ':5' -c':norm! $' -c 'set incsearch hls' eval.c

Note: cursor is on the last char:
grafik

Now start a search, type:

  • / Note: cursor on commandline, not visible
  • \ Note: cursor jumps to first \ which happens to be on line 830, column 45
  • v Note: cursor moves one character to the left, which happens to be line 6, column 1:

grafik

And that makes sense, because in a pattern kind of sense, there does not exist the notion of pattern modifier, an empty \v just matches everywhere, which you can see by the christmas like highlighting if you actually search for \v, since it matches everywhere.

Ram-Z

unread,
Aug 22, 2018, 9:48:53 AM8/22/18
to vim/vim, Subscribed

v Note: cursor moves one character to the left, which happens to be line 6, column 1

I suppose you meant right. I never realised that it moved a single char to the right. I'm seeing the same as you.

And that makes sense, because in a pattern kind of sense, there does not exist the notion of pattern modifier, an empty \v just matches everywhere, which you can see by the christmas like highlighting if you actually search for \v, since it matches everywhere.

But there is a concept of no pattern, as simply typing / does not move the cursor, why is that different? IMHO, any of \c, \C, \v, \m, \M, \V should behave the same as having no pattern.

With the current behaviour, I will have to disable incsearch for substitutions because with the cursor moving I immediately lose context of what I wanted to replace file wide and I cannot use ^r w to insert the word from under the cursor.

Christian Brabandt

unread,
Aug 22, 2018, 10:28:59 AM8/22/18
to vim/vim, Subscribed

I suppose you meant right

correct.

With the current behaviour, I will have to disable incsearch for substitutions because with the cursor moving I immediately lose context of what I wanted to replace file wide and I cannot use ^r w to insert the word from under the cursor.

Perhaps you need to add <C-T> after typing \V to that the cursor moves back.

Ram-Z

unread,
Aug 22, 2018, 11:24:22 AM8/22/18
to vim/vim, Subscribed

Perhaps you need to add <C-T> after typing \V to that the cursor moves back.

Unfortunately that then highlights everything again.

Disabling incsearch like this will work.

augroup vimrc-incsearch-highlight
  autocmd!
  autocmd CmdlineEnter : :set noincsearch
  autocmd CmdlineLeave : :set incsearch
augroup END

It would be much nicer to have very magic just work the same way as normal search does.

Bram Moolenaar

unread,
Aug 22, 2018, 4:39:31 PM8/22/18
to vim/vim, Subscribed

Christian wrote:

> No, it happens because incsearch did until very recently not highlight patterns for the `:s` command.
>
> I think we would need a patch like this:
> ```patch

> diff --git a/src/ex_getln.c b/src/ex_getln.c
> index b0da5d80e..d6e53089d 100644
> --- a/src/ex_getln.c
> +++ b/src/ex_getln.c
> @@ -562,7 +562,7 @@ may_do_incsearch_highlighting(
> {
> next_char = ccline.cmdbuff[skiplen + patlen];
> ccline.cmdbuff[skiplen + patlen] = NUL;
> - if (empty_pattern(ccline.cmdbuff))
> + if (empty_pattern(ccline.cmdbuff + skiplen))
> set_no_hlsearch(TRUE);
> ccline.cmdbuff[skiplen + patlen] = next_char;
> }
> ```

Do do this properly we need to do a bit more. Acutally, it's probably
better to move the check for an empty pattern to
do_incsearch_highlighting()

--
Hacker: Someone skilled in computer programming (good guy).
Cracker: A hacker that uses his skills to crack software (bad guy).

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Christian Brabandt

unread,
Aug 23, 2018, 2:57:14 AM8/23/18
to vim/vim, Subscribed

fixed by 8b0d5ce

Christian Brabandt

unread,
Aug 23, 2018, 2:57:14 AM8/23/18
to vim/vim, Subscribed

Closed #3363.

Ram-Z

unread,
Aug 23, 2018, 5:28:49 AM8/23/18
to vim/vim, Subscribed

Works as expected now.

Thanks.

Ram-Z

unread,
Aug 23, 2018, 12:20:22 PM8/23/18
to vim/vim, Subscribed

Actually this has now introduced #2337 again, i.e. /\v will highlight everything and jump to top of file.

Bram Moolenaar

unread,
Aug 23, 2018, 2:56:36 PM8/23/18
to vim/vim, Subscribed

> Actually this has now introduced #2337 again, i.e. `/\v` will
> highlight everything and jump to top of file.

Weirdly we don't have a test for that.
The empty pattern check should not have been moved but copied.


--
CRONE: Who sent you?
ARTHUR: The Knights Who Say GNU!
CRONE: Aaaagh! (she looks around in rear) No! We have no licenses here.
"Monty Python and the Holy editor wars" PYTHON (MONTY) SOFTWARE LTD


/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

—

Ram-Z

unread,
Aug 24, 2018, 9:12:59 AM8/24/18
to vim/vim, Subscribed

I see the regression has been fixed in 4edfe2d

Thanks.

blayz3r

unread,
May 24, 2019, 8:09:22 PM5/24/19
to vim/vim, Subscribed

All text is highlighted with :call matchadd("IncSearch", '\v'), is this intentional?

Christian Brabandt

unread,
May 25, 2019, 2:17:59 AM5/25/19
to vim/vim, Subscribed
Yes, it works just like a normal search.
BTW please ask user questions at the vim_use ml or use vi.stackexchange.com.

> Am 25.05.2019 um 02:09 schrieb blayz3r <notifi...@github.com>:
>
> All text is highlighted with :call matchadd("IncSearch", '\v'), is this intentional?
>
> —
> You are receiving this because you modified the open/close state.
> Reply to this email directly, view it on GitHub, or mute the thread.

—
You are receiving this because you are subscribed to this thread.

Reply to this email directly, view it on GitHub, or mute the thread.

Blay263

unread,
May 25, 2019, 2:26:07 AM5/25/19
to vim_dev
Sure will do. My point was that shouldn't :call matchadd("IncSearch", '\v') highlight nothing as well?

Christian Brabandt

unread,
May 25, 2019, 2:31:17 AM5/25/19
to vim...@googlegroups.com
An that changed I believe. In that case we should probably change it here as well
> --
> --
> You received this message from the "vim_dev" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/vim_dev/617cc16b-a8f1-4d7b-b6dc-1da13a823fea%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Bram Moolenaar

unread,
May 25, 2019, 3:53:22 PM5/25/19
to vim/vim, Subscribed

> All text is highlighted with :call matchadd("IncSearch", '\v'), is
> this intentional?

Yes. It's not very useful, but you get what you asked for.

--
The technology involved in making anything invisible is so infinitely
complex that nine hundred and ninety-nine billion, nine hundred and
ninety-nine million, nine hundred and ninety-nine thousand, nine hundred
and ninety-nine times out of a trillion it is much simpler and more
effective just to take the thing away and do without it.
-- Douglas Adams, "The Hitchhiker's Guide to the Galaxy"


/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

—

Reply all
Reply to author
Forward
0 new messages