Re: search visual block

2006-10-18 Thread Jean-Rene David
* David Fishburn [2006.10.18 22:00]:
> " Courtesy of Michael Naumann, Jürgen Krämer
> " Visually select text, then search for it
> if version >= 602
> " Here are two enhanced versions of these mappings which use VIM 6.2's
> " getregtype() function to determine whether the unnamed register
> contains
> " a characterwise, linewise or blockwise selection. After the search has
> 
> " been executed, the register *and* its type can then be restored with
> " setreg().
> vnoremap  * :
>   \let old_reg=getreg('"')
>   \let old_regmode=getregtype('"')
>   \gvy/=substitute(substitute(
>   \escape(@", '\\/.*$^~[]' ), "\n$", "", ""),
>   \"\n", '\\_[[:return:]]', "g")
>   \:call setreg('"', old_reg, old_regmode)
> vnoremap  # :
>   \let old_reg=getreg('"')
>   \let old_regmode=getregtype('"')
>   \gvy?=substitute(substitute(
>   \escape(@", '\\/.*$^~[]' ), "\n$", "", ""),
>   \"\n", '\\_[[:return:]]', "g")
>   \:call setreg('"', old_reg, old_regmode)
> else
> " If you use both VIM 6.2 and older versions these mappings
> " should be defined depending on the current version.
> vnoremap  * :let old_reg=@"
>   \gvy/=substitute(substitute(
>   \escape(@", '\\/.*$^~[]' ), "\n$", "", ""),
>   \"\n", '\\_[[:return:]]', "g")
>   \:let @"=old_reg
> vnoremap  # :let old_reg=@"
>   \gvy?=substitute(substitute(
>   \escape(@", '\\/.*$^~[]' ), "\n$", "", ""),
>   \"\n", '\\_[[:return:]]', "g")
>   \:let @"=old_reg
> endif

This is where I got my inspiration. Note the exact
same credits. But I found the mappings so
illegible that I rewrote them as functions. 

I don't find using a function to be much of a
"con" compare to the "pro" of legibility. The
function I posted today has already been hacked
and made better. It would take much more time and
thought to modify these monsters...

Reading the comments at the top, I thought for a
minute that the mapping could search for a
blockwise visual region. That would be pretty
neat.

-- 
JR


RE: search visual block

2006-10-18 Thread David Fishburn
 

> -Original Message-
> From: Lev Lvovsky [mailto:[EMAIL PROTECTED] 
> Sent: Tuesday, October 17, 2006 5:04 PM
> To: vim@vim.org
> Subject: search visual block
> 
> Is it possible to search for a string by selecting that 
> string in visual mode?  Meaning, if I highlight something, 
> and then want to search for that thing which is highlighted 
> in the rest of the doc?
> 
> otherwise, is there a way to copy that string so that I can 
> later put it into the :/ command?

I know you have had lots of different reponses on this thread.
This is what I keep in my vimrc, which doesn't use a function.

Simply highlight the text and hit * or #, just like you would for a given
work in normal mode.

" Courtesy of Michael Naumann, Jürgen Krämer
" Visually select text, then search for it
if version >= 602
" Here are two enhanced versions of these mappings which use VIM 6.2's
" getregtype() function to determine whether the unnamed register
contains 
" a characterwise, linewise or blockwise selection. After the search has

" been executed, the register *and* its type can then be restored with 
" setreg(). 
vnoremap  * :
  \let old_reg=getreg('"')
  \let old_regmode=getregtype('"')
  \gvy/=substitute(substitute(
  \escape(@", '\\/.*$^~[]' ), "\n$", "", ""),
  \"\n", '\\_[[:return:]]', "g")
  \:call setreg('"', old_reg, old_regmode)
vnoremap  # :
  \let old_reg=getreg('"')
  \let old_regmode=getregtype('"')
  \gvy?=substitute(substitute(
  \escape(@", '\\/.*$^~[]' ), "\n$", "", ""),
  \"\n", '\\_[[:return:]]', "g")
  \:call setreg('"', old_reg, old_regmode)
else
" If you use both VIM 6.2 and older versions these mappings 
" should be defined depending on the current version.
vnoremap  * :let old_reg=@"
  \gvy/=substitute(substitute(
  \escape(@", '\\/.*$^~[]' ), "\n$", "", ""),
  \"\n", '\\_[[:return:]]', "g")
  \:let @"=old_reg
vnoremap  # :let old_reg=@"
  \gvy?=substitute(substitute(
  \escape(@", '\\/.*$^~[]' ), "\n$", "", ""),
  \"\n", '\\_[[:return:]]', "g")
  \:let @"=old_reg
endif



Re: search visual block

2006-10-18 Thread David Thompson
--- Robert Cussons <[EMAIL PROTECTED]> wrote:
> Jean-Rene David wrote:
> > * Robert Cussons [2006.10.18 09:29]:
> > 
> >>Everything seems to work fine now, except the
> >>searched for items aren't highlighted like they
> >>normally are when I search
> > 
> > 
> > Whether or not search items are highlighted
> > depends on the value of the 'hlsearch' option.
> > 
> > The search item gets highlighted on my end when the
> > option is set. Is yours set?
[snip]

I have learned a lot from this thread!

I changed the function slightly (see below) because I also
didn't understand the missing ^M was causing my problem.

I like to use * and # for searching for a string in highlight
mode, then I press F10 to turn off highlighting.  However,
pressing F10 with nohls highlights the word under the cursor
without searching for it, which was is a modification of some
comments at the bottom of vimtip #1.

Between this thread and the discussions in vimtip #1 I now
have the following which works really *really* nicely:

"begin
" disabled initially since it distracts me
set nohlsearch

" in normal mode, make * highlight the search string
nnoremap  * *:set hls
nnoremap  # #:set hls

" in visual mode, use highlighted selection for search pattern
vnoremap  * :call VisualSearch('f'):set hls
vnoremap  # :call VisualSearch('b'):set hls
function! VisualSearch(direction)
let l:saved_reg = @"
execute "normal! vgvy"
let l:pattern = escape(@", '\/.*$^~[]')
let l:pattern = substitute(l:pattern, "\n$", "", "")
let @/ = l:pattern
let @" = l:saved_reg
if a:direction == 'f'
normal n
else
normal N
endif
endfunc

" map  to toggle the highlight search mode
" if hlsearch is on  then simply turn it off
" if hlsearch is off then highlight word under cursor only
nnoremap   :call SetSearchReg():set invhls
function! SetSearchReg()
if &hlsearch == 0
let @/ = expand('')
endif
endfunc

" my mind/fingers think of  as highlight search mode
" so make visual mode  and  work like * and #
vnoremap   :call VisualSearch('f'):set hls
vnoremap   :call VisualSearch('b'):set hls
"-end-

Regards,

David


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


Re: search visual block

2006-10-18 Thread Robert Cussons

Jean-Rene David wrote:

* Robert Cussons [2006.10.18 09:29]:


Everything seems to work fine now, except the
searched for items aren't highlighted like they
normally are when I search



Whether or not search items are highlighted
depends on the value of the 'hlsearch' option.

The search item gets highlighted on my end when the
option is set. Is yours set?

:set hls?



This is quite strange, it is set, i.e.

:set hls?

returns

hlsearch

If I use your visual selection when I first launch gvim, then it works 
with the highlighting. If I then use  to clear the highlighting 
according to this line in my .vimrc that I gave before:


:nnoremap  mz:nohlsearch/`z

then the highlighting is no longer displayed as is expected.
If I then do

:set hls?

again, I get again:

hlsearch

but if I use your visual selection tool again then this time I get no 
highlighting. Of course if I set hls again using :set hls then it works 
fine again until I press enter.


So what I was thinking was just to add a :set hls at the start of your 
function, then it should alway highlight the searched for items, however 
I tried variations on including it but no nothing about scripting so 
they didn't work, can you tell me how I should add it? I tried the below 
but it didn't work.


Thanks,
Rob.

" Search for visually selected text {{{
" From an idea by Michael Naumann, Jürgen Krämer.
function! VisualSearch(direction) range
set hlsearch
   let l:saved_reg = @"
   execute "normal! vgvy"
   let l:pattern = escape(@", '\\/.*$^~[]')
   let l:pattern = substitute(l:pattern, "\n$", "", "")
   if a:direction == 'b'
  execute "normal ?" . l:pattern . "\r"
   else
  execute "normal /" . l:pattern . "\r"
   endif
   let @/ = l:pattern
   let @" = l:saved_reg
endfunction

vnoremap  * :call VisualSearch('f')
vnoremap  # :call VisualSearch('b')




Re: search visual block

2006-10-18 Thread Jean-Rene David
* Robert Cussons [2006.10.18 09:29]:
> Everything seems to work fine now, except the
> searched for items aren't highlighted like they
> normally are when I search

Whether or not search items are highlighted
depends on the value of the 'hlsearch' option.

The search item gets highlighted on my end when the
option is set. Is yours set?

:set hls?

-- 
JR


Re: search visual block

2006-10-18 Thread Jean-Rene David
* Benji Fisher [2006.10.18 09:15]:
>  I try to avoid such problems by not including raw CR, ESC, etc.
> characters in my vim scripts.  I suggest replacing the two :execute
> lines with
>  execute "normal ?" . l:pattern . "\"
> and
>  execute "normal /" . l:pattern . "\"

I was looking for a way to avoid the literals.
Thanks for that. That's definitely better.

-- 
JR


Re: search visual block

2006-10-18 Thread Robert Cussons

Jean-Rene David wrote:

* Robert Cussons [2006.10.18 06:30]:


I did notice that between the if and else there
are " which just act as comments as they are on
newlines,



Sorry, I should have known that wouldn't come out
right. There's a literal newline between the
quotes. You can enter it by pressing
.

Here's that section of code with the literal
newline entered as two separate characters:

if a:direction == 'b'
   execute "normal ?" . l:pattern . "^M"
else
   execute "normal /" . l:pattern . "^M"
endif



I was selecting text in visual mode, then
pressing / or ? and I just get the normal action
of pressing / or ?



I agree with what you say below completely, I just didn't express myself 
clearly enough in the paragraph above, I was just telling you the 
actions I had performed and it appears I should have been pressing * or 
# not / or ? as I originally thought, but the lack of the literal 
newline was causing it not to work too.





Well you could do it with "/" and "?" but I like
to keep their behavior intact as it is useful to
extend the visual region.

I remapped "*" and "#" instead, as shown in these
lines:

vnoremap  * :call VisualSearch('f')
vnoremap  # :call VisualSearch('b')

I prefer that because these don't have any special
meaning in visual mode and it ties in nicely with
the "search next/previous word" function they have
in normal mode.



Thanks for your help,
Rob.


Re: search visual block

2006-10-18 Thread A.J.Mechelynck

Jean-Rene David wrote:

* Robert Cussons [2006.10.18 06:30]:

I did notice that between the if and else there
are " which just act as comments as they are on
newlines,


Sorry, I should have known that wouldn't come out
right. There's a literal newline between the
quotes. You can enter it by pressing
.

[...]

Instead of "^M", you can use "\r", which is more readable and defines the same 
string (see ":help expr-string")



Best regards,
Tony.


Re: search visual block

2006-10-18 Thread Robert Cussons

Benji Fisher wrote:

On Wed, Oct 18, 2006 at 12:28:28PM +0200, Robert Cussons wrote:


Jean-Rene David wrote:


[snip]


"--< cut here >---
" Search for visually selected text {{{
" From an idea by Michael Naumann, Jürgen Krämer.
function! VisualSearch(direction) range
 let l:saved_reg = @"
 execute "normal! vgvy"
 let l:pattern = escape(@", '\\/.*$^~[]')
 let l:pattern = substitute(l:pattern, "\n$", "", "")
 if a:direction == 'b'
execute "normal ?" . l:pattern . "
"
 else
execute "normal /" . l:pattern . "
"
 endif
 let @/ = l:pattern
 let @" = l:saved_reg
endfunction

vnoremap  * :call VisualSearch('f')
vnoremap  # :call VisualSearch('b')
"--< cut here >---




 I think the original included raw CR characters in the two :execute
lines.  Both are intended to end with "^M" (which is how they appeared
in my copy of Jean-Rene's note).  I think your e-mail client broke the
lines, leading to syntax errors.


That may well be, don't know too much about how mozilla -mail works



 I try to avoid such problems by not including raw CR, ESC, etc.
characters in my vim scripts.  I suggest replacing the two :execute
lines with
 execute "normal ?" . l:pattern . "\"
and
 execute "normal /" . l:pattern . "\"

HTH --Benji Fisher



Everything seems to work fine now, except the searched for items aren't 
highlighted like they normally are when I search, is there a simple 
reason or am I just asking for too much, sorry I can't sort this out 
myself, but I don't understand the function well enough to see a 
possilbe reason, the only thing I could think of that might cause it 
would be this in my .vimrc


" Clears search highlighting by just hitting a return.
" The  clears the command line.
" (From Zdenek Sekera [EMAIL PROTECTED]  on the vim list.)
" I added the final  to restore the standard behaviour of
"  to go to the next line and the mz and `z to return the cursor to its
" precommand position
:nnoremap  mz:nohlsearch/`z

However, if this were interfering it would clear the command line as 
well and I don't see that so it can't be the cause.



Thanks,
Rob.


Re: search visual block

2006-10-18 Thread Jean-Rene David
* Robert Cussons [2006.10.18 06:30]:
> I did notice that between the if and else there
> are " which just act as comments as they are on
> newlines,

Sorry, I should have known that wouldn't come out
right. There's a literal newline between the
quotes. You can enter it by pressing
.

Here's that section of code with the literal
newline entered as two separate characters:

if a:direction == 'b'
   execute "normal ?" . l:pattern . "^M"
else
   execute "normal /" . l:pattern . "^M"
endif

> I was selecting text in visual mode, then
> pressing / or ? and I just get the normal action
> of pressing / or ?

Well you could do it with "/" and "?" but I like
to keep their behavior intact as it is useful to
extend the visual region.

I remapped "*" and "#" instead, as shown in these
lines:

vnoremap  * :call VisualSearch('f')
vnoremap  # :call VisualSearch('b')

I prefer that because these don't have any special
meaning in visual mode and it ties in nicely with
the "search next/previous word" function they have
in normal mode.

-- 
JR


Re: search visual block

2006-10-18 Thread Benji Fisher
On Wed, Oct 18, 2006 at 12:28:28PM +0200, Robert Cussons wrote:
> Jean-Rene David wrote:
[snip]
> >
> >"--< cut here >---
> >" Search for visually selected text {{{
> >" From an idea by Michael Naumann, Jürgen Krämer.
> >function! VisualSearch(direction) range
> >   let l:saved_reg = @"
> >   execute "normal! vgvy"
> >   let l:pattern = escape(@", '\\/.*$^~[]')
> >   let l:pattern = substitute(l:pattern, "\n$", "", "")
> >   if a:direction == 'b'
> >  execute "normal ?" . l:pattern . "
> >"
> >   else
> >  execute "normal /" . l:pattern . "
> >"
> >   endif
> >   let @/ = l:pattern
> >   let @" = l:saved_reg
> >endfunction
> >
> >vnoremap  * :call VisualSearch('f')
> >vnoremap  # :call VisualSearch('b')
> >"--< cut here >---
> 
> This sounds brilliant Jean-Rene, I put it into my .vimrc, but it doesn't 
> seem to work, I did notice that between the if and else there are " 
> which just act as comments as they are on newlines, didn't know if this 
> was just my text wrapping, so I tried putting them on the line above, 
> both with a space between or not, didn't know if that might be cause but 
> that didn't seem to help either. Sorry I may be missing something 
> obvious, but I'd like to get this to work as it seems very useful.
> I was selecting text in visual mode, then pressing / or ? and I just get 
> the normal action of pressing / or ?

 I think the original included raw CR characters in the two :execute
lines.  Both are intended to end with "^M" (which is how they appeared
in my copy of Jean-Rene's note).  I think your e-mail client broke the
lines, leading to syntax errors.

 I try to avoid such problems by not including raw CR, ESC, etc.
characters in my vim scripts.  I suggest replacing the two :execute
lines with
 execute "normal ?" . l:pattern . "\"
and
 execute "normal /" . l:pattern . "\"

HTH --Benji Fisher


Re: search visual block

2006-10-18 Thread Robert Cussons

Jean-Rene David wrote:

* Lev Lvovsky [2006.10.17 17:15]:


Is it possible to search for a string by
selecting that string in  visual mode?  Meaning,
if I highlight something, and then want to
search for that thing which is highlighted in
the rest of the doc?



You already got lots of good answers. Here's
another one.

I've had this in my vimrc for years, and use it
when the string I'm searching for is not a
keyword. It works both forward and backward, puts
the searched pattern in the search history and
doesn't screw up any register.

"--< cut here >---
" Search for visually selected text {{{
" From an idea by Michael Naumann, Jürgen Krämer.
function! VisualSearch(direction) range
   let l:saved_reg = @"
   execute "normal! vgvy"
   let l:pattern = escape(@", '\\/.*$^~[]')
   let l:pattern = substitute(l:pattern, "\n$", "", "")
   if a:direction == 'b'
  execute "normal ?" . l:pattern . "
"
   else
  execute "normal /" . l:pattern . "
"
   endif
   let @/ = l:pattern
   let @" = l:saved_reg
endfunction

vnoremap  * :call VisualSearch('f')
vnoremap  # :call VisualSearch('b')
"--< cut here >---

HTH,



This sounds brilliant Jean-Rene, I put it into my .vimrc, but it doesn't 
seem to work, I did notice that between the if and else there are " 
which just act as comments as they are on newlines, didn't know if this 
was just my text wrapping, so I tried putting them on the line above, 
both with a space between or not, didn't know if that might be cause but 
that didn't seem to help either. Sorry I may be missing something 
obvious, but I'd like to get this to work as it seems very useful.
I was selecting text in visual mode, then pressing / or ? and I just get 
the normal action of pressing / or ?


Thanks,
Rob.




Re: search visual block

2006-10-17 Thread Jean-Rene David
* Lev Lvovsky [2006.10.17 17:15]:
> Is it possible to search for a string by
> selecting that string in  visual mode?  Meaning,
> if I highlight something, and then want to
> search for that thing which is highlighted in
> the rest of the doc?

You already got lots of good answers. Here's
another one.

I've had this in my vimrc for years, and use it
when the string I'm searching for is not a
keyword. It works both forward and backward, puts
the searched pattern in the search history and
doesn't screw up any register.

"--< cut here >---
" Search for visually selected text {{{
" From an idea by Michael Naumann, Jürgen Krämer.
function! VisualSearch(direction) range
   let l:saved_reg = @"
   execute "normal! vgvy"
   let l:pattern = escape(@", '\\/.*$^~[]')
   let l:pattern = substitute(l:pattern, "\n$", "", "")
   if a:direction == 'b'
  execute "normal ?" . l:pattern . "
"
   else
  execute "normal /" . l:pattern . "
"
   endif
   let @/ = l:pattern
   let @" = l:saved_reg
endfunction

vnoremap  * :call VisualSearch('f')
vnoremap  # :call VisualSearch('b')
"--< cut here >---

HTH,

-- 
JR 
[who has a vague remembrance that this subject has
come up before]


RE: search visual block

2006-10-17 Thread Max Dyckhoff
>From :help visual-operators


Note that the ":vmap" command can be used to specifically map keys in Visual
mode.  For example, if you would like the "/" command not to extend the Visual 
area, but instead take the highlighted text and search for that: >
:vmap / y/"


I would suggest not using the default yank register (it annoys me when things 
clear it without warning me!) so try this instead (which uses the 'q' register 
instead):
:vmap / "qy/q

Then whenever you press / with something highlighted in visual mode, it will 
get searched for!

Hope that helps!

Max

> -Original Message-
> From: Lev Lvovsky [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, October 17, 2006 4:47 PM
> To: David Thompson
> Cc: vim@vim.org
> Subject: Re: search visual block
>
>
> On Oct 17, 2006, at 4:36 PM, David Thompson wrote:
> > Why go to the trouble of highlighting it?
> >
> > Have you discovered the * key?  Just press the * key while in
> > normal mode and vim searches for the word under the cursor.
>
> I have indeed, very useful, however this is for multiple words
> separated by spaces.
>
> -lev
>



Re: search visual block

2006-10-17 Thread Lev Lvovsky


On Oct 17, 2006, at 4:36 PM, David Thompson wrote:

Why go to the trouble of highlighting it?

Have you discovered the * key?  Just press the * key while in
normal mode and vim searches for the word under the cursor.


I have indeed, very useful, however this is for multiple words  
separated by spaces.


-lev




Re: search visual block

2006-10-17 Thread David Thompson
--- David Thompson <[EMAIL PROTECTED]> wrote:
> --- Lev Lvovsky <[EMAIL PROTECTED]> wrote:
> > Is it possible to search for a string by selecting that string in  
> > visual mode?  Meaning, if I highlight something, and then want to  
> > search for that thing which is highlighted in the rest of the doc?
> 
> Why go to the trouble of highlighting it?
> 
> Have you discovered the * key?  Just press the * key while in
> normal mode and vim searches for the word under the cursor.   
> 
> See this tip,
> 
>   http://vim.sourceforge.net/tips/tip.php?tip_id=1

Btw, at the bottom of this tip, I added a comment about mapping
F10 to use hlsearch.  Here it is, it may do what you want,

  map  :set invhls:let @/=""/

Now use F10 key just like the * key, except F10 enables and
disables the "highlight search" mode.

See also,

  :help hlsearch

Regards,

David

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


Re: search visual block

2006-10-17 Thread David Thompson
--- Lev Lvovsky <[EMAIL PROTECTED]> wrote:
> Is it possible to search for a string by selecting that string in  
> visual mode?  Meaning, if I highlight something, and then want to  
> search for that thing which is highlighted in the rest of the doc?

Why go to the trouble of highlighting it?

Have you discovered the * key?  Just press the * key while in
normal mode and vim searches for the word under the cursor.   

See this tip,

  http://vim.sourceforge.net/tips/tip.php?tip_id=1

Regards,

David


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


Re: search visual block

2006-10-17 Thread Bill McCarthy
On Tue 17-Oct-06 4:03pm -0600, Lev Lvovsky wrote:

> Is it possible to search for a string by selecting that string in
> visual mode?  Meaning, if I highlight something, and then want to
> search for that thing which is highlighted in the rest of the doc?

You can do this with a visual map:

vnoremap / y/"

This will not always work - the visual area may contain
characters will special meaning in a pattern.

See

:h :y
:h c_CTRL-R_=

-- 
Best regards,
Bill



search visual block

2006-10-17 Thread Lev Lvovsky
Is it possible to search for a string by selecting that string in  
visual mode?  Meaning, if I highlight something, and then want to  
search for that thing which is highlighted in the rest of the doc?


otherwise, is there a way to copy that string so that I can later put  
it into the :/ command?


thanks!
-lev