Tuesday, July 17, 2012

Re: Key mapping behaves differently than same sequence of keys struck by hand

On Tue, July 17, 2012 17:36, porphyry5 wrote:
> On Monday, July 16, 2012 5:12:18 PM UTC-7, Gary Johnson wrote:
>> On 2012-07-16, Graham Lawrence wrote:
>> > As a keystroke sequence
>> > hEa"<Esc>Bi"<Esc>j
>> > seems to enquote the current word anywhere in the line.
>> > In particular, it does so if the cursor is in column 1 at the start
>> of
>> > the first word.
>> >
>> > But as a key mapping, e.g.
>> > :map <F5> hEa"<Esc>Bi"<Esc>j
>> > it fails if the cursor is in column 1
>> >
>> > Why the difference?
>>
>> The answer is buried rather deep in the documentation, in the third
>> paragraph below
>>
>> :help map_return
>>
>> Note that when an error is encountered (that causes an error
>> message or beep) the rest of the mapping is not executed. This
>> is Vi-compatible.
>>
>> Regards,
>> Gary
>
> I had disregarded that because there is neither beep nor message, nothing
> happens at all when the cursor is in column 1.

Yes there is, the mapping will be aborted, if your cursor is on the
first column or in the last line.
:set visualbell shows that, when issuing 'h' on the first column.

One way around it, is to use an expression mapping, e.g. something
like this:

:map <expr> <F5> QuoteWord()

fu! QuoteWord()
let r=''
if col('.') > 1
let r.='h'
endif
let r.='Ea"'. "\<Esc>Bi". '"'. "\<Esc>"
if line('.') < line('$')
let r.='j'
endif
return r
endfu

Or make use of the text-object (:h text-object)
:map <f5> ciW"<C-R>""<Esc>j

regards,
Christian

--
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

No comments:

Post a Comment