Friday, July 6, 2012

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

On Friday, July 6, 2012 3:05:09 AM UTC-5, Roberto S. wrote:
> I'm learning VIM so probably this is a simple question for you...
>
> I have created my first macro to generate getters from selected fields in a java file:
> au BufRead,BufNewFile *.java let @g="^cwpublic2wyw$p2bcl() { return this.A; }2b"
>
> So with a class:
> public class Customer {
> private String name;
> private int age;
> }
>
> I can VISUAL SELECT "name" and "age" rows and press :norm @g and Enter.
> Then I tryed with a key mapping (<Leader>get) and it runs.
> au BufRead,BufNewFile *.java nmap <Leader>get ^cwpublic<ESC>2wyw$p2bcl() { return this.<ESC>A; }<ESC>2b
>
> At the end I would like to do the same thing with a custom command called Jget
> au BufRead,BufNewFile *.java command! Jget ^cwpublic<ESC>2wyw$p2bcl() { return this.<ESC>A; }<ESC>2b
>
> This does not run. I tryed also with a "exe" after Jget, but nothing.
> What am I doing wrong?
>

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.

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