How to incude '/' in search(find) string on the fly

20 views
Skip to first unread message

Venkata Suryam Setty ISSA

unread,
Aug 25, 2008, 5:30:17 AM8/25/08
to vim...@googlegroups.com
Hi,

To include '/' in the search string when doing search & replace, I use
'#' as the field separator ( generally I will not have '#' in my search
strings).

Is there a way to achieve something similar in normal search with '/' or
'?' on the fly?

Regards
Venkat.

John Beckett

unread,
Aug 25, 2008, 6:07:17 AM8/25/08
to vim...@googlegroups.com
Venkata Suryam wrote:
> Is there a way to achieve something similar in normal search
> with '/' or '?' on the fly?

If using '/' to search forwards, you would have to escape each slash in the pattern,
that is, put a backslash before each slash. Following command finds "a/b":

/a\/b

Or, if you can select existing text, you can use this tip:
http://vim.wikia.com/wiki/Search_for_visually_selected_text

John

Venkata Suryam Setty ISSA

unread,
Aug 25, 2008, 6:43:06 AM8/25/08
to vim...@googlegroups.com


John Beckett wrote:
Venkata Suryam wrote:
  
Is there a way to achieve something similar in normal search 
with '/' or '?' on  the fly?
    
If using '/' to search forwards, you would have to escape each slash in the pattern,
that is, put a backslash before each slash. Following command finds "a/b":

/a\/b
  
It becomes quite annoying to put  backslash before each slash especially when I copy & paste the search string on to the command line & the search string has lot of slashes ('/'s).

That's why I am asking for something like '#' in search & replace, which can be used as field separator, if the search string doesn't contain '#'s.

Regards
Venkat.


Tim Chase

unread,
Aug 25, 2008, 7:02:31 AM8/25/08
to vim...@googlegroups.com
> It becomes quite annoying to put backslash before each slash especially
> when I copy & paste the search string on to the command line & the
> search string has lot of slashes ('/'s).

Well, while it's not exactly mis-stream ability to change, you
can so things like

:let @/='some/string/with/slashes?and?question?marks'

which, if you have 'hls' set, you can see readily highlights what
you're looking for. You can then use n/N to go to the
next/previous match.

-tim


Venkata Suryam Setty ISSA

unread,
Aug 25, 2008, 7:14:12 AM8/25/08
to vim...@googlegroups.com


Tim Chase wrote:
It becomes quite annoying to put  backslash before each slash especially 
when I copy & paste the search string on to the command line & the 
search string has lot of slashes ('/'s).
    
Well, while it's not exactly mis-stream ability to change, you 
can do things like

   :let @/='some/string/with/slashes?and?question?marks'

which, if you have 'hls' set, you can see readily highlights what 
you're looking for.  You can then use n/N to go to the 
next/previous match.
  
Thanks a lot. This is what I am looking for.

--Venkat.
  

Jürgen Krämer

unread,
Aug 25, 2008, 7:17:46 AM8/25/08
to vim...@googlegroups.com

Hi,

you could remap ? and / so that they are prefixed with a backslash:

cnoremap <expr> / getcmdtype() == '/' ? '\/' : '/'
cnoremap <expr> ? getcmdtype() == '?' ? '\?' : '?'

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us. (Calvin)

Venkata Suryam Setty ISSA

unread,
Aug 25, 2008, 8:03:56 AM8/25/08
to vim...@googlegroups.com


Jürgen Krämer wrote:
Hi,

Venkata Suryam Setty ISSA wrote:
  
To include '/' in the search string when doing search & replace, I use 
'#' as the field separator ( generally I will not have '#' in my search 
strings).

Is there a way to achieve something similar in normal search with '/' or 
'?' on  the fly?
    
you could remap ? and / so that they are prefixed with a backslash:

  cnoremap <expr>  /  getcmdtype() == '/' ? '\/' : '/'
  cnoremap <expr>  ?  getcmdtype() == '?' ? '\?' : '?'
  

