:edit {file} question

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

:edit {file} question

malahal
What is the easiest way to edit a file that is in the same directory as
the current file? E.g. I open a file like this: vim /x/y/z/w/file1.c and
want to now open /x/y/z/w/file2.c? Occasionally want to open files in
the parent directory of current file's directory. It would be nice if
there is a special character like "," which starts from the current
buffer's directory. So ":e ,/file2.c" would work. Or maybe "~~" double
tilda character if "," doesn't work...

Thanks, Malahal.
Reply | Threaded
Open this post in threaded view
|

Re: :edit {file} question

A.J.Mechelynck
[hidden email] wrote:

> What is the easiest way to edit a file that is in the same directory as
> the current file? E.g. I open a file like this: vim /x/y/z/w/file1.c and
> want to now open /x/y/z/w/file2.c? Occasionally want to open files in
> the parent directory of current file's directory. It would be nice if
> there is a special character like "," which starts from the current
> buffer's directory. So ":e ,/file2.c" would work. Or maybe "~~" double
> tilda character if "," doesn't work...
>
> Thanks, Malahal.
>
>

Method 1: One particular file

        :e %:h/filename

Method 2: Change directory for the current window only (assuming other
files, possibly in other directories, are in split windows which are not
to be affected)

        :lcd %:h
        :e file1
        " do some edits
        :w
        :e file2
        " etc.

Method 3: Change the "current directory" for the whole of Vim

        :cd %:h
        :e file1
        " do something
        :w
        :e file2
        " etc.

In all the above examples, you can use ":new" ":tabnew" etc. instead of
":e". In Method 2 the "child" window will (IIUC) inherit the ":lcd"
setting of its "parent", i.e. of the one which was current when you
issued the "new" (etc.) command.

See
        :help :cd
  :help :_%
        :help filename-modifiers
        :help :lcd
        " etc.


HTH,
Tony.
Reply | Threaded
Open this post in threaded view
|

Re: :edit {file} question

John Orr
In reply to this post by malahal
On Thursday 20 July 2006 11:10, [hidden email] wrote:
> What is the easiest way to edit a file that is in the same directory as
> the current file? E.g. I open a file like this: vim /x/y/z/w/file1.c and
> want to now open /x/y/z/w/file2.c? Occasionally want to open files in
> the parent directory of current file's directory. It would be nice if
> there is a special character like "," which starts from the current
> buffer's directory. So ":e ,/file2.c" would work. Or maybe "~~" double
> tilda character if "," doesn't work...

A while back I defined the mapping
cmap , <c-r>=expand('%:h')<cr>/
which kind of does what you suggested - it inserts the path to the current file when you type a comma in the command line.  I've found it very useful.
For what I'm doing at present, it suits me to have it.  But it does mean that when I want a real comma (eg in a search pattern, or a :set command) I have to use Ctrl-v or Ctrl-q first.
I guess it could easily be improved call a function which checks the command line to see if it starts with
:e, :sp, and whatever else is appropriate - and otherwise inserts a regular comma.  I'll let you know if I get around to it.

John
Reply | Threaded
Open this post in threaded view
|

Re: :edit {file} question

Peter Hodge-2
In reply to this post by malahal
Hi Malahal,

You can use ':S' to open a new File Explorer window or ':E' to open file
explorer in the current window.

regards,
Peter



--- [hidden email] wrote:

> What is the easiest way to edit a file that is in the same directory as
> the current file? E.g. I open a file like this: vim /x/y/z/w/file1.c and
> want to now open /x/y/z/w/file2.c? Occasionally want to open files in
> the parent directory of current file's directory. It would be nice if
> there is a special character like "," which starts from the current
> buffer's directory. So ":e ,/file2.c" would work. Or maybe "~~" double
> tilda character if "," doesn't work...
>
> Thanks, Malahal.
>



       

       
               
____________________________________________________
On Yahoo!7
Messenger - Make free PC-to-PC calls to your friends overseas.
http://au.messenger.yahoo.com 

Reply | Threaded
Open this post in threaded view
|

Re: :edit {file} question

panshizhu
In reply to this post by malahal
[hidden email] wrote on 2006.07.20 09:10:43:

