On 14/11/08 20:54, madiyaan wrote: > Hello: > > Is there a way to open a file relative to the current file's > directory? > > I do not want to change the directory to the current file's directory > because I often invoke :!make from the source directory's root. (But > maybe there is a way to change to the current directory, and go back > to the root directory before invoking main... that would be equally > useful to me). > > For example, I am in project/ and I invoke: > > vim lib/tools/tool1.cpp > > I don't want to change my directory to project/lib/tools/ because when > I invoke :!make, it will use the Makefile in project/lib/tools/ and > not the one that I intend to use, which lies in project/. I am wanting > to know whether I can easily open up project/lib/tools/tool2.cpp > without typing in the entire path. > > Regards,
The current file's directory is expand('%:p:h'). So you could use (untested) function CurFileRelPath(path) let hasdoswin = has('win32') || has('win64') || has('win16') \ || has('dos32') || has('dos16') if a:path[0] == '/' return a:path endif if hasdoswin && (a:path[0] == '\' || a:path =~ '^\a:') return a:path endif let prefix = expand('%:p:h') if prefix[-1:] != '/' \ || (hasdoswin && prefix[-1:] != '\') let prefix .= '/' endif return prefix . a:path endfunction Then you could invoke it anywhere in a command typed at the command-line by means of Ctrl-R followed by =CurFileRelPath('something') then Enter. 'something' (which you should enter with the quotes) will be resolved as a relative path unless its first character (or its first one or two characters in Dos or Windows) seem to indicate an absolute path. I'm not treating x:abcd (which is relative to Drive X's current directory) as a relative path, this is intentional. Note that expand('%:p:h') will usually not end in a path separator, but for the root directory of a Dos drive or of the whole Unix filesystem it will, hence the last ":if" above. --~--~---------~--~----~------------~-------~--~----~ You received this message from the "vim_use" maillist. For more information, visit http://www.vim.org/maillist.php -~----------~----~----~----~------~----~------~--~---