This is not working in my VIM. Is it because my VIM version is 6.3.82? ( I can't find any help for getcmdtype() in my VIM).

--Venkat
  

Jürgen Krämer

unread,
Aug 25, 2008, 8:11:15 AM8/25/08
to vim...@googlegroups.com

Hi,

Venkata Suryam Setty ISSA wrote:
>

yes, both <expr> and getcmdtype() were introduced in VIM 7.0.

Tony Mechelynck

unread,
Aug 25, 2008, 1:37:33 PM8/25/08
to vim...@googlegroups.com
On 25/08/08 13:17, Jürgen Krämer wrote:
>
> Hi,
>
> Venkata Suryam Setty ISSA wrote:
>> To include '/' in the search string when doing search& replace, I use

>> '#' as the field separator ( generally I will not have '#' in my search
>> strings).
>>
>> Is there a way to achieve something similar in normal search with '/' or
>> '?' on the fly?
>
> you could remap ? and / so that they are prefixed with a backslash:
>
> cnoremap<expr> / getcmdtype() == '/' ? '\/' : '/'
> cnoremap<expr> ? getcmdtype() == '?' ? '\?' : '?'
>
> Regards,
> Jürgen
>

The problem with this, even in Vim 7, is that you won't be able to add
an offset after the search, e.g. to go to the line below the match:

/pattern/+1

nor will it work in a range:

:/pattern1/;/pattern2/command

The ":let @/ = expression" solution proposed in the other subthread
looks better to me.


Best regards,
Tony.
--
hundred-and-one symptoms of being an internet addict:
145. You e-mail your boss, informing him you'll be late.

aklt

unread,
Aug 25, 2008, 6:18:24 PM8/25/08
to vim_use
Hi,

On Aug 25, 7:37 pm, Tony Mechelynck <antoine.mechely...@gmail.com>
wrote:
> On 25/08/08 13:17, Jürgen Krämer wrote:
>
>
>
>
>
> > Hi,
>
> > Venkata Suryam Setty ISSA wrote:
> >> To include '/' in the search string when doing search& replace, I use
> >> '#' as the field separator ( generally I will not have '#' in my search
> >> strings).
>
> >> Is there a way to achieve something similar in normal search with '/' or
> >> '?' on the fly?
>
> > you could remap ? and / so that they are prefixed with a backslash:
>
> > cnoremap<expr> / getcmdtype() == '/' ? '\/' : '/'
> > cnoremap<expr> ? getcmdtype() == '?' ? '\?' : '?'
>
> > Regards,
> > Jürgen
>
> The problem with this, even in Vim 7, is that you won't be able to add
> an offset after the search, e.g. to go to the line below the match:
>
> /pattern/+1
>
> nor will it work in a range:
>
> :/pattern1/;/pattern2/command
>
> The ":let @/ = expression" solution proposed in the other subthread
> looks better to me.

I once wrote a script called substitute.vim which lets you search
for and substitute text under the cursor. Funny characters like '\'
and '/', etc.
should be dealt with automatically, maybe it can be of help:

http://www.vim.org/scripts/script.php?script_id=1167

Venkata Suryam Setty ISSA

unread,
Aug 26, 2008, 12:17:29 AM8/26/08
to vim...@googlegroups.com


Tony Mechelynck wrote:
On 25/08/08 13:17, Jürgen Krämer wrote:
  
Hi,

Venkata Suryam Setty ISSA wrote:
    
To include '/' in the search string when doing search&  replace, I use
'#' as the field separator ( generally I will not have '#' in my search
strings).

Is there a way to achieve something similar in normal search with '/' or
'?' on  the fly?
      
you could remap ? and / so that they are prefixed with a backslash:

   cnoremap<expr>   /  getcmdtype() == '/' ? '\/' : '/'
   cnoremap<expr>   ?  getcmdtype() == '?' ? '\?' : '?'

Regards,
Jürgen

    
The problem with this, even in Vim 7, is that you won't be able to add 
an offset after the search, e.g. to go to the line below the match:

	/pattern/+1

nor will it work in a range:

	:/pattern1/;/pattern2/command

The ":let @/ = expression" solution proposed in the other subthread 
looks better to me.

Thanks Tony.

--Venkat.
  

Jürgen Krämer

unread,
Aug 26, 2008, 3:28:50 AM8/26/08
to vim...@googlegroups.com

Hi,

Tony Mechelynck wrote:
> On 25/08/08 13:17, Jürgen Krämer wrote:
>>
>> Venkata Suryam Setty ISSA wrote:
>>> To include '/' in the search string when doing search& replace, I use
>>> '#' as the field separator ( generally I will not have '#' in my search
>>> strings).
>>>
>>> Is there a way to achieve something similar in normal search with '/' or
>>> '?' on the fly?
>> you could remap ? and / so that they are prefixed with a backslash:
>>
>> cnoremap<expr> / getcmdtype() == '/' ? '\/' : '/'
>> cnoremap<expr> ? getcmdtype() == '?' ? '\?' : '?'
>

> The problem with this, even in Vim 7, is that you won't be able to add
> an offset after the search, e.g. to go to the line below the match:
>
> /pattern/+1
>
> nor will it work in a range:
>
> :/pattern1/;/pattern2/command

you can always type <C-V> / or <C-V> ? if you want an unescaped slash or
question mark.

Carl Ponder US

unread,
Oct 29, 2022, 10:51:48 PM10/29/22
to vim_use
Solutions were posted on this page
that, if I read correctly, define a new search-operator that allows the embedded '/' characters.
The operator being "S" or "s" or "Ss" or something. Maybe '#' could be re-mapped the same way.

But in my case, I still want to use "/" and "?" as before. I tried using analogous forms to re-map these characters
```
command! -nargs=1 / let @/ = escape(<q-args>, '/')|normal! /<C-R>/<CR>
command! -nargs=1 ? let @? = escape(<q-args>, '?')|normal! /<C-R>/<CR>
command! -nargs=1 / let @/ = <q-args>|set hlsearch
command! -nargs=1 '/' let @/ = <q-args>
```
but they all gave vim-errors.

Carl Ponder US

unread,
Oct 29, 2022, 10:51:48 PM10/29/22
to vim_use
Also, it looks like this approach here
```
cnoremap <expr>  /  getcmdtype() == '/' ? '\/' : '/'
cnoremap <expr>  ?  getcmdtype() == '?' ? '\?' : '?'
```
does the escaping when I type-in the pattern but not when I copy/paste it using the mouse.

Salman Halim

unread,
Oct 30, 2022, 2:46:08 AM10/30/22
to vim...@googlegroups.com
Carl,

The errors you are getting from the commands aren't about what you have on the RHS of the command, but rather because / and ? aren't valid command names. E182: Invalid command name. It's worth examining the error message as not all errors are the same.

You could call it something else, such as S or SS or something like that; the following works for me:

com! -nargs=1 SS let @/=<q-args>

Your mapping approach is fine, but might potentially require a other mappings around it; I can create a mapping to paste using the keyboard that works, but have no idea if it will work for the mouse as I don't use the mouse with Vim:

cnoremap <expr> <c-r>* getcmdtype() == '/' ? escape(@*, '/') : @*

Change the * above to the register that holds your clipboard contents. Note that if your copied text contains a newline, using the mapping above will end the search; if that ends up being an issue you see you'll have to write a custom function for that expr that both escapes the slashes in the clipboard contents and replaces newlines with '\n' so it doesn't actually press enter.

Hope this helps,

Salman

--
--
You received this message from the "vim_use" 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_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/b23bc7b1-e562-418f-af09-12b9baad3877n%40googlegroups.com.


--
 
Salman

I, too, shall something make and glory in the making.

Carl Ponder US

unread,
Oct 30, 2022, 7:21:43 AM10/30/22
to vim_use
I'm not using a mouse in the sense of being able to move the cursor by clicking within the text.
I'm only keyboarding the Vim operations, except to select/copy/paste text between the Vim session and windows with other activities.
This works file to insert text into the file being edited, but if I'm not in insert-mode, I still can't paste a sequence like
```
:s/foo/zap/g
```
into the session and have it work the way it would if I typed it in . Why is that?

Carl Ponder US

unread,
Oct 30, 2022, 7:39:54 AM10/30/22
to vim_use
It looks like this is giving me what I want:
```

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

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

Salman Halim

unread,
Oct 31, 2022, 1:12:08 AM10/31/22
to Vim Users
Glad you found a solution. I've just become really accustomed to escaping the slash so don't even think about it any more. 

Salman

--
--
You received this message from the "vim_use" 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_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages