Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim Aligning inline comments

Tags:

vim

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.

like image 941
Fergal Moran Avatar asked Mar 23 '12 16:03

Fergal Moran


1 Answers

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.

like image 182
lucapette Avatar answered Oct 11 '22 17:10

lucapette