> What is the easiest way to edit a file that is in the same directory as
> the current file? E.g. I open a file like this: vim /x/y/z/w/file1.c and
> want to now open /x/y/z/w/file2.c? Occasionally want to open files in
> the parent directory of current file's directory. It would be nice if
> there is a special character like "," which starts from the current
> buffer's directory. So ":e ,/file2.c" would work. Or maybe "~~" double
> tilda character if "," doesn't work...
>
> Thanks, Malahal.

you have many way:

1. open a file explore does this, use :Explore
or:
2. creat a map to change to current directory, then just :e <tab>




--
Sincerely, Pan, Shi Zhu. ext: 2606

Reply | Threaded
Open this post in threaded view
|

Re: :edit {file} question

Matthew Winn
In reply to this post by malahal
On Wed, Jul 19, 2006 at 06:10:43PM -0700, [hidden email] wrote:
> What is the easiest way to edit a file that is in the same directory as
> the current file? E.g. I open a file like this: vim /x/y/z/w/file1.c and
> want to now open /x/y/z/w/file2.c?

I just type :e ^R% to get the current filename, followed by enough ^Ws
to remove the trailing parts I don't want.

--
Matthew Winn ([hidden email])
Reply | Threaded
Open this post in threaded view
|

Re: :edit {file} question

Wim R. Crols
In reply to this post by malahal
[hidden email] wrote:

> What is the easiest way to edit a file that is in the same directory as
> the current file? E.g. I open a file like this: vim /x/y/z/w/file1.c and
> want to now open /x/y/z/w/file2.c? Occasionally want to open files in
> the parent directory of current file's directory. It would be nice if
> there is a special character like "," which starts from the current
> buffer's directory. So ":e ,/file2.c" would work. Or maybe "~~" double
> tilda character if "," doesn't work...
>
> Thanks, Malahal.
>  
Maybe ':set autochdir' would help too?

Wim
Reply | Threaded
Open this post in threaded view
|

Re: :edit {file} question

John Orr
In reply to this post by John Orr
Okay, here's a couple of versions depending upon which version of vim you have.  I don't know if it's universal, but my installation of version 6.3 has a non-functioning getcmdline command, and vim7 adds a useful new command, getcmdtype, hence:

" Get path to current file in command-line using comma
if v:version >= 700
  " Use getcmdtype, new to 700, but disallow on "set" and "let"
  cnoremap , <c-r>=getcmdtype()==':'&&match(getcmdline(),'\v(^\| )(se\|set\|let) ')==-1?expand('%:h').'/':','<cr>
elseif v:version > 603
  " No getcmdtype function, allow on e/sp/vsp commands only, vert sp should also work
  cnoremap , <c-r>=match(getcmdline(),'\v^(vert \|)(e\|v?sp) ')!=-1?expand('%:h').'/':','<cr>
else
  " Seems to be a bug in getcmdline in my vim 6.3 - use the simple method
  cnoremap , <c-r>=expand('%:h')<cr>/
endif

Hope the email program doesn't bugger that up too much, and as always, customise it for your needs.

Cheers,
John

On Thursday 20 July 2006 11:50, John Orr wrote:

> On Thursday 20 July 2006 11:10, [hidden email] wrote:
> > What is the easiest way to edit a file that is in the same directory as
> > the current file? E.g. I open a file like this: vim /x/y/z/w/file1.c and
> > want to now open /x/y/z/w/file2.c? Occasionally want to open files in
> > the parent directory of current file's directory. It would be nice if
> > there is a special character like "," which starts from the current
> > buffer's directory. So ":e ,/file2.c" would work. Or maybe "~~" double
> > tilda character if "," doesn't work...
>
> A while back I defined the mapping
> cmap , <c-r>=expand('%:h')<cr>/
> which kind of does what you suggested - it inserts the path to the current file when you type a comma in the command line.  I've found it very useful.
> For what I'm doing at present, it suits me to have it.  But it does mean that when I want a real comma (eg in a search pattern, or a :set command) I have to use Ctrl-v or Ctrl-q first.
> I guess it could easily be improved call a function which checks the command line to see if it starts with
> :e, :sp, and whatever else is appropriate - and otherwise inserts a regular comma.  I'll let you know if I get around to it.
>
> John
>