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

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

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

lessthanideal
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
Reply | Threaded
Open this post in threaded view
|

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

lessthanideal

On Sep 9, 1:06 pm, lessthanideal <[hidden email]>
wrote:

> I've tried to do it by using BufFilePre and BufFilePro

Sorry for typo, that should read BufFilePre and BufFilePost

--
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
Reply | Threaded
Open this post in threaded view
|

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

Christian Brabandt
On Thu, September 9, 2010 2:10 pm, lessthanideal wrote:
>
> On Sep 9, 1:06 pm, lessthanideal <[hidden email]>
> wrote:
>> I've tried to do it by using BufFilePre and BufFilePro
>
> Sorry for typo, that should read BufFilePre and BufFilePost

If you don't mind changing your cwd, I would simply set autochdir

regards,
christian

--
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
Reply | Threaded
Open this post in threaded view
|

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

scott-268
On Thursday 09 September 2010 08:41:02 Christian Brabandt wrote:

> On Thu, September 9, 2010 2:10 pm, lessthanideal wrote:
> > On Sep 9, 1:06 pm, lessthanideal
> > <[hidden email]>
> >
> > wrote:
> >> I've tried to do it by using BufFilePre and BufFilePro
> >
> > Sorry for typo, that should read BufFilePre and BufFilePost

> If you don't mind changing your cwd, I would simply set
> autochdir

i know netrw used to have a problem with autochdir but charles
fixed that quite a bit ago -- i don't know of any other
plugins that have a problem with that setting

[full disclosure:  i use as few plugins as possible so i don't
have a wide plugin experience]

sc

--
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
Reply | Threaded
Open this post in threaded view
|

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

Gary Johnson-4
In reply to this post by lessthanideal
On 2010-09-09, lessthanideal wrote:

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

It's a little more typing (6 characters), but you could do this:

    :saveas %:p:h/newname

Regards,
Gary

--
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
Reply | Threaded
Open this post in threaded view
|

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

lessthanideal
Thanks for all replies.  The solution I've settled on is to create a
custom
command with %:p:h and map it to a function key so I don't
accidentally type
:sav instead of :Sav.

"SaveasSamePath saves with new name to same path (all one line)
command! -nargs=1 SaveasSamePath exe "saveas " . expand("%:p:h") .
expand("<args>")

"F7 calls SaveasSamePath.  Note space at end of line
map <F7> :SaveasSamePath

I've seen other threads reporting obscure problems with autochdir and
the help
implies (to me) relying on it now or in future is not a good idea
unless you
really need to be compatible with "Sun ONE Studio 4 Enterprise
Edition".
Probably being over-cautious but I just don't like it.

By the way the I found the same problem with BufFilePre and
BufFilePost
using redhat VIM - Vi IMproved 7.0.

regards,
Geoff

--
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
Reply | Threaded
Open this post in threaded view
|

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

Ven Tadipatri
In reply to this post by Gary Johnson-4
On Thu, Sep 9, 2010 at 12:58 PM, Gary Johnson <[hidden email]> wrote:

> On 2010-09-09, lessthanideal wrote:
>> 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?
>
> It's a little more typing (6 characters), but you could do this:
>
>    :saveas %:p:h/newname

I was looking for a way to do this. I think I was always exiting out
of vi, then changing to the directory and reopening the file.
Could you explain what the p and h do? % is the current file I believe.

Thanks,
Ven

>
> Regards,
> Gary
>
> --
> 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
>

--
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
Reply | Threaded
Open this post in threaded view
|

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

Gary Johnson-4
On 2010-09-11, Ven Tadipatri wrote:

> On Thu, Sep 9, 2010 at 12:58 PM, Gary Johnson <[hidden email]> wrote:
> > On 2010-09-09, lessthanideal wrote:
> >> 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?
> >
> > It's a little more typing (6 characters), but you could do this:
> >
> >    :saveas %:p:h/newname
>
> I was looking for a way to do this. I think I was always exiting out
> of vi, then changing to the directory and reopening the file.
> Could you explain what the p and h do? % is the current file I believe.

Sure.  I should have provided a reference the first time.

The :p expands % to the full pathname of the current file; :h
removes the last component of the path, leaving just the parent
directory of the file.  See

    :help filename-modifiers
    :help cmdline-special

Regards,
Gary

--
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
Reply | Threaded
Open this post in threaded view
|

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

lessthanideal
In reply to this post by lessthanideal

On Sep 10, 12:54 pm, lessthanideal <[hidden email]>
wrote:
> Thanks for all replies.  The solution I've settled on is to create a
> custom command with %:p:h and map it to a function key so
> I don't accidentally type :sav instead of :Sav.

Oops, missed out final /.  I throught I'd specifically checked that
as well :(  Anyway for anyone interested it should be

"SaveasSamePath saves with new name to same path (all one line)
 command! -nargs=1 SaveasSamePath exe "saveas " . expand("%:p:h") .
"/" .expand("<args>")

"F7 calls SaveasSamePath.  Note space at end of line
map <F7> :SaveasSamePath

regards,
Geoff

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