Fergal Moran
Fergal Moran

Reputation: 4634

vim Aligning inline comments

Not really sure how to phrase this other than by example..

Given...

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',    # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': 'adroit',                      # Or path to database file if using sqlite3.
    'USER': 'root',                    # Not used with sqlite3.
    'PASSWORD': '',                      # Not used with sqlite3.
    'HOST': '',                         # Set to empty string for localhost. Not used with sqlite3.
    'PORT': '',                            # Set to empty string for default. Not used with sqlite3.
}

}

How would I format it so that all of the comments line up correctly, like so

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',  # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': 'adroit',                      # Or path to database file if using sqlite3.
    'USER': 'root',                        # Not used with sqlite3.
    'PASSWORD': '',                        # Not used with sqlite3.
    'HOST': '',                            # Set to empty string for localhost. Not used with sqlite3.
    'PORT': '',                            # Set to empty string for default. Not used with sqlite3.
}

}

The above example is from a django settings file, but I come across this issue a lot in various languages so ideally I would be looking for a way of doing this regardless of the comment delimiter.

Upvotes: 5

Views: 799

Answers (1)

lucapette
lucapette

Reputation: 20724

Use tabular. With this plugin you can just visually select the part you want to align and then type:

'<,'>Tabularize /#

It works so nicely that it looks like magic.

Upvotes: 5

Related Questions