Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the current directory in Vim commands

Tags:

vim

For my Haskell programs, I know that the executable's name in the path is the same as my current directory's name. Now I want to create a mapping like so:

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

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

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

like image 838
Sacchan Avatar asked Dec 18 '12 15:12

Sacchan


1 Answers

Use

fnamemodify(getcwd(), ':t')

or

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

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

To move this into your mapping use

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

. For the description of why you should not ever use :map see here.

like image 159
ZyX Avatar answered Oct 24 '22 08:10

ZyX