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 silent * :call VisualSearch('f')CR
vnoremap silent # :call VisualSearch('b')CR
-- 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-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 silent * :call VisualSearch('f')CR
 vnoremap silent # :call VisualSearch('b')CR
 -- 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 . \CR
and
 execute normal / . l:pattern . \CR

HTH --Benji Fisher


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
CTRL-VCTRL-M.

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 silent * :call VisualSearch('f')CR
vnoremap silent # :call VisualSearch('b')CR

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 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 silent * :call VisualSearch('f')CR
vnoremap silent # :call VisualSearch('b')CR
-- 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 . \CR
and
 execute normal / . l:pattern . \CR

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 BS clears the command line.
 (From Zdenek Sekera [EMAIL PROTECTED]  on the vim list.)
 I added the final cr to restore the standard behaviour of
 cr to go to the next line and the mz and `z to return the cursor to its
 precommand position
:nnoremap CR mz:nohlsearchCR/BSCR`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 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
CTRL-VCTRL-M.

[...]

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

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
CTRL-VCTRL-M.

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 silent * :call VisualSearch('f')CR
vnoremap silent # :call VisualSearch('b')CR

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 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 . \CR
 and
  execute normal / . l:pattern . \CR

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 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 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 Enter to clear the highlighting 
according to this line in my .vimrc that I gave before:


:nnoremap CR mz:nohlsearchCR/BSCR`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 silent * :call VisualSearch('f')CR
vnoremap silent # :call VisualSearch('b')CR




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 silent * *:set hlsCR
nnoremap silent # #:set hlsCR

 in visual mode, use highlighted selection for search pattern
vnoremap silent * :call VisualSearch('f')CR:set hlsCR
vnoremap silent # :call VisualSearch('b')CR:set hlsCR
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 F10 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 silent F10 :call SetSearchReg()CR:set invhlsCR
function! SetSearchReg()
if hlsearch == 0
let @/ = expand('cword')
endif
endfunc

 my mind/fingers think of F10 as highlight search mode
 so make visual mode F10 and ShiftF10 work like * and #
vnoremap silent F10 :call VisualSearch('f')CR:set hlsCR
vnoremap silent S-F10 :call VisualSearch('b')CR:set hlsCR
-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 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 silent * :C-U
  \let old_reg=getreg('')bar
  \let old_regmode=getregtype('')cr
  \gvy/C-RC-R=substitute(substitute(
  \escape(@, '\\/.*$^~[]' ), \n$, , ),
  \\n, '\\_[[:return:]]', g)crcr
  \:call setreg('', old_reg, old_regmode)cr
vnoremap silent # :C-U
  \let old_reg=getreg('')bar
  \let old_regmode=getregtype('')cr
  \gvy?C-RC-R=substitute(substitute(
  \escape(@, '\\/.*$^~[]' ), \n$, , ),
  \\n, '\\_[[:return:]]', g)crcr
  \:call setreg('', old_reg, old_regmode)cr
else
 If you use both VIM 6.2 and older versions these mappings 
 should be defined depending on the current version.
vnoremap silent * :C-Ulet old_reg=@cr
  \gvy/C-RC-R=substitute(substitute(
  \escape(@, '\\/.*$^~[]' ), \n$, , ),
  \\n, '\\_[[:return:]]', g)crcr
  \:let @=old_regcr
vnoremap silent # :C-Ulet old_reg=@cr
  \gvy?C-RC-R=substitute(substitute(
  \escape(@, '\\/.*$^~[]' ), \n$, , ),
  \\n, '\\_[[:return:]]', g)crcr
  \:let @=old_regcr
endif



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 leader/ y/C-RCR

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



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 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 F10 :set invhlsCR:let @/=C-rC-wCR/BS

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 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 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/C-RCR


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/C-RqCR

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 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 silent * :call VisualSearch('f')CR
vnoremap silent # :call VisualSearch('b')CR
-- cut here ---

HTH,

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