Robert
Robert

Reputation: 15726

Using Vim, how do I 'set statusline' to align right?

My ~/.vimrc uses the following statusline setting

set statusline=%F%m%r%h%w\ %{&ff}\ %Y\ [0x\%02.2B]\ %l/%L,%v\ %p%%

Everything is left aligned. help 'statusline' says that the - character is used to "Left justify the item. The default is right justified when minwid is larger than the length of the item."

However, I haven't been able to use (or not use) - to ever align things to the right.

What is an example of having one group of items left aligned and one group right aligned?

I've also tried to use = but it just prints the = sign.

Upvotes: 16

Views: 7351

Answers (3)

BrightChen
BrightChen

Reputation: 11

Agree with Xavier T.

using %= which means right-align following items

The fallowing is my vimrc

set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}][TYPE=%Y][ASCII=\%03.3b][HEX=\%02.2B]%=[POS=%04l,%04v][%p%%][LEN=%L]

Upvotes: 1

Xavier T.
Xavier T.

Reputation: 42238

You must use %=

What is at the left of %= will be left aligned, and what is at the right of %= will be right aligned.

For example, here is the statusline I use.

set statusline=%f%m%r%h\ [%L]\ [%{&ff}]\ %y%=[%p%%]\ [line:%05l,col:%02v]

Upvotes: 6

Jeet
Jeet

Reputation: 39837

You need to prefix the = with a percent sign: %=.

Using your example:

set statusline=%F%m%r%h%w\ %{&ff}\ %Y\ [0x\%02.2B]\ %=l/%L,%v\ %p%%

Will right-align the "%l/%L,%v\ %p%%" group. You should also probably force a truncation using %< in a suitable place to accommodate narrow windows:

set statusline=%F%m%r%h%w%<\ %{&ff}\ %Y\ [0x\%02.2B]\ %=l/%L,%v\ %p%%

Upvotes: 19

Related Questions