mapping question

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

mapping question

Sean Hubbell
Hello,

  Would anyone please let me know what I am doing wrong with the
following mapping:

nnoremap <F3> :let z=expand("DBR_" . expand
("<cword>"))<CR>%s;\=expand("<cword>");\=z;g<CR>


I would like to be able to edit a file and modify the word under the
cursor for all occurences of that word in the document with a prefix.
For example,


This that the other other

would be

This that the DBR_other DBR_other

if I selected the "other" word in the file.

Thanks in advance,

Sean
Reply | Threaded
Open this post in threaded view
|

RE: mapping question

Keith W. Roberts
 

> -----Original Message-----
> From: Sean Hubbell [mailto:[hidden email]]
> Sent: Friday, August 19, 2005 6:18 AM
> To: vim mailing list
> Subject: mapping question
>
> Hello,
>
>   Would anyone please let me know what I am doing wrong with the
> following mapping:
>
> nnoremap <F3> :let z=expand("DBR_" . expand
> ("<cword>"))<CR>%s;\=expand("<cword>");\=z;g<CR>
                 ^^
Insert : between the characters above carets.  The reason is that the <cr>
executes the first Ex command, putting you back in Normal mode.

-Keith



Reply | Threaded
Open this post in threaded view
|

Re: mapping question

Sean Hubbell
Keith W. Roberts wrote:

>
>
>  
>
>>-----Original Message-----
>>From: Sean Hubbell [mailto:[hidden email]]
>>Sent: Friday, August 19, 2005 6:18 AM
>>To: vim mailing list
>>Subject: mapping question
>>
>>Hello,
>>
>>  Would anyone please let me know what I am doing wrong with the
>>following mapping:
>>
>>nnoremap <F3> :let z=expand("DBR_" . expand
>>("<cword>"))<CR>%s;\=expand("<cword>");\=z;g<CR>
>>    
>>
>                 ^^
>Insert : between the characters above carets.  The reason is that the <cr>
>executes the first Ex command, putting you back in Normal mode.
>
>-Keith
>
>
>
>
>  
>
Thanks Keith.

Sean
Reply | Threaded
Open this post in threaded view
|

Re: mapping question

Tim Chase-2
In reply to this post by Sean Hubbell
>  Would anyone please let me know what I am doing wrong with the
> following mapping:
>
> nnoremap <F3> :let z=expand("DBR_" . expand
> ("<cword>"))<CR>%s;\=expand("<cword>");\=z;g<CR>

A couple things stand out as possible candidates for trouble

-you're expanding something that's already been expanded...I
don't know if it's intentional, but it stood out as a bit odd.
It might make sense in the context you're using it.  Perhaps to
look for a file-name consisting of "DBR_" prefixed to the word
currently under the cursor, returning an empty string if the file
doesn't exist?

-the first <CR> terminates the Ex command, so the following "%"
acts in normal mode (as this is a normal-mode mapping).  This is
the "jump to matching paren/brace" command.  Thus, you may need
another colon in there.

-you're using "\=" in the search clause of the substitute.  In
this context, it means "zero-or-one match of the previous atom",
and doesn't evaluate the expand() call.  For this, you may have
to wrap matters in an exec() call.

However, if I understand what you're trying to do, perhaps the
following mapping will do what you're trying to do?

        :nnoremap <f3> :%s;<c-r><c-w>;DBR_&;g<cr>

This makes use of the fact that in command-line mode, you can use
control+R followed by control+W to insert the word in your
document on which the cursor is currently sitting.  Something
akin to using expand("<cword>").

For more info, check out

        :help c_CTRL-R_CTRL-W

(where there are also bits for the filename matching under the
cursor, WORD under the cursor, etc)

Hope this gives you some leads to try,

-tim





Reply | Threaded
Open this post in threaded view
|

Re: mapping question

Sean Hubbell
Tim Chase wrote:

>> Would anyone please let me know what I am doing wrong with the
>> following mapping:
>>
>> nnoremap <F3> :let z=expand("DBR_" . expand
>> ("<cword>"))<CR>%s;\=expand("<cword>");\=z;g<CR>
>
>
> A couple things stand out as possible candidates for trouble
>
> -you're expanding something that's already been expanded...I don't
> know if it's intentional, but it stood out as a bit odd. It might make
> sense in the context you're using it. Perhaps to look for a file-name
> consisting of "DBR_" prefixed to the word currently under the cursor,
> returning an empty string if the file doesn't exist?
>
> -the first <CR> terminates the Ex command, so the following "%" acts
> in normal mode (as this is a normal-mode mapping). This is the "jump
> to matching paren/brace" command. Thus, you may need another colon in
> there.
>
> -you're using "\=" in the search clause of the substitute. In this
> context, it means "zero-or-one match of the previous atom", and
> doesn't evaluate the expand() call. For this, you may have to wrap
> matters in an exec() call.
>
> However, if I understand what you're trying to do, perhaps the
> following mapping will do what you're trying to do?
>
> :nnoremap <f3> :%s;<c-r><c-w>;DBR_&;g<cr>
>
> This makes use of the fact that in command-line mode, you can use
> control+R followed by control+W to insert the word in your document on
> which the cursor is currently sitting. Something akin to using
> expand("<cword>").
>
> For more info, check out
>
> :help c_CTRL-R_CTRL-W
>
> (where there are also bits for the filename matching under the cursor,
> WORD under the cursor, etc)
>
> Hope this gives you some leads to try,
>
> -tim
>
>
Thanks (again) for the great help Tim. I have tried a few things. I did
not know how to get around the expand in the substitute problem and this
takes care of that.

Sean
Reply | Threaded
Open this post in threaded view
|

Re: mapping question

Sean Hubbell
In reply to this post by Tim Chase-2
Tim Chase wrote:

>> Would anyone please let me know what I am doing wrong with the
>> following mapping:
>>
>> nnoremap <F3> :let z=expand("DBR_" . expand
>> ("<cword>"))<CR>%s;\=expand("<cword>");\=z;g<CR>
>
>
> A couple things stand out as possible candidates for trouble
>
> -you're expanding something that's already been expanded...I don't
> know if it's intentional, but it stood out as a bit odd. It might make
> sense in the context you're using it. Perhaps to look for a file-name
> consisting of "DBR_" prefixed to the word currently under the cursor,
> returning an empty string if the file doesn't exist?
>
> -the first <CR> terminates the Ex command, so the following "%" acts
> in normal mode (as this is a normal-mode mapping). This is the "jump
> to matching paren/brace" command. Thus, you may need another colon in
> there.
>
> -you're using "\=" in the search clause of the substitute. In this
> context, it means "zero-or-one match of the previous atom", and
> doesn't evaluate the expand() call. For this, you may have to wrap
> matters in an exec() call.
>
> However, if I understand what you're trying to do, perhaps the
> following mapping will do what you're trying to do?
>
> :nnoremap <f3> :%s;<c-r><c-w>;DBR_&;g<cr>
>
> This makes use of the fact that in command-line mode, you can use
> control+R followed by control+W to insert the word in your document on
> which the cursor is currently sitting. Something akin to using
> expand("<cword>").
>
> For more info, check out
>
> :help c_CTRL-R_CTRL-W
>
> (where there are also bits for the filename matching under the cursor,
> WORD under the cursor, etc)
>
> Hope this gives you some leads to try,
>
> -tim
>
>
>
>
>
>
How would one add word boundaries to c-w?

Sean
Reply | Threaded
Open this post in threaded view
|

RE: mapping question

Keith W. Roberts
 

