Truncate back of filename instead of front when not enough space in multi-tab vim

Viewed 183

If I don't have enough terminal columns to display all filenames in a multi-tab vim session, it truncates the front of the filenames in order to fit them all into the tabs:

With enough space:

enter image description here

Without enough space:

enter image description here

As you can see it has truncated the names from the front, which makes them harder to find. Can I reverse this behaviour to make vim truncate from the back of the name?

1 Answers

TL;DR: There's no simple option for that, but the (GUI and terminal) tabline is fully customizable, so you can indeed tweak it according to your needs.

:help setting-tabline both provides the documentation for tweaking the tabline as well as an example MyTabLine() function that

is just a simplistic example that results in a tab pages line that resembles the default, but without adding a + for a modified buffer or truncating the names.

It then even mentions your wanted behavior:

You will want to reduce the width of labels in a clever way when there is not enough room.

Implementing the truncation

In order to truncate from the right, you can use strpart() (but it would be based on character count and requires explicit handling for non-ASCII characters) or matchstr(). Combining the latter with the /\%v regular expression atom, you can match for character width, handling tabs and double-width characters, too:

:echo matchstr('a 日本語 string that is too long', '^.*\ze\%>10v.*$')
a 日本語 s

Library functions like from my ingo-library plugin can deliver a truncation indicator in addition:

:echo ingo#strdisplaywidth#TruncateTo('a 日本語 string that is too long', 10)
a 日本語 …
:echo ingo#avoidprompt#TruncateTo('a 日本語 string that is too long', 10)
a 日…long

Related