Path easy-search

97 views
Skip to first unread message

giacomo 'giotti' mariani

unread,
Apr 6, 2011, 10:11:33 AM4/6/11
to vim...@googlegroups.com
Hello everyone,
some days ago I was wondering on the best way to search for full
*NIX path (something like /path/to/file) in a file. The easier solution,
a back-slash before each slash, is not that comfortable, especially if
using the mouse for copy and paste.
I looked around and summarized my results in:

http://vim.wikia.com/wiki/Command_for_searching_expressions_%28paths%29_wich_include_backslashes

but I'm not sure that this is the best way to achieve the desired goal.
Is there a best way?

Thanks
Giacomo

--
##############################################################
giacomo 'giotti' mariani
gpg --keyserver pool.sks-keyservers.net --recv-key 0x99bfa859
O< ASCII ribbon campaign: stop HTML mail
www.asciiribbon.org
##############################################################

Christian Brabandt

unread,
Apr 6, 2011, 12:00:54 PM4/6/11
to vim...@googlegroups.com
On Wed, April 6, 2011 4:11 pm, giacomo 'giotti' mariani wrote:
> Hello everyone,
> some days ago I was wondering on the best way to search for full
> *NIX path (something like /path/to/file) in a file. The easier solution,
> a back-slash before each slash, is not that comfortable, especially if
> using the mouse for copy and paste.
> I looked around and summarized my results in:
>
> http://vim.wikia.com/wiki/Command_for_searching_expressions_%28paths%29_wich_include_backslashes

that link shows me an empty page ("This page needs content...")

> but I'm not sure that this is the best way to achieve the desired goal.
> Is there a best way?
>

Simply assign your search string directly to the search-register, e.g.
if you have your search string in the Clipboard, use:
:let @/=@+
and press n to jump to the next occurence.

Of course you can also assign it manually:
:let @/='/path/with/many/slashes'

regards,
Christian

Tim Chase

unread,
Apr 6, 2011, 12:20:50 PM4/6/11
to vim...@googlegroups.com, Christian Brabandt
On 04/06/2011 11:00 AM, Christian Brabandt wrote:
> Simply assign your search string directly to the search-register, e.g.
> if you have your search string in the Clipboard, use:
> :let @/=@+
> and press n to jump to the next occurence.
>
> Of course you can also assign it manually:
> :let @/='/path/with/many/slashes'

Or you can search backwards with "?":

?/path/to/wherever/with/slashes

If you want to change the direction of the search (rather than
using the somewhat awkwardly-inverted n/N commands), you can then
search forwards with no search criteria which will reuse the
previous pattern without the need to escape it.

-tim

Christian Brabandt

unread,
Apr 6, 2011, 12:34:21 PM4/6/11
to vim...@googlegroups.com

*g*, when searching backwards, I always get confused, whether to use
n/N for my desired direction. Is 'n' keeping the direction or searching
forward? And if 'N' reverses the direction and ? also reverses the
direction this reverses twice? That is too confusing for me...

So I try to avoid ?-search and jump to a place from which I can search
forward.

regards,
Christian

Tim Chase

unread,
Apr 6, 2011, 12:46:08 PM4/6/11
to vim...@googlegroups.com, Christian Brabandt
On 04/06/2011 11:34 AM, Christian Brabandt wrote:
> *g*, when searching backwards, I always get confused, whether
> to use n/N for my desired direction. Is 'n' keeping the
> direction or searching forward? And if 'N' reverses the
> direction and ? also reverses the direction this reverses
> twice? That is too confusing for me...

while it took me a while to figure it out (and remember it), it
still doesn't feel totally natural to think that "n" means "keep
searching in the same direction as my last search, whatever that
was", so unless I've JUST done a backwards "?" search, I find it
a bit cumbersome and side with you. Though sometimes setting
'nowrapscan' helps me keep them straight because I can then tell
when I hit the top/bottom of the file.

-tim


Tony Mechelynck

unread,
Apr 6, 2011, 12:57:42 PM4/6/11
to vim...@googlegroups.com, Christian Brabandt

? n n n n searches repeatedly backwards. Then hit N to search forward,
and n to search backward again.


Best regards,
Tony.
--
"Death is nature's way of saying `Howdy'".

Christian Brabandt

unread,
Apr 6, 2011, 1:50:07 PM4/6/11
to vim...@googlegroups.com
Hi Tony!

On Mi, 06 Apr 2011, Tony Mechelynck wrote:

> ? n n n n searches repeatedly backwards. Then hit N to search
> forward, and n to search backward again.

I *know* that. Just my fingers...

(When I navigate code and jump around a lot, I tend to forget, in which
direction I originally searched. I usually only know that I want to move
downwards, so I hit 'n' and I hate when Vim moves upwards...)

That's why I try to avoid the ? search.

