Can I write an abbreviation that only works at the beginning of the line?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Can I write an abbreviation that only works at the beginning of the line?

Jose Caballero
Hi,

I am asking here because I am not sure myself how to google it.
I tried with something similar to the subject of this email, but no
luck. That, or I didn't actually understand what I found.

Let's say, and this is not my real need but a very clear example, I
want to write an abbreviation that converts, when writing python code:

class

into

class (object):

But I only want that to works when string "class" is being printed at
the beginning of a line.
I don't want the replacement to happen when I am writing the docstring
like "This is a class that does this and that".

Is it possible?

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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Can I write an abbreviation that only works at the beginning of the line?

Dmitri Vereshchagin
On Sun, Aug 27, Jose Caballero wrote:

>Let's say, and this is not my real need but a very clear example, I
>want to write an abbreviation that converts, when writing python code:
>
>class
>
>into
>
>class (object):
>
>But I only want that to works when string "class" is being printed at
>the beginning of a line.
>I don't want the replacement to happen when I am writing the docstring
>like "This is a class that does this and that".
>
>Is it possible?

You can use something like this

    iabbrev <expr> class getline('.') ==# 'class' ? 'class (object):' : 'class'

The key moment here is "<expr>".  The expression

    getline('.') ==# 'class' ? 'class (object):' : 'class'

will be evaluated to obtain replacement string.  You can make it more
accurate by comparison only with the part before the cursor position.

-Dmitri

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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.