Friday, July 6, 2012

Re: custom commands executing a sequence of commands (macro)...

On Fri, Jul 6, 2012 at 12:04 PM, Ben Fritz <fritzophrenic@gmail.com>
wrote:

> On Friday, July 6, 2012 3:05:09 AM UTC-5, Roberto S. wrote:
>
> The :command command defines a user-command which runs an ex command,
> not a series of normal-mode commands. You probably need to use the
> :normal ex command, which DOES run a series of normal-mode commands.
> Try normal! ^cwpublic.... instead of just stringing the commands in
> there.
>
> To enter special characters, you may need to combine it with :exec and
> use the special escape syntax for strings, e.g.
>
> exec "normal! ^cwpublic\<ESC>..."
>
> Or, instead of using exec, you can enter the special characters
> literally by typing CTRL+V and then the character you need, in insert
> mode.
>
> Note, in a script I'd probably do this with a substitute command
> instead of using normal-mode commands, but since you already have a
> normal-mode command sequence to do what you want, it's probably faster
> to do as you're doing.

To summarize, what you're trying to do is to get Vim to execute your
particular sequence as if you were hitting the keys directly, NOT as an
actual command you type on the command-line.

To make life a little easier, you could use this command I wrote a while
ago (to make MY life a little easier):

function! Normal( bang, args )
let mapCommand = a:bang == '!' ? 'noremap' : 'map'

let g:Normal_mapCount = GetVar#GetSafe( 'g:Normal_mapCount', 0 ) + 1

execute mapCommand . ' Normal_map' . g:Normal_mapCount . ' ' . a:args
execute 'normal Normal_map' . g:Normal_mapCount
execute 'unmap Normal_map' . g:Normal_mapCount

let g:Normal_mapCount -= 1

" Clean it up, if we're done.
if ( g:Normal_mapCount == 0 )
unlet g:Normal_mapCount
endif
endfunction
com! -bang -nargs=+ Normal call Normal( <q-bang>, <q-args> )

Basically, now, instead of

> exec "normal! ^cwpublic\<ESC>..."

You can just execute

> Normal! ^cwpublic<ESC>...

It does the same thing as the built-in :normal, but doesn't require the
use of an execute statement and forcible escaping of character
sequences. On the other hand, if you have genuine need of < and > in
your sequence, then you have to use the built-in version.

(In case someone's wondering, it's a little complex because of the need
to allow for the Normal command containing other Normal commands.)

Hope this helps,

Salman

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