Use current directory in Vim commands

For my Haskell programs, I know that the executable name in the path matches the name of the current directory. Now I want to create a mapping like this:

:map <leader>rr :!curdir()<cr> 

However, the only command I know is getcwd() , which gives me the whole path, not just the directory name.

Is there an easy way to extract only the directory name?

+7
source share
2 answers

Using

 fnamemodify(getcwd(), ':t') 

or

 fnamemodify('.', ':p:h:t') 

. :h in the second case is necessary because :p emits a trailing path separator (thus, the last component of the path selected by :t is now an empty string).

To move this to your mapping use

 :noremap \rr :!<Cr>=shellescape(fnamemodify('.', ':p:h:t'), 1)<CR><CR> 

. For a description of why you should never use :map , see here .

+13
source

You can use something like:

 split(getcwd(), "/")[-1] 
0
source

All Articles