4

I can use %t in my statusline to display the file name (tail) of the file in the buffer, but there's no corresponding option to display only the path, without the file name.

What I'm after: I'd like to display the path to the file in the buffer (as %f does), but I want the file name to stand out by giving it a different color. So if there were a corresponding %h (head) item I would just do:

set statusline=
set statusline+=%h           " Path to file (head)
set statusline+=%#Keyword#   " Switch color
set statusline+=%t           " File name (tail)

Does anyone know how can I achieve this?

1 Answer 1

5

Maybe you could use the following:

set statusline=
" Path to file (head)
set statusline+=%{expand('%:h')}/
" Switch color
set statusline+=%#Keyword#
" File name (tail)
set statusline+=%t

%{} allows you to evaluate an expression, which here is expand('%:h') (the head of the current file name).

Edit:

Your last comment says that you don't want to expand the tilda ~, and that you want the head of the path to use the highlight group of the statusline.

For the 1st problem, you could add the filename modifier :~ like this:

set statusline+=%{expand('%:~:h')}

For the 2nd problem, you could use the highlight group StatusLine:

set statusline+=%#StatusLine#

But it seems that it wouldn't give the desired background color.
So you could type :hi to get a list of all the highlight groups currently defined on your system, look for the one whose colors you're interested in and use its name to set up the colors of the head of the path.

It would give something like:

set statusline=
set statusline+=%#YourHighlightGroup#
set statusline+=%{expand('%:~:h')}/
set statusline+=%#Keyword#
set statusline+=%t
5
  • 1
    Almost there. That gives me the full path of the file in the buffer (/Users/name/directory/), not the compact output I get from %f (~/directory/). Also, the background color is different from the rest of the status line, no idea why: imgur.com/UaIrNND
    – mrzool
    Commented Apr 13, 2016 at 8:33
  • No need of substitute(), to get the full path use :p, so in your case: expand('%:p:h') will do it. Commented Apr 13, 2016 at 15:39
  • Still not getting the compact output I get with %f: with expand('%:~:.:h') I only get the path from the current directory, with expand('%:p:h') I get the full path from root. And the problem with the background color persists even if using %#StatusLine# :(
    – mrzool
    Commented Apr 14, 2016 at 10:23
  • @zool Have you tried to remove the :. modifier to get this %{expand('%:~:h')}? For the background color, you could type :hi to see all the highlight groups installed, look for the one whose colors you're interested in, then note its name to replace %#StatusLine#. If this doesn't work, could you post the output of :verb hi StatusLine and :verb hi Keyword? Commented Apr 14, 2016 at 16:16
  • @user9433424 worked beautifully with %{expand('%:~:h')} and by hunting for the right highlight group through :hi (the wrong background color with StatusLine was indeed a color scheme issue). If you edit your answer accordingly I will accept it. Thank you very much for your help!
    – mrzool
    Commented Apr 15, 2016 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.