Help with user defined command

43 views
Skip to first unread message

howardb21

unread,
May 16, 2012, 6:32:23 PM5/16/12
to vim_use
Hi Ben and others, I need help correctly writing a user command.
Others have suggested how I can do it, but I have not gotten the
suggestions that work. I want to write a user command that executes
this command:

:abbreviate <exrc> ihs somefunction(ihs,irs) <CR>

where ihs and irs are strings

How do I write such a user command? I've tried different combinations
of exe and quotes and <f-args>, but can not seem to get it correct. I
would appreciate someone writing out the exact user command that would
result in the above command being executed.

Thanks

John Beckett

unread,
May 16, 2012, 9:39:58 PM5/16/12
to vim...@googlegroups.com
howardb21 wrote:
> I want to write a user command that executes this command:
>
> :abbreviate <exrc> ihs somefunction(ihs,irs) <CR>
>
> where ihs and irs are strings

I expect you mean <expr> and not <exrc>?

I must have missed the original thread, but I think it would be
good to start again with a statement of what problem needs to be
solved. I'm wondering why 'ihs' appears twice, and what would
you want the user command to be?

How about a simple example showing what you would like to enter
and what it should do.

John

Ben Fritz

unread,
May 17, 2012, 11:33:56 AM5/17/12
to vim...@googlegroups.com

It's been a while, but I have a vague memory. I think he's trying to set up an abbreviation the same way in a loop or function or something, with different left side and right side.

I think he wants something like:

exec 'abbreviate <expr> '.lhs.' somefunction('.lhs.','.rhs.')'

But I'd need to dig up the old thread to be sure.

Or howardb21, you could post the actual problem again.

Christian Brabandt

unread,
May 17, 2012, 11:52:20 AM5/17/12
to vim...@googlegroups.com
Hi Ben!
I think it started here:
http://groups.google.com/group/vim_use/msg/5d2c72f422a76745

regards,
Christian
--

howardb21

unread,
May 18, 2012, 3:10:05 PM5/18/12
to vim_use
On May 16, 6:39 pm, "John Beckett" <johnb.beck...@gmail.com> wrote:

> I think it would be good to start again with a statement of what problem
> needs to be solved. I'm wondering why 'ihs' appears twice, and what would
> you want the user command to be?
>
> How about a simple example showing what you would like to enter
> and what it should do.

OK There is a form of the abbreviation command like this:

    ab <expr> string expression

For this purpose I want to use this function for the expression:

function! ABB(lhs, rhs)
    if getcmdtype() == ':'
        return a:lhs
    else
        return a:rhs
    endif
endfunction

So after I execute the command,

ab <expr> howard ABB("howard","schwartz")

each time I type howard it will be replaced by schwartz, except when
I am on the  : command line. In that case, howard will not be replaced
by
schwartz.

I want to write a user defined command that does the same
thing as the above, ab, command. Let's call the user command Ab. Then,

Ab howard schwartz

will have the same effect as executing the command,

ab <expr> howard ABB(howard,schwartz)

The string, 'howard', must occur twice in the ordinary abbreviation
command
above - once as the initial string, and again as the first argument of
the
function ABB().

So I must, extract `howard' from the expressions <args> or <f-args>
that
evaluate to all arguments of a user command.

I know this will not work, but here is a sample command that might do
the
trick:

command! =nargs=2 ab <expr> matchstr(<args>,"^.* ") ABB(<f-
args>)

if <args> evaulated to "howard Schwartz" than the matchstr function
might
return the first characters followed by a blank (which would be
"howard ")

I want to define a user command like that that will really work! Why?
Because,

Ab howard schwartz

is a lot simpler than,

ab <expr> howard ABB(howard,schwartz)











Ben Fritz

unread,
May 18, 2012, 3:55:47 PM5/18/12
to vim...@googlegroups.com
Have you tried the command Christian created to do exactly this in your original thread?

https://groups.google.com/d/topic/vim_use/46_Na5k0Ku8/discussion

Note his solution uses <q-args> and the split() function to get the individual pieces. You could also probably use <f-args> and just use the arguments separately.

howardb21

unread,
May 19, 2012, 5:54:40 PM5/19/12
to vim_use


On May 18, 12:55 pm, Ben Fritz <fritzophre...@gmail.com> wrote:

> Have you tried the command Christian created to do exactly this in your original thread?
>
>  https://groups.google.com/d/topic/vim_use/46_Na5k0Ku8/discussion
>
> Note his solution uses <q-args> and the split() function to get the individual pieces. > You could also probably use <f-args> and just use the arguments separately.

I tried it and it did not seem to work. Personal taste wise - the
split function and printf seemed like overkill - I thought there
should be a simpler way. And the
special form of ab <expr> string expression seems made to order for
what I was after. That is why I wanted to just use this command, but
in a simpler form.

Ben Fritz

unread,
May 20, 2012, 5:57:39 PM5/20/12
to vim...@googlegroups.com
On Saturday, May 19, 2012 4:54:40 PM UTC-5, howardb21 wrote:
>
> I tried it and it did not seem to work. Personal taste wise - the
> split function and printf seemed like overkill - I thought there
> should be a simpler way. And the
> special form of ab <expr> string expression seems made to order for
> what I was after. That is why I wanted to just use this command, but
> in a simpler form.

What do mean by "it doesn't work"? What did it do instead of creating the desired abbreviation? As for a simpler way, did you try using the same approach but using <f-args> instead of all the split stuff? If so, what EXACT commands did you use? We cannot help without more detail about what went wrong.

Reply all
Reply to author
Forward
0 new messages