Regular expression as a trigger

One of the code conventions at work is to include a padding of one space inside square brackets and parenthesis. So list[ index ], not list[index]. Adding those spaces can be annoying, so I tried writing a snippet:

# name: Bracket
# key: [
# --
[ ${1:exp} ]

This works when the opening square bracket is preceded by a space or beginning of a line, but not when its preceded by an identifier. I think one way to do this would be to have the trigger be a regular expression:

# key: "[:ascii:]"[

Or something like that. Is this even possible? Or is there some other clever way to make this work? I'm writing this for python, but I think that shouldn't matter.



Minor mode solution

I too have had to deal with such a coding standard. My initial solution was identical to @Francesco's based on autopair.

However, I actually wanted to remove this padding in some cases, so made this minor-mode to be more flexible. I definitely recommend using it along with some kind of paired delimiter insertion tool (my favorite is autopair).

See: delim-pad

Using yasnippet

Going down your original track of using yasnippet ... ( BTW autopair and yasnippet are both written by João Távora, both are very powerful and flexible. kudos to that guy! )

Even if you got "[" recognized as a key, you would still have to keep pressing the trigger key to expand the snippet. That can get tired pretty quickly.

Yasnippet also allows you to bind snippets to keys directly, so this will work:

# -*- mode: snippet -*-
# name: beginsquare
# binding: [
# --
[ $0 ]

I don't know about YASnippet, but what you want can easily be achieved using autopair (which can be installed via the packaging system):

(defun autopair-space-after-bracket (action pair pos-before)
  (when (and (eq action 'opening)
             (eq pair ?\]))
    (insert "  ")

(setq autopair-handle-action-fns (list #'autopair-default-handle-action

Using this, and starting from the following situation (| marks point position):


then inserting [ yields:

list[ | ]

The reason why this is not working, is because you are trying to expand on list[ as opposed to [ when something is before this square bracket. Yasnippet is "not your guy" I would say for creating an expansion on [. I'll show you at the end the more common use of yasnippet.

Considering that I would not really like to press the activation key (e.g. TAB) whenever I would type a bracket, I would go with the above answer from Francesco. If it helps, I also use autopair.

Furthermore, a good, or at least better way to use yasnippet is if you were indeed interested in using this only for list, or some situations which you are willing to define beforehand. Consider creating many snippets for code you use often and you'll be producing code really fast. Anyway, here is the example that I think suits your need for this situation:

list[ $0 ]

Bind it to either list or even shorter to l, whatever floats your boat.

Personally I would go with a combination of both autopair and yasnippet, they are both great. Good luck!

Copy paste:

# name: List
# key: list
# --
list[ $0 ]

Need Your Help

MySQL - Designing database for a simple game

mysql sql database database-design

I am designing a database for a simple browser based game.

How to test if a Java iterator always uses the same order (reproducible ordering)?

java collections iterator set order

I have a code in which for-each-loops on a Set need to rely on the fact that the iterator returns the elements always in the same order, e.g.