0

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.

  • Look up :h ::. and the expand() function – muru Jan 31 '18 at 10:23
3

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

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

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.