[vim/vim] Question-mark or forward-slash truncates a search-string (Issue #11460)

153 views
Skip to first unread message

Carl Ponder

unread,
Oct 29, 2022, 7:57:50 AM10/29/22
to vim/vim, Subscribed

Steps to reproduce

Create a file with this in it:

foo/a
foo/b
foo/bar
foo?bar
Do a search by typing "/foo/bar" or "?foo?bar". It will highlight all occurrences of "foo", not just "foo/bar" or "foo?bar".
Note that "/foo?bar" and "?foo/bar" both work.

### Expected behaviour

I would expect the whole string to have been searched for.
I know that this has been a longstanding issue, and many people  have posted explanations on how to escape the embedded "/" or "?".
But where in the vim specification does it say the embedded "/" or "?" is not a literal?
I'm doing a search here, not a substitution, so it looks to me like this is just a deficiency in the implementation, likely you're using the same code to process both searches and substitutions, and decomposing the command-string prior to performing the search.

### Version of Vim

VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Sep 13 2022 09:35:02) Included patches: 1-3995, 4563, 4646, 4774, 4895, 4899, 4901, 4919 Modified by team...@tracker.debian.org Compiled by team...@tracker.debian.org Huge version without GUI. 

### Environment

Ubuntu 22.04
xterm-256color
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)


### 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/11460@github.com>

Carl Ponder

unread,
Oct 29, 2022, 8:01:45 AM10/29/22
to vim/vim, Subscribed

Ok looking further, I see that there's an oprional "count" argument allowed:
?foo?2
/foo/2
Could you add an option to disable this?


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/11460/1295815372@github.com>

Carl Ponder

unread,
Oct 29, 2022, 8:08:03 AM10/29/22
to vim/vim, Subscribed

And, honestly, why couldn't the syntax have been "2/foo" instead of "/foo/2", which would have allowed for "2/foo/bar" to work?


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/11460/1295817442@github.com>

Carl Ponder

unread,
Oct 29, 2022, 6:22:55 PM10/29/22
to vim/vim, Subscribed

Based on this posting
https://vim.fandom.com/wiki/Searching_for_expressions_which_include_slashes
I tried re-mapping the '/' in my ~/.vimrc

command! -nargs=1 / let @/ = <q-args>|set hlsearch

but it didn't work.


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/11460/1296002128@github.com>

Gary Johnson

unread,
Oct 29, 2022, 8:19:25 PM10/29/22
to reply+ACY5DGFG3REEIXTRSV...@reply.github.com, vim...@googlegroups.com
On 2022-10-29, Carl Ponder wrote:
> Based on this posting
> https://vim.fandom.com/wiki/Searching_for_expressions_which_include_slashes
> I tried re-mapping the '/' in my ~/.vimrc
>
> command! -nargs=1 / let @/ = <q-args>|set hlsearch
>
> but it didn't work.

That won't work because user-defined commands must begin with an
upper-case letter. See

:help user-commands

To remap a command such as /, you must use a mapping, such as this
example:

:cnoremap <CR> <C-\>eescape(getcmdline(), '/')<CR><CR>:cunmap <lt>CR><CR>

See

:help map-overview
:help x_CTRL-\_e
:help getcmdline()
:helpgrep \cctrl-\\.\?e

and the mappings in the file

$VIMRUNTIME/macros/less.vim

That mapping is probably too simple, but it worked in the few test
cases I tried.

HTH,
Gary

vim-dev ML

unread,
Oct 29, 2022, 8:19:44 PM10/29/22
to vim/vim, vim-dev ML, Your activity


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/11460/1296033965@github.com>

Gary Johnson

unread,
Oct 29, 2022, 8:44:14 PM10/29/22
to vim...@googlegroups.com, reply+ACY5DGFG3REEIXTRSV...@reply.github.com
I got so involved in the problem of mapping <CR> that I forgot about
mapping /. Here's a better example, again just fleshed out to the
point of working, but closer to a complete solution.

:nnoremap / :call SlashEscape()<CR>/

:function SlashEscape()
: cnoremap <CR> <C-\>eescape(getcmdline(), '/')<CR><CR>:cunmap <lt>CR><CR>
:endfunction

Regards,
Gary

vim-dev ML

unread,
Oct 29, 2022, 8:44:32 PM10/29/22
to vim/vim, vim-dev ML, Your activity


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/11460/1296041318@github.com>

Carl Ponder

unread,
Oct 30, 2022, 7:27:17 AM10/30/22
to vim/vim, vim-dev ML, Comment

IUsing the above, tried to search with the command "/foo/bar" but it gave me the error

E486: Pattern not found: foo\\   


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/issues/11460/1296225288@github.com>

Carl Ponder

unread,
Oct 30, 2022, 7:39:39 AM10/30/22
to vim/vim, vim-dev ML, Comment

Ok. And adding this seems to take care of the analogous case with ? search:

:nnoremap ? :call QuestionEscape()<CR>?
:function QuestionEscape()
:    cnoremap <CR> <C-\>eescape(getcmdline(), '?')<CR><CR>:cunmap <lt>CR><CR>
:endfunction


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/issues/11460/1296229235@github.com>

Carl Ponder

unread,
Oct 30, 2022, 7:39:39 AM10/30/22
to vim/vim, vim-dev ML, Comment

Closed #11460 as completed.


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/issue/11460/issue_event/7698848400@github.com>

ben.k...@gmail.com

unread,
Oct 31, 2022, 11:30:45 AM10/31/22
to vim_dev
`:help /` explains the next `/` (or `?`) as being used for offsets. `:help pattern` does contain one instance of the pattern `\\\/` (which matches an escaped `/`) demonstrating it.

In general, if the syntax of `/` is `/pattern[/[offset]]`, how could it _not_ be that `/` in the pattern would need escaped? Also note in `:help /[]` that `[/]` works to escape the slash, too. 

Reply all
Reply to author
Forward
0 new messages