Quantcast

Make :saveas save to path of current file

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Make :saveas save to path of current file

lessthanideal
Suppose the current working directory is "C:\" and I am editing
"C:\temp\test.txt".  I issue the command

:saveas test2.txt

The file is saved as C:\test2.txt, hHow can I make it be saved
as C:\temp\test2.txt?

My current solution is to define my own command (all one line)

command! -nargs=1 SaveasSamePath exe "saveas " . expand("%:p:h") .
"/" . expand("<args>")

I would like it just to work with :saveas though.  I have tried using
the BufFilePre event to move to the right directory first.

au BufFilePre * cd %:p:h | registers | echo getcwd()

This changes the current working directory but the file still ends up
in the old working directory.  I think this is because the full name
"C:\test2.txt" is stored right after the command is issued before
BufFilePre
occurs.  The registers part of my command shows it in the alternate-
file
register.  If I could change that register at that point I guess it
would work,
but it is a read-only register.

(Another problem is that BufFilePre triggers for :saveas and :file
commands, I
don't use :file myself but I don't want to break any plugins that
might use it
so ideally my solution would check, was this event caused by :saveas?
I don't
know how to check that.  I thought of checking register ": (most
recently
executed command) for "sav", but it only gets set once the command is
complete.)

One solution would be to move the file once it has been saved but that
seems
inelegant especially if moving between drives.

I know there are ways of changing the working directory whenever you
switch to
a buffer but I'd prefer not to do that.  If I could get BufFilePre
working, I would
use BufFilePost to move back to the original working directory
afterwards.

Any ideas?

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
|  
Report Content as Inappropriate
star

RE: Make :saveas save to path of current file

John Beckett-2
lessthanideal wrote:
> Suppose the current working directory is "C:\" and I am
> editing "C:\temp\test.txt".  I issue the command
>
> :saveas test2.txt
>
> The file is saved as C:\test2.txt, hHow can I make it be
> saved as C:\temp\test2.txt?

The way :saveas is working is correct because if you do not
specify a directory, Vim's current directory should be used.

What you want is something like this:
    :saveas %:p:h/newname.txt

See :help filename-modifiers

I would just type that command because it's worth remembering
and should not be needed often. It would be possible to make a
user command to simplify it.

John

--
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
|  
Report Content as Inappropriate
star

Re: Make :saveas save to path of current file

Ben Schmidt
On 19/08/11 8:23 AM, John Beckett wrote:

> lessthanideal wrote:
>> Suppose the current working directory is "C:\" and I am
>> editing "C:\temp\test.txt".  I issue the command
>>
>> :saveas test2.txt
>>
>> The file is saved as C:\test2.txt, hHow can I make it be
>> saved as C:\temp\test2.txt?
>
> The way :saveas is working is correct because if you do not
> specify a directory, Vim's current directory should be used.
>
> What you want is something like this:
>      :saveas %:p:h/newname.txt

Alternatively, you could use one of the many tricks to ensure the
current directory matches the directory of the file in the current
buffer. If you search the Vim tips wiki, I'm sure you'll find a few ways
to do that if you're interested.

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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Make :saveas save to path of current file

lessthanideal
On Aug 19, 12:53 am, Ben Schmidt <[hidden email]>
wrote:

> On 19/08/11 8:23 AM, John Beckett wrote:
>
> > lessthanideal wrote:
> >> Suppose the current working directory is "C:\" and I am
> >> editing "C:\temp\test.txt".  I issue the command
>
> >> :saveas test2.txt
>
> >> The file is saved as C:\test2.txt, hHow can I make it be
> >> saved as C:\temp\test2.txt?
>
> > The way :saveas is working is correct because if you do not
> > specify a directory, Vim's current directory should be used.
>
> > What you want is something like this:
> >      :saveas %:p:h/newname.txt
>
> Alternatively, you could use one of the many tricks to ensure the
> current directory matches the directory of the file in the current
> buffer. If you search the Vim tips wiki, I'm sure you'll find a few ways
> to do that if you're interested.
>
> Ben.

Thanks for these answers.  I find this tr

--
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
|  
Report Content as Inappropriate
star

Re: Make :saveas save to path of current file

lessthanideal
In reply to this post by Ben Schmidt
I think Google mail just sent an empty reply from me as well, sorry
for spam there.

On Aug 19, 12:53 am, Ben Schmidt <[hidden email]>
wrote:

> On 19/08/11 8:23 AM, John Beckett wrote:
>
> > What you want is something like this:
> >      :saveas %:p:h/newname.txt
>
> Alternatively, you could use one of the many tricks to ensure the
> current directory matches the directory of the file in the current
> buffer. If you search the Vim tips wiki, I'm sure you'll find a few ways
> to do that if you're interested.
>
> Ben.

Thanks for your answers, although I hoped I'd covered them both in my
original
email to save people suggesting them :)  It's useful to know they're
the
methods people recommend.

A recent unrelated thread showed me what's obviously a more correct
way to
redefine :saveas than my attempts, i.e. remapping :sav to call my
custom
command.  I can't account for not spotting that before (especially
since I
was already mapping F7 to call it ):  Anyway, now I have

"All on one line
command! -nargs=1 SaveasSamePath exe "sav " . expand("%:p:h") . "/" .
expand("<args>")

"Remap so I get my command
nnoremap :sav :SaveasSamePath

"... and so I can still get the original if I need it by typing it in
full
nnoremap :saveas :saveas

Hopefully this won't cause problems even for plugins that assume no
mappings
and issue the command ":sav" themselves.  I haven't been able to make
it
go wrong myself in defined commands (defining more execute commands to
call "sav"
works as expected and calls :saveas).

The original reason I didn't want to change the working directory
whenever I
switch to a buffer was because 'autochdir' seemed to cause problems,
and also,
the help makes it sound (to me) like a very non-standard option.  That
was a
couple of years ago when I looked at this.  (This saveas problem
doesn't trip
me up very often, which is why I can't just remember %:p:h)  I did
more
recently find

http://vim.wikia.com/wiki/Set_working_directory_to_the_current_file

and my impression was the methods of doing this still might cause
problems in
some situations; since I don't particularly want to change directory
apart from
to solve my saveas problem, it seemed better to avoid doing so.  But,
if this
remapping doesn't work out I'll probably look at it again.

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
|  
Report Content as Inappropriate
star

Re: Make :saveas save to path of current file

Ben Fritz


On Aug 22, 10:20 am, lessthanideal <[hidden email]>
wrote:

>
> A recent unrelated thread showed me what's obviously a more correct
> way to
> redefine :saveas than my attempts, i.e. remapping :sav to call my
> custom
> command.  I can't account for not spotting that before (especially
> since I
> was already mapping F7 to call it ):  Anyway, now I have
>
> "All on one line
> command! -nargs=1 SaveasSamePath exe "sav " . expand("%:p:h") . "/" .
> expand("<args>")
>
> "Remap so I get my command
> nnoremap :sav :SaveasSamePath
>
> "... and so I can still get the original if I need it by typing it in
> full
> nnoremap :saveas :saveas
>

I'd suggest a command-line abbreviation rather than a normal-mode
mapping to accomplish this:



> Hopefully this won't cause problems even for plugins that assume no
> mappings
> and issue the command ":sav" themselves.  I haven't been able to make
> it
> go wrong myself in defined commands (defining more execute commands to
> call "sav"
> works as expected and calls :saveas).
>

If your mappings screw up a plugin, that's the plugin's fault. They
ought to be using commands such as :normal! (with the '!') to avoid
user mappings if they assume any behavior at all.

> The original reason I didn't want to change the working directory
> whenever I
> switch to a buffer was because 'autochdir' seemed to cause problems,
> and also,
> the help makes it sound (to me) like a very non-standard option.  That
> was a
> couple of years ago when I looked at this.  (This saveas problem
> doesn't trip
> me up very often, which is why I can't just remember %:p:h)  I did
> more
> recently find
>
> http://vim.wikia.com/wiki/Set_working_directory_to_the_current_file
>
> and my impression was the methods of doing this still might cause
> problems in
> some situations; since I don't particularly want to change directory
> apart from
> to solve my saveas problem, it seemed better to avoid doing so.  But,
> if this
> remapping doesn't work out I'll probably look at it again.
>

For what it's worth, I've been running with either the autocmd or the
'autochdir' option active in my .vimrc for four years now with almost
no trouble whatsoever. There was a bug a few years ago in netrw
concerning the autochdir option, and there was a bug in the --remote
commands where 'autochdir' just didn't work until (I think) 7.3, and
there are (or were) some troubles with sessions (:mksession sessions)
when 'autochdir' was set and there were special plugin buffers around.
Other than that, no problems here.

--
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
|  
Report Content as Inappropriate
star

Re: Make :saveas save to path of current file

lessthanideal
On Aug 22, 6:47 pm, Ben Fritz <[hidden email]> wrote:

> On Aug 22, 10:20 am, lessthanideal <[hidden email]>
> wrote:
> > "All on one line
> > command! -nargs=1 SaveasSamePath exe "sav " . expand("%:p:h") . "/" .
> > expand("<args>")
>
> > "Remap so I get my command
> > nnoremap :sav :SaveasSamePath
>
> > "... and so I can still get the original if I need it by typing it in
> > full
> > nnoremap :saveas :saveas
>
> I'd suggest a command-line abbreviation rather than a normal-mode
> mapping to accomplish this:

" Abbreviation replacing mappings I had before
ca sav SaveasSamePath

> If your mappings screw up a plugin, that's the plugin's fault. They
> ought to be using commands such as :normal! (with the '!') to avoid
> user mappings if they assume any behavior at all.

I didn't know about the abbreviation feature at all, thanks for
pointing
it out.  Yes, the behaviour when typing the command in is much
nicer, no waiting for the 'timeout' period.  Also, I assume (from
":help :normal" not mentioning it) that this will not cause problems
for even badly written plugins.

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