2

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?

3

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
  • 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 – zool Apr 13 '16 at 8:33
  • No need of substitute(), to get the full path use :p, so in your case: expand('%:p:h') will do it. – MisterOccan Apr 13 '16 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# :( – zool Apr 14 '16 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? – user9433424 Apr 14 '16 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! – zool Apr 15 '16 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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