Skip to content
Tags

,

Vim’s hanging indent

26/04/2012

This tip is not Linux-specific (since is available at so many platforms) but it is surely useful, at least to me, for daily programming or other editing activities that take place on Linux. I leave here this note, since this Vim’s feature is one that I always forget how to access and use it: I want to format paragraphs having a left, hanging indent.

Most of the times, while I create lists in my text files I want to format them like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Suspendisse enim orci, tristique et molestie non,
hendrerit convallis tellus.  Morbi vestibulum aliquam
urna, vestibulum sagittis sapien sagittis sed.  Duis
ultrices diam id felis mattis sollicitudin. Morbi id
hendrerit lorem. Morbi bibendum velit vel libero semper
id auctor nunc imperdiet.  Mauris at velit diam. Praesent
placerat ipsum ut massa bibendum faucibus.

   Etiam vitae tristique ipsum. Pellentesque dictum
   tristique ipsum, pulvinar sagittis nibh posuere in.
   Praesent in lectus eget lacus laoreet venenatis ut et
   ligula. Pellentesque habitant morbi tristique senectus
   et netus et malesuada fames ac turpis egestas. Integer
   blandit gravida neque at egestas. Nullam non erat at nulla
   vehicula vulputate sit amet sit amet purus.

   Nam scelerisque ullamcorper imperdiet. Donec ac magna
   vitae metus porta pharetra. Proin pharetra eleifend
   libero, id sollicitudin nunc tincidunt vel. Etiam urna
   purus, eleifend non dictum ac, euismod eget tellus.
   Curabitur iaculis auctor velit sit amet elementum.
   Suspendisse lacinia vestibulum dolor, eget lacinia purus
   pharetra eu.

Pellentesque nulla mauris, sodales a semper a, volutpat id
   massa.  Nullam dignissim volutpat elementum.
   Vestibulum eleifend varius ligula, et sagittis massa
   convallis in. Suspendisse venenatis tortor ut quam
   dignissim pretium. Ut eu interdum arcu. Duis orci enim,
   tempor eue porttitor dignissim, posuere et elit. Fusce
   sit amet turpis eu mi faucibus luctus quis vel odio.

Duis dignissim sollicitudin enim. Ut risus orci, dictum id
tristique id, molestie varius lorem. Vestibulum ante ipsum
primis in faucibus orci luctus et ultrices posuere cubilia
Curae; Aliquam tincidunt dolor id velit accumsan quis
semper felis condimentum. Nulla dui neque, gravida vel
egestas nec, porta sed ipsum. Cum sociis natoque penatibus
et magnis dis parturient montes, nascetur ridiculus mus.
Nullam nec nisi augue.

This paragraph style can be set up either during writing (so, new lines will be aligned  using the left indent of the previous line instead of starting at column 1) or while we are editing an already written paragraph using the features.

I am definitely sure this formatting can be achieved with Vim, but I always forget how. After reading the documentation (again!) for some time I realized why I forget this operation: it actually involves setting specific values at 2 different options.

First and most important as it seems, option autoindent needs to be enabled in order to have the hanging indent. Most of the times it is enabled when editing a source code file, but not on regular text files. Second, the list-option formatoptions needs to have the value 2 in its values, which forces Vim to format a paragraph using the indent of its second line instead of the first.

And since we are at this topic, option formatoptions have many more options and different operations. Let me conclude some of them:

TODO

For more information about these options, consult the fo-table tag in Vim’s help.

Advertisements
Leave a Comment

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: