2

Does anyone know how I can get the directory name of the CWD without the preceding path?

I'm trying to build a titlestring like this one:

{cwd_dirname_without_path}/{file_path_relative_to_cwd}

I think %f would suffice for the second part of the title, but the first part seems tricky.

1
  • Look up :h ::. and the expand() function
    – muru
    Jan 31, 2018 at 10:23

1 Answer 1

12

There are several possibilities:

  • Use an substitute: :echo substitute(getcwd(), '^.*/', '', '') (which basically means, cut off anything from the beginning until the latest /, note this needs to be adjusted on Windows because the \ is path separator there).

  • Using fnamemodify(), something like this should do it: :echo fnamemodify(getcwd(), ':t') (which basically means, return the tail path of the current working directory).

See also the help at :help getcwd() :help fnamemodify() :help getcwd() :help substitute() and :help filename-modifiers

1
  • Works like a charm! Thanks! I have been struggling with this since your answer to my last question :p
    – Rotareti
    Jan 31, 2018 at 10:37

Your Answer

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

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