regards,
Christian
--
Das Leben ist kurz, und es wird im Jenseits keiner nach der Zahl der
B�cher gefragt, die er bew�ltigt hat. Darum ist es unklug und
sch�dlich, mit wertloser Lekt�re Zeit hinzubringen.
-- Hermann Hesse (�ber das Leben)

Tim Chase

unread,
Apr 6, 2011, 2:05:01 PM4/6/11
to vim...@googlegroups.com
On 04/06/2011 12:50 PM, Christian Brabandt wrote:
>> ? n n n n searches repeatedly backwards. Then hit N to
>> search forward, and n to search backward again.
>
> I *know* that. Just my fingers...
>
> (When I navigate code and jump around a lot, I tend to forget,
> in which direction I originally searched. I usually only know
> that I want to move downwards, so I hit 'n' and I hate when
> Vim moves upwards...)

And the only thing worse than expecting "n" to move downwards
only to have it move upwards is having a file that's so similar
(such as a long tab/CSV file) where you can't readily tell
whether you've gone up or down :)

I confess, while Vim has a profusion of options, that's one I
wouldn't mind seeing...something that toggled between "n means
next in the same search-direction as last time" and "n means
search downward in the file". I suppose I could do something like

:nnoremap n /<cr>
:nnoremap N ?<cr>

if it really bugged me that much.

-tim


Christian Brabandt

unread,
Apr 6, 2011, 2:16:16 PM4/6/11
to vim...@googlegroups.com
Hi Tim!

On Mi, 06 Apr 2011, Tim Chase wrote:

> On 04/06/2011 12:50 PM, Christian Brabandt wrote:
> >>? n n n n searches repeatedly backwards. Then hit N to
> >>search forward, and n to search backward again.
> >
> >I *know* that. Just my fingers...
> >
> >(When I navigate code and jump around a lot, I tend to forget,
> >in which direction I originally searched. I usually only know
> >that I want to move downwards, so I hit 'n' and I hate when
> >Vim moves upwards...)
>
> And the only thing worse than expecting "n" to move downwards only
> to have it move upwards is having a file that's so similar (such as
> a long tab/CSV file) where you can't readily tell whether you've
> gone up or down :)

Oh yes, I know that...

regards,
Christian

John Beckett

unread,
Apr 6, 2011, 7:39:05 PM4/6/11
to vim...@googlegroups.com
giacomo wrote:
> some days ago I was wondering on the best way to search
> for full *NIX path (something like /path/to/file) in a file.
> The easier solution, a back-slash before each slash, is not
> that comfortable, especially if using the mouse for copy and paste.
> I looked around and summarized my results in:
>
> http://vim.wikia.com/wiki/Command_for_searching_expressions_%28paths%29_wich_include_backslashes

For the record, the actual title has a dot ('.') at the end:

http://vim.wikia.com/wiki/Command_for_searching_expressions_%28paths%29_wich_include_backslashes.

I have put the information discussed here into the comments in
the tip and will clean it up later.

John

John Little

unread,
Apr 6, 2011, 9:34:23 PM4/6/11
to vim_use

> >http://vim.wikia.com/wiki/Command_for_searching_expressions_%28paths%...
>
> that link shows me an empty page ("This page needs content...")

That url lacks a trailing period:

http://vim.wikia.com/wiki/Command_for_searching_expressions_%28paths%29_wich_include_backslashes.

Regards, John

Tony Mechelynck

unread,
Apr 7, 2011, 12:30:29 AM4/7/11
to vim...@googlegroups.com, John Little

Hm, apparently my mailer did not include the period in the link. The
following should work:
http://vim.wikia.com/wiki/Command_for_searching_expressions_%28paths%29_wich_include_backslashes%2E

Also, maybe that wiki page needs a rename or a redirect.


Best regards,
Tony.
--
It is against the law for a monster to enter the corporate limits of
Urbana, Illinois.

Ben Schmidt

unread,
Apr 7, 2011, 2:25:16 AM4/7/11
to vim...@googlegroups.com, Tim Chase
> I suppose I could do something like
>
> :nnoremap n /<cr>
> :nnoremap N ?<cr>
>
> if it really bugged me that much.

That is going straight into my .vimrc. One of those things that annoys
me, but not enough for me to bother doing anything about it. But if
someone's going to hand me a solution on a plate. :-)

(I guess it's similar to the J-is-not-an-operator thing, which I
recently addressed when something pushed me over the line!).

Smiles,

Ben.

eNG1Ne

unread,
Apr 7, 2011, 4:46:50 AM4/7/11
to vim_use
From a technical writer's stand-point, the title of an article should
not have a final full-stop: it isn't a sentence. Also, remember to
spell-check: "wich" is a typo <g>

On Apr 7, 6:30 am, Tony Mechelynck <antoine.mechely...@gmail.com>
wrote:
> On 07/04/11 03:34, John Little wrote:
>
>
>
> >>>http://vim.wikia.com/wiki/Command_for_searching_expressions_%28paths%...
>
> >> that link shows me an empty page ("This page needs content...")
>
> > That url lacks a trailing period:
>
> >http://vim.wikia.com/wiki/Command_for_searching_expressions_%28paths%....
>
> > Regards, John
>
> Hm, apparently my mailer did not include the period in the link. The
> following should work:http://vim.wikia.com/wiki/Command_for_searching_expressions_%28paths%...

Andy Wokula

unread,
Apr 9, 2011, 4:53:45 AM4/9/11
to vim...@googlegroups.com

Heh, around 2007 I almost got a headache around this ...

* what's wrong with "n" and "N"
After "d//e" (delete to end of match), "n" (from now on) moves to the
_end_ of the next match, which has become the last search pattern (not
wanted!); also I want "n" to always go forward and "N" go backward
* Solution:
:no n //<cr>
:no N ??<cr>
Problem: First item in the search history ("/" + "Ctrl-P") now is "//" and
not the last search string, I have to press Ctrl-P again (annoying,
because "//" is useless)
* Solution:
:no <silent> n //<cr>:call histdel('/',-1)<cr>
:no <silent> N ??<cr>:call histdel('/',-1)<cr>
Problem: The current search string is no longer echoed (but with <silent>
omitted, ":call histdel(..." will be echoed, even worse).
* Solution:
:no <silent> n //<cr>:call histdel('/',-1)<bar>echo '/'.histget('/',-1)<cr>
:no <silent> N ??<cr>:call histdel('/',-1)<bar>echo '/'.histget('/',-1)<cr>
Problem: The message "search hit BOTTOM, continuing at TOP" is overwritten
by the explicit echo.
* Solution:
:no <script> n //<cr><sid>histdel
:no <script> N ??<cr><sid>histdel
:no <silent> <sid>histdel :call histdel('/',-1)<cr>
Problem: Doesn't work for Visual mode. [21-10-2007]
* Solution:
:no <script> n //<cr><sid>histdel
:no <script> N ??<cr><sid>histdel
:nn <silent> <sid>histdel :call histdel('/',-1)<cr>
:vn <silent> <sid>histdel :<c-u>call histdel('/',-1)<cr>gv
Problem: In Visual mode this produces flickering, Command-line turns
Visual mode off when pressing Enter).
* Solution:
:no <script> n //<cr><sid>histdel
:no <script> N ??<cr><sid>histdel

:nn <silent> <sid>histdel :call<sid>histdel(0)<cr>
:vn <silent> <sid>histdel :<c-u>call<sid>histdel(1)<cr>

