Wednesday, July 20, 2011

Re: How to get the directory name of the current file

On Wed, 20 Jul 2011, Bee wrote:

> On Jul 20, 6:52 am, "Benjamin R. Haskell" wrote:
>> On Wed, 20 Jul 2011, 何聪辉 wrote:
>>> Hi, I am using vim as the simple IDE, and use qmake and make tool to
>>> configure my project. If a directory named "Foo", and inside the
>>> directory, there exists some headers and source files, I use the
>>> command "qmake -project", and it will generate a file called
>>> "Foo.pro", After I type "qmake", it will automatically generate the
>>> Makefile that I want, and by default, the target of the Makefile is
>>> the name of the directory, that is, "Foo". Now I want to do some
>>> mapping, say map <C-F5> to execute the executable file, "Foo". So I
>>> need to get the name of the directory. How can i achieve that?
>>
>> You want:
>>
>> %:p:h:t
>> or
>> expand('%:p:h:t')
>>
>> (depending on context)
>>
>> --
>> Best,
>> Ben
>
> Looking at help it seems :t cannot be used with :h
>
> help %:h
>
> :h Head of the file name (the last component and any separators
> removed).
> Cannot be used with :e, :r or :t.

Huh. Bizarre. It works fine from my testing. Perhaps it just means
that once you've used :e, :r, or :t you can no longer recover the
portion that :h would return?

So:
test under alpine test under Windows
expand('%:p') /tmp/pico.92409 C:\Users\bhaskell\Desktop\test.txt
expand('%:p:h') /tmp C:\Users\bhaskell\Desktop
expand('%:p:t:h') pico.92409 test.txt
expand('%:p:h:t') tmp Desktop

If the OP really wants 'Foo', rather than /home/projects/Foo, %:p:h:t is
the way to go.

--
Best,
Ben

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment