Thursday, September 9, 2010

How to make saveas default to original file's directory?

I'm using Windows VIM 7.3.2 (the Vim without Cream build)

:saveas puts the new file into the current working directory if
no path is specified. I want it to default to the directory of
the original file.

Example, from new VIM session
:cd c:\test
:e c:\h.txt " cwd is still c:\test
:sav h2.txt " I want this to come out as c:\h2.txt but
" it comes out as c:\test\h2.txt

I could do this by defining my own :Sav command but I'd like it
to be the default if possible. I considered autochdir but it can
cause plugin problems. I looked on the Vim Wiki.

I can do it manually by changing directory (:cd) first. I've tried
to do it by using BufFilePre and BufFilePro to temporarily
change the current working directory before and after the :saveas
but it doesn't seem to work - it seems the directory changes (from
making the autocommands echo the directory at various points), but
for some reason :saveas doesn't uses the working directory then.
I don't really understand exactly what goes on though, for one thing
each event is triggered twice.

I tried with gvim -u NONE -N to turn off conflicts with plugins etc.

Is this an approach that should work? Can anyone suggest how to do
this, if it's possible?

thanks,
Geoff

Here's my autocommands

"First time event triggers define variable cwd as "x" so we know we
" have gone through first time
"Second time through change variable to current working dir, then
" change current working dir to dir of current file name
au BufFilePre * if (exists("cwd") == 0) | let cwd = "x" | else
| let cwd = getcwd() | cd %:p:h | endif

"If variable cwd exists, change working dir back to the stored value
" then undefine the variable
au BufFilePost * if (exists("cwd") != 0) | echo cwd | :exe ":cd ".cwd
| unlet cwd | endif

--
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