:func! <sid>histdel(vmode)
: if a:vmode
: normal! gv
: endif
: call histdel('/',-1)
:endfunc
Problem: If cursor is at `> in Visual mode and `< is not on screen, cursor
positioning is like after doing "oo" (flip to `< and back).
* Solution:
I thought
:vn <silent> <sid>histdel :<c-u>call<sid>histdel(1, line("w0"))<cr>
could help restoring the old position, but it fails.
Although the cursor is at `>, line("w0") always refers to `<, why?


(didn't further investigate here)

What I have now in the vimrc:

ono m //e<CR>
xn <script> m //e<SID>SelOff<CR>
cno <expr> <SID>SelOff &sel=="exclusive" ? "+1" : ""
" do the right thing after o_//e and .
no <script> n //<CR><SID>HistDel
no <script> N ??<CR><SID>HistDel
sunm n|sunm N
nn <silent> <SID>HistDel :call<sid>HistDel(0)<CR>
vn <silent> <SID>HistDel :<C-U>call<sid>HistDel(1)<CR>
ino <silent> <SID>HistDel <C-R>=<sid>HistDel(0)<CR>
func! <sid>HistDel(vmode)
if a:vmode
normal! gv
endif
call histdel('/', -1)
return ""
endfunc

--
Andy

Ben Schmidt

unread,
Apr 10, 2011, 8:09:26 AM4/10/11
to vim...@googlegroups.com, Andy Wokula
> Heh, around 2007 I almost got a headache around this ...
[...]

> What I have now in the vimrc:
>
> ono m //e<CR>
> xn <script> m //e<SID>SelOff<CR>
> cno <expr> <SID>SelOff &sel=="exclusive" ? "+1" : ""
> " do the right thing after o_//e and .
> no <script> n //<CR><SID>HistDel
> no <script> N ??<CR><SID>HistDel
> sunm n|sunm N
> nn <silent> <SID>HistDel :call<sid>HistDel(0)<CR>
> vn <silent> <SID>HistDel :<C-U>call<sid>HistDel(1)<CR>
> ino <silent> <SID>HistDel <C-R>=<sid>HistDel(0)<CR>
> func! <sid>HistDel(vmode)
> if a:vmode
> normal! gv
> endif
> call histdel('/', -1)
> return ""
> endfunc

Thanks! I'll give it a try!
Ben.

Ben Schmidt

unread,
May 1, 2011, 10:04:02 PM5/1/11
to vim...@googlegroups.com, Andy Wokula
> * what's wrong with "n" and "N"
...

> What I have now in the vimrc:
>
> ono m //e<CR>
> xn <script> m //e<SID>SelOff<CR>
> cno <expr> <SID>SelOff &sel=="exclusive" ? "+1" : ""
> " do the right thing after o_//e and .
> no <script> n //<CR><SID>HistDel
> no <script> N ??<CR><SID>HistDel
> sunm n|sunm N
> nn <silent> <SID>HistDel :call<sid>HistDel(0)<CR>
> vn <silent> <SID>HistDel :<C-U>call<sid>HistDel(1)<CR>
> ino <silent> <SID>HistDel <C-R>=<sid>HistDel(0)<CR>
> func! <sid>HistDel(vmode)
> if a:vmode
> normal! gv
> endif
> call histdel('/', -1)
> return ""
> endfunc

Problem: Folds are not automatically opened any more
Solution: Add a zv command when "search" is in 'foldopen'

no <script> <expr> n "//<CR>".(&fdo=~"search"?"zv":"")."<SID>HistDel"
no <script> <expr> N "??<CR>".(&fdo=~"search"?"zv":"")."<SID>HistDel"

(The rest is unchanged.)

Ben.

Andy Wokula

unread,
May 2, 2011, 8:55:48 AM5/2/11
to vim...@googlegroups.com, Ben Schmidt

He, thanks, that's right.
But I'd use another script mapping to add the zv; for example, at the
moment, one can't do
(Insert mode) Ctrl-O n


ono m //e<CR>
xn <script> m //e<SID>SelOff<CR>

cno <expr> <SID>SelOff &sel=="exclusive" ? "+1" : ""

no <script> n //<CR><SID>zv<SID>HistDel
no <script> N ??<CR><SID>zv<SID>HistDel
sunm n|sunm N

no <expr> <SID>zv &fdo=~"search" ? "zv" : ""
imap <SID>zv <Nop>

nn <silent> <SID>HistDel :call<sid>HistDel(0)<CR>
vn <silent> <SID>HistDel :<C-U>call<sid>HistDel(1)<CR>
ino <silent> <SID>HistDel <C-R>=<sid>HistDel(0)<CR>

func! <sid>HistDel(vmode)
if a:vmode
normal! gv
endif
call histdel('/', -1)
return ""
endfunc


--
Andy

ZyX

unread,
Feb 19, 2013, 10:45:39 PM2/19/13
to vim...@googlegroups.com, anw...@yahoo.de
суббота, 9 апреля 2011 г., 12:53:45 UTC+4 пользователь Andy Wokula написал:
Problem: the result is fucking too much code.
Solution:

noremap <expr> n 'Nn'[v:searchforward]
noremap <expr> N 'nN'[v:searchforward]

Andy Wokula

unread,
Mar 15, 2013, 12:38:37 PM3/15/13
to vim...@googlegroups.com, ZyX
You're right, but in the first place I wanted
/pattern
d//e
(hit Enter as-needed) to be repeatable with
n.n.n.n.

Can you suggest a simple mapping which supports this?

Builtin `n' doesn't work, it moves to the end of the next match
(remembering offset "e").
`/<CR>' also remembers the offset.
`//<CR>' moves to start of the next match -- ok, but it leaves a useless
entry (`/') in the history (to be removed by HistDel() function).

Actually, I'm fine with the solution -- it worked well for years.

--
Andy

ZyX

unread,
Aug 30, 2013, 12:47:52 PM8/30/13
to vim...@googlegroups.com, ZyX, anw...@yahoo.de
There is an interesting fact that setting `@/` resets //... flags. Thus you may try the following:

function s:SetSearch(sstr)
let @/=@/
return a:sstr
endfunction
noremap <expr> n <SID>SetSearch('Nn'[v:searchforward])
noremap <expr> N <SID>SetSearch('nN'[v:searchforward])

Andy Wokula

unread,
Aug 30, 2013, 1:59:04 PM8/30/13
to vim...@googlegroups.com, ZyX
Nice little observation, thanks for noticing!

Weird: after `?pattern<CR>`, the first `n` goes further back, following
`n`s move downwards. It appears that `:let @/ = @/` sets
v:searchforward to 1.

function s:SetSearch(sstr)
let @/=@/
return a:sstr
endfunction

noremap <expr> n <SID>SetSearch('n')
noremap <expr> N <SID>SetSearch('N')

--
Andy
Reply all
Reply to author
Forward
0 new messages