> -----Original Message-----
> From: Sean Hubbell [mailto:[hidden email]]
> Sent: Friday, August 19, 2005 7:56 AM
> To: Tim Chase
> Cc: vim mailing list
> Subject: Re: mapping question
>
> Tim Chase wrote:
>
> >> Would anyone please let me know what I am doing wrong with the
> >> following mapping:
> >>
> >> nnoremap <F3> :let z=expand("DBR_" . expand
> >> ("<cword>"))<CR>%s;\=expand("<cword>");\=z;g<CR>
> >
> >
> > A couple things stand out as possible candidates for trouble
> >
> > -you're expanding something that's already been expanded...I don't
> > know if it's intentional, but it stood out as a bit odd. It
> might make
> > sense in the context you're using it. Perhaps to look for a
> file-name
> > consisting of "DBR_" prefixed to the word currently under
> the cursor,
> > returning an empty string if the file doesn't exist?
> >
> > -the first <CR> terminates the Ex command, so the following
> "%" acts
> > in normal mode (as this is a normal-mode mapping). This is
> the "jump
> > to matching paren/brace" command. Thus, you may need
> another colon in
> > there.
> >
> > -you're using "\=" in the search clause of the substitute. In this
> > context, it means "zero-or-one match of the previous atom", and
> > doesn't evaluate the expand() call. For this, you may have to wrap
> > matters in an exec() call.
> >
> > However, if I understand what you're trying to do, perhaps the
> > following mapping will do what you're trying to do?
> >
> > :nnoremap <f3> :%s;<c-r><c-w>;DBR_&;g<cr>
> >
> > This makes use of the fact that in command-line mode, you can use
> > control+R followed by control+W to insert the word in your
> document on
> > which the cursor is currently sitting. Something akin to using
> > expand("<cword>").
> >
> > For more info, check out
> >
> > :help c_CTRL-R_CTRL-W
> >
> > (where there are also bits for the filename matching under
> the cursor,
> > WORD under the cursor, etc)
> >
> > Hope this gives you some leads to try,
> >
> > -tim
> >
> >
> >
> >
> >
> >
> How would one add word boundaries to c-w?
>
> Sean
>
>

Surround with \< and \>, as:

  :nnoremap <f3> :%s;\<<c-r><c-w>\>;DBR_&;g<cr>

:h pattern-atoms

-Keith


Reply | Threaded
Open this post in threaded view
|

Re: mapping question

Tim Chase-2
In reply to this post by Sean Hubbell
>> :nnoremap <f3> :%s;<c-r><c-w>;DBR_&;g<cr>
[trim]
> How would one add word boundaries to c-w?

The ^R^W should default to using word boundaries, as defined
(IIUC) by the contents of your 'iskeyword' settings.  If you want
to include anything that's non-whitespace, you want what Vim
refers to as a "WORD" rather than a "word"

        :help word
        :help WORD

(they're right next to each other, so if you go to the first one,
just keep reading :)

To use a WORD instead, use

        <c-r><c-a>

instead of

        <c-r><c-w>

If, instead, you want to add the word boundaries to the
*searching*, just use the standard \< and \> as in

        nnoremap <f3> :%s;\<<c-r><c-w>\>;DBR_&;g<cr>

which seems to work for me.

-tim





Reply | Threaded
Open this post in threaded view
|

Re: mapping question

Sean Hubbell
Tim Chase wrote:

>>> :nnoremap <f3> :%s;<c-r><c-w>;DBR_&;g<cr>
>>
> [trim]
>
>> How would one add word boundaries to c-w?
>
>
> The ^R^W should default to using word boundaries, as defined (IIUC) by
> the contents of your 'iskeyword' settings. If you want to include
> anything that's non-whitespace, you want what Vim refers to as a
> "WORD" rather than a "word"
>
> :help word
> :help WORD
>
> (they're right next to each other, so if you go to the first one, just
> keep reading :)
>
> To use a WORD instead, use
>
> <c-r><c-a>
>
> instead of
>
> <c-r><c-w>
>
> If, instead, you want to add the word boundaries to the *searching*,
> just use the standard \< and \> as in
>
> nnoremap <f3> :%s;\<<c-r><c-w>\>;DBR_&;g<cr>
>
> which seems to work for me.
>
> -tim
>
>
>
>
>
>
I noticed that the match seems to match from the current position to the
end of the file. Is there a way to wrap this?
Also, the icing on the cake would be to disallow executing this twice.
For example, given:

DBR_this

do not allow a substitution since there already is a prefix?
I can live without this, but it would be nice.


Thanks again for the great help.

Sean


Reply | Threaded
Open this post in threaded view
|

Re: mapping question

Sean Hubbell

>> The ^R^W should default to using word boundaries, as defined (IIUC)
>> by the contents of your 'iskeyword' settings. If you want to include
>> anything that's non-whitespace, you want what Vim refers to as a
>> "WORD" rather than a "word"
>>
>> :help word
>> :help WORD
>>
>
Running gvim 6.3.46 on cAos, CTRL-RCTRL-W does not default to using word
boundaries. My iskeyword settings contain the following:

"@,48-57,_,192-255" which notes as the correct default setting. I can
investigate this more if anyone would like?

???

Sean
Reply | Threaded
Open this post in threaded view
|

Re: mapping question

Tim Chase-2
In reply to this post by Sean Hubbell
> I noticed that the match seems to match from the current position to the
> end of the file. Is there a way to wrap this?

I'm not quite sure I follow you...because of the "%" in the
mapping, it should be applied to all lines in the file, not just
from the current line to the end of file.  I tried it here and it
replaced stuff above the current location just fine.  You might
want to check your example (things like spelling, or occasions
for which there aren't word-boundaries around the missing case,
or something of the sort)

> Also, the icing on the cake would be to disallow executing this twice.
> For example, given:
>
> DBR_this
>
> do not allow a substitution since there already is a prefix?
> I can live without this, but it would be nice.

A little wrapping in an "if" statement (to ensure that the word
currently under the cursor doesn't begin with "DBR_") should do
the trick:

nnoremap <f3> :if match(expand("<cword>"), "^DBR_") < 0 <bar>
%s;\<<c-r><c-w>\>;DBR_&;g <bar> endif<cr>

If you shift to using WORDs instead, be sure to change both the
<cword> and the <c-r><c-w> to <cWORD> and <c-r><c-a>

-tim










Reply | Threaded
Open this post in threaded view
|

RE: mapping question

Keith W. Roberts
In reply to this post by Sean Hubbell
 

> -----Original Message-----
> From: Sean Hubbell [mailto:[hidden email]]
> Sent: Friday, August 19, 2005 8:13 AM
> To: Tim Chase
> Cc: vim mailing list
> Subject: Re: mapping question
>
> Tim Chase wrote:
>
> >>> :nnoremap <f3> :%s;<c-r><c-w>;DBR_&;g<cr>
> >>
> > [trim]
> >
> >> How would one add word boundaries to c-w?
> >
> >
> > The ^R^W should default to using word boundaries, as
> defined (IIUC) by
> > the contents of your 'iskeyword' settings. If you want to include
> > anything that's non-whitespace, you want what Vim refers to as a
> > "WORD" rather than a "word"
> >
> > :help word
> > :help WORD
> >
> > (they're right next to each other, so if you go to the
> first one, just
> > keep reading :)
> >
> > To use a WORD instead, use
> >
> > <c-r><c-a>

Hey, that's news even to me! :)  The <c-w> or <c-a> just determines what the
boundaries will be of the text *selected for inclusion*; they don't enforce
that the text *searched for* be word bounded.  Hence the use of \< and \>
fore and aft.

> > instead of
> >
> > <c-r><c-w>
> >
> > If, instead, you want to add the word boundaries to the
> *searching*,
> > just use the standard \< and \> as in
> >
> > nnoremap <f3> :%s;\<<c-r><c-w>\>;DBR_&;g<cr>
> >
> > which seems to work for me.
> >
> > -tim
> >
> >
> >
> >
> >
> >
> I noticed that the match seems to match from the current
> position to the
> end of the file. Is there a way to wrap this?

Not sure what you mean by this.  The % causes substitution in the entire
buffer (file).  If you just do a search with /<c-r><c-w> then:
- if incsearch is set, you will see the "next" match hilighted
- when you hit Enter, if hlsearch is set, all the matches will be hilighted
- if repeat using 'n', you will see each successive match till the eof
(unless wrapscan is set, in which case it'll wrap to the top of file and
continue)

:h is
:h hls
:h ws

> Also, the icing on the cake would be to disallow executing
> this twice.
> For example, given:
>
> DBR_this
>
> do not allow a substitution since there already is a prefix?
> I can live without this, but it would be nice.

Well, ummm... if you are executing this map, presumably you're looking right
at the word, and can see that there's a prefix already there, but ... :))

Kidding aside, doing that gets into some of the hairier regex forms.  I'll
let someone else tackle that one.

-Keith


Reply | Threaded
Open this post in threaded view
|

Re: mapping question

Tim Chase-2
> Well, ummm... if you are executing this map, presumably you're
> looking right at the word, and can see that there's a prefix
> already there, but ... :))

Well, to modify my previous suggestion, how about this addition?

nnoremap <f3> :if match(expand("<cword>"), "^DBR_") < 0 <bar>
%s;\<<c-r><c-w>\>;DBR_&;g <bar> else <bar> echoerr "Hey, doofus!
  It's already got one! :*)" <bar> endif<cr>

just a little fun for ya... :)

-tim