Backstory

I really wanted to have the filename of the file I was editing to be a different color than the path of the file. Using statusline without any tricks would only provide just the filename or the full path with filename.

Solution

bufname and fnamemodify to the rescue!

Using bufname with '%' as an argument: bufname('%') will return the filename of file currently in the buffer.

Using fnamemodify with the above and the ':p:h' as escape codes will give you the entire path of the passed file and then cut off the filename.

All together it looks like: fnamemodify(bufname('%'), ':p:h')

My full statusline (with helpful comments)

[code lang=text]
" put paste info
set statusline=%#error#%{SL('HasPaste')}%*
" set color to 'search'
set statusline+=%#todo#
" if a help file or not
set statusline+=%h
" if modified or not
set statusline+=%m
" set color to 'error'
set statusline+=%#error#
" read-only or not
set statusline+=%r
" reset color
set statusline+=%*
" FILETYPE (COMMENTED OUT FOR NOW)
"set statusline+=%y\
" column number
set statusline+=\ %c,
" line/total lines
set statusline+=%l/%L
" percent of file
set statusline+=\ %P
" Move to the right align
set statusline+=%=
" put up to 50 characters of filename and path
set statusline+=\ %30.30{fnamemodify(bufname('%'),':p:h')}/
set statusline+=%#statusbold#
set statusline+=%t
" space at end to make it easier to read
set statusline+=\
[/code]

Some other tips:

To add a space you need to use: \_ where _ is actually a space.
You can define your own highlight names (seen above with #statusbold#) with:

[code lang=text]
highlight statusbold term=bold,reverse ctermfg=5 ctermbg=118 gui=bold,reverse
[/code]

Leave a Reply