On 2007-04-19, "John R. Culleton" <[EMAIL PROTECTED]> wrote:
> I want a single F key assignment via user's gvimrc to activate several 
> commands in order, e.g.:
> %s/^"/``/
> %s/ "/ ``/g
> %s/"$/''/
> %s/" /'' /g
> (The left hand part contains an inch sign and the right hand part 
> contains either two back ticks or two apostrophes.) I may add more 
> such commands to the same F key. 
> 
> Can I string them all together in a gvimrc command or would it be 
> handier to have the F key call a script somehow? This will be for a 
> naive user.  (I am pretty naive myself :)

You can certainly string them together in a single mapping using 
<bar> as a separator and <CR> as a terminator, e.g.,

    nmap <F2> :%s/^"/``/ <bar> %s/ "/ ``/g <bar> %s/"$/''/ <bar> %s/" /'' /g<CR>

However, that mapping will fail as soon as one of the substitutions 
fails, so a better mapping would include the 'e' flag at the end of 
each substitution, e.g.,

    nmap <F2> :%s/^"/``/e <bar> %s/ "/ ``/ge <bar> %s/"$/''/e <bar> %s/" /'' 
/ge<CR>

That mapping will leave the command visible in the command line 
after the user presses the function key, and if the command is long 
or the window is narrow, the user will also get the "Press ENTER or 
type a command to continue" prompt when the command is done.  To 
avoid that, you can add <silent> to the mapping, e.g.,

    nmap <silent> <F2> :%s/^"/``/e <bar> %s/ "/ ``/ge <bar> %s/"$/''/e <bar> 
%s/" /'' /ge<CR>

Such a long mapping can be difficult to maintain.  One way to make 
that easier is to put the commands in a function and have the 
mapping just call that function, e.g.,

    nmap <silent> <F2> :call DoSub()<CR>

    function DoSub()
        %s/^"/``/e
        %s/ "/ ``/ge
        %s/"$/''/e
        %s/" /'' /ge
    endfunction

See also

    :help :bar
    :help map_bar
    :help :s_flags
    :help map-<silent>
    :help user-functions

HTH,
Gary

-- 
Gary Johnson                 | Agilent Technologies
[EMAIL PROTECTED]     | Mobile Broadband Division
                             | Spokane, Washington, USA

Reply via email to