My current statusline (in .vimrc) contains %F to show the current filename.

I want to perform a string substitution in %F. More specifically I want to substitute ~/remotes/foo -> [foo]


  • ~/remotes/foo/myproject/file.py -> [foo]/myproject/file.py
  • ~/remotes/bar/other_project/main.py -> [bar]/other_project/main.py

In plain Vim I'd do something like s/~\/remotes\/foo/[foo].

How to to that in statusline ?

I've tried some combinations like

:set statusline=%F
:set statusline=%{substitute(statusline, 'foo', 'bar', 'g')}

witout success.

1 Answer 1


With a little sprinkling of :help filename-modifiers, :help expand() gives you the equivalent of %F:

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

From there, you can perform the desired substitution:

set statusline=%{expand('%:p:~')->substitute('\\~\/remotes\/foo','[foo]','')}

Note that the resulting filename will always have either of those three forms:

  • /foo/bar/baz.txt if outside of ~,
  • ~/foo/bar/baz if inside of ~,
  • [foo]/bar/baz if inside of ~/remotes/foo.

It is OK given your explicit requirements but, if you want more fine-tuning, you will have to write your own function. See also :help fnamemodify().

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.