Automatically going to another file

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

Automatically going to another file

ovid-4
Hi all,

I've been asked to contribute some information for a book about Perl
and I wanted to include useful vim info for making Perl development
easier.

One issue I'd like to address is automatically jumping to a test file.
I often find myself writing code in pairs of files:  the code and the
test code.  It would be helpful to be able to simply map something like
",gc" ("g"oto "c"omplementing file) and be able to bounce back and
forth between the two.

I thought about writing a vim script which would read the current file
and look for a Perl comment which would identify the complementary
file, but I was wondering if there was a "more correct" way of doing
this.

Cheers,
Ovid

--
If this message is a response to a question on a mailing list, please send
follow up questions to the list.

Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/
Reply | Threaded
Open this post in threaded view
|

Re: Automatically going to another file

A.J.Mechelynck
----- Original Message -----
From: "Ovid" <[hidden email]>
To: <[hidden email]>
Sent: Tuesday, September 13, 2005 2:50 AM
Subject: Automatically going to another file


> Hi all,
>
> I've been asked to contribute some information for a book about Perl
> and I wanted to include useful vim info for making Perl development
> easier.
>
> One issue I'd like to address is automatically jumping to a test file.
> I often find myself writing code in pairs of files:  the code and the
> test code.  It would be helpful to be able to simply map something like
> ",gc" ("g"oto "c"omplementing file) and be able to bounce back and
> forth between the two.
>
> I thought about writing a vim script which would read the current file
> and look for a Perl comment which would identify the complementary
> file, but I was wondering if there was a "more correct" way of doing
> this.
>
> Cheers,
> Ovid
>
> --
> If this message is a response to a question on a mailing list, please send
> follow up questions to the list.
>
> Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/

Method I: Open both files in split windows:
either

    vim -o filename1 filename2

or

    vim
        :e filename1
        :new filename2

Go from one to the other using ^Ww (that's Ctrl-W followed by w). If more
than two windows, ^Ww goes to the next window, ^WW to the previous one, both
in round-robin fashion. With a count, either command goes to the nth window
from top, not to the nth next or previous one.

Method I bis: To have the current file use as much screen real estate as
possible, but also see the statusline for the other file(s) (with [+] if
modified) do the same as above, and in addition

    :set lines=99999 columns=99999 noequalalways winminheight=0
winheight=99999

(All on one line: lines=99999 columns=99999 maximize the Vim window; the
other settings are what I call "Rolodex Vim", which opens the current window
to maximum height and squashes all other split-windows to a status line and
nothing else.)

Method II: Open both files, but see only one at a time, and hide the other,
not even showing a statusline for it.

    vim filename1 filename2

Vim opens with filename1 displayed.
Go from filename1 to filename2 using

    :next

(after saving the file if necessary)
Go back to the previous file using either

    :Next
or
    :prev

If you want to switch files without saving and without losing your edits,
then use

    :hide next
or
    :hide prev

or, set 'hidden' ON before switching files.

In that case, to "write all and quit", use

    :xa
or
    :wqa

(to "write all and continue", use    :wa    )


Whatever method you decide to use, you can map the file-switching commands
to a key, for instance

    :map    <F11>    <C-W>w
    :map    <S-F11>    <C-W>W
    :imap    <F11>    <C-O><C-W>w
    :imap    <S-F11>    <C-O><C-W>W
or
    :map    <F11>    :hide next<CR>
    :map    <S-F11>    :hide prev<CR>
    :imap    <F11>    <C-O>:hide next<CR>
    :imap    <S-F11>    <C-O>:hide prev<CR>


Best regards,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: Automatically going to another file

ovid-4
--- Tony Mechelynck <[hidden email]> wrote:

> Method I: Open both files in split windows:
> either
>
>     vim -o filename1 filename2
>
> or
>
>     vim
>         :e filename1
>         :new filename2
<snip>

Thank you.  There was lots of useful information there.  I'm wondering
if there's some way of having vim *automatically* detect the
"filename2".  For example, I can add this to my Perl code:

  # vim:ts=4:set ai

And vim will happily set up the proper tab spacing and autoindent.  Is
there some way I could automatically set a variable to the name of the
"complementary" file so that a keybinding could automatically jump to
it?

Cheers,
Ovid

--
If this message is a response to a question on a mailing list, please send
follow up questions to the list.

Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/
Reply | Threaded
Open this post in threaded view
|

Re: Automatically going to another file

Gary Johnson
In reply to this post by ovid-4
On 2005-09-12, Ovid <[hidden email]> wrote:

> Hi all,
>
> I've been asked to contribute some information for a book about Perl
> and I wanted to include useful vim info for making Perl development
> easier.
>
> One issue I'd like to address is automatically jumping to a test file.
> I often find myself writing code in pairs of files:  the code and the
> test code.  It would be helpful to be able to simply map something like
> ",gc" ("g"oto "c"omplementing file) and be able to bounce back and
> forth between the two.
>
> I thought about writing a vim script which would read the current file
> and look for a Perl comment which would identify the complementary
> file, but I was wondering if there was a "more correct" way of doing
> this.

In addition to Tony's suggestions, if the two files share a common
basename but have different suffixes, you could adapt this mapping
used to open in a new window the .h or .c C file corresponding to
the current .c or .h file.

    map gc :if expand("%:e")=='c'<CR>sfind %:t:r.h<CR>else<CR>sfind %:t:r.c<CR>endif<CR>

HTH,
Gary

--
Gary Johnson                 | Agilent Technologies
[hidden email]     | Wireless Division
                             | Spokane, Washington, USA
Reply | Threaded
Open this post in threaded view
|

Re: Automatically going to another file

A.J.Mechelynck
In reply to this post by ovid-4
----- Original Message -----
From: "Ovid" <[hidden email]>
To: "Tony Mechelynck" <[hidden email]>; <[hidden email]>
Sent: Tuesday, September 13, 2005 3:34 AM
Subject: Re: Automatically going to another file


> --- Tony Mechelynck <[hidden email]> wrote:
>> Method I: Open both files in split windows:
>> either
>>
>>     vim -o filename1 filename2
>>
>> or
>>
>>     vim
>>         :e filename1
>>         :new filename2
> <snip>
>
> Thank you.  There was lots of useful information there.  I'm wondering
> if there's some way of having vim *automatically* detect the
> "filename2".  For example, I can add this to my Perl code:
>
>  # vim:ts=4:set ai
>
> And vim will happily set up the proper tab spacing and autoindent.  Is
> there some way I could automatically set a variable to the name of the
> "complementary" file so that a keybinding could automatically jump to
> it?
>
> Cheers,
> Ovid

In modelines (such as the one you showed), you set options, not variables.

If there is a fixed relation between both files, you can set up a function
to do it. Let's take an example from C (I don't know Perl): switch between a
.c source file and the corresponding .h header file with the same root
filename.

function! RelatedFile(filename)
    let extension = fnamemodify(a:filename, ":e")
    let rootfile = fnamemodify(a:filename, ":p:r"
    if extension == "c"
        return rootfile . ".h"
    elseif extension == "h"
        return rootfile . ".c"
    else
        return a:filename
    endif
endfunction
map    <F11>    :hide edit RelatedFile(expand("%"))<CR>
imap    <F11>    <C-O>:hide edit RelatedFile(expand("%"))<CR>


If there is something unclear in the above, don't forget the Vim help.


Best regards,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: Automatically going to another file

Dominic Evans
All these replies seem to suggest a lot of work, when it seems much
easier just to set it up manually.

If the other file is in the same directory as the perl source. Then
you can simply include its name in a comment somewhere in the file and
use 'gf' to open and start editing. (if its elsewhere you'll have to
set up your 'path' variable - use vim help)

e.g

#!/usr/bin/perl
#
# tests: test_methods_for_this.pl

chomp;
chomp;


Then you would just place the cursor over the other filename and type gf.

Dom

On 13/09/05, Tony Mechelynck <[hidden email]> wrote:

> ----- Original Message -----
> From: "Ovid" <[hidden email]>
> To: "Tony Mechelynck" <[hidden email]>; <[hidden email]>
> Sent: Tuesday, September 13, 2005 3:34 AM
> Subject: Re: Automatically going to another file
>
>
> > --- Tony Mechelynck <[hidden email]> wrote:
> >> Method I: Open both files in split windows:
> >> either
> >>
> >>     vim -o filename1 filename2
> >>
> >> or
> >>
> >>     vim
> >>         :e filename1
> >>         :new filename2
> > <snip>
> >
> > Thank you.  There was lots of useful information there.  I'm wondering
> > if there's some way of having vim *automatically* detect the
> > "filename2".  For example, I can add this to my Perl code:
> >
> >  # vim:ts=4:set ai
> >
> > And vim will happily set up the proper tab spacing and autoindent.  Is
> > there some way I could automatically set a variable to the name of the
> > "complementary" file so that a keybinding could automatically jump to
> > it?
> >
> > Cheers,
> > Ovid
>
> In modelines (such as the one you showed), you set options, not variables.
>
> If there is a fixed relation between both files, you can set up a function
> to do it. Let's take an example from C (I don't know Perl): switch between a
> .c source file and the corresponding .h header file with the same root
> filename.
>
> function! RelatedFile(filename)
>     let extension = fnamemodify(a:filename, ":e")
>     let rootfile = fnamemodify(a:filename, ":p:r"
>     if extension == "c"
>         return rootfile . ".h"
>     elseif extension == "h"
>         return rootfile . ".c"
>     else
>         return a:filename
>     endif
> endfunction
> map    <F11>    :hide edit RelatedFile(expand("%"))<CR>
> imap    <F11>    <C-O>:hide edit RelatedFile(expand("%"))<CR>
>
>
> If there is something unclear in the above, don't forget the Vim help.
>
>
> Best regards,
> Tony.
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Automatically going to another file

Tim Chase-2
> # tests: test_methods_for_this.pl
>
> Then you would just place the cursor over the other filename and type gf.

In addition to the other suggestions here, you can use ^Wf
(ctrl+w followed by "f") to do the same thing as the above "gf",
only the new file will open in a new window.

On top of that, you can use ^^ (ctrl+^) to toggle between your
"current" file and your "alternate" file, so once you've got your
two files set up (likely with "gf"), you can use the ^^ to jump
back and forth quickly.

One other thing that occurs to me is that if you've made edits on
the file and want to jump to another file in the same window,
you'll either need to write the file, or set the 'hidden' option
so that buffers can be non-visible yet have modifications.

Just a few more ideas.  Hope they help,

-tim







Reply | Threaded
Open this post in threaded view
|

RE: Automatically going to another file

Keith W. Roberts
 

> -----Original Message-----
> From: Tim Chase [mailto:[hidden email]]
> Sent: Tuesday, September 13, 2005 5:39 AM
> To: [hidden email]
> Cc: [hidden email]
> Subject: Re: Automatically going to another file
>
> > # tests: test_methods_for_this.pl
> >
> > Then you would just place the cursor over the other
> filename and type gf.
>
> In addition to the other suggestions here, you can use ^Wf
> (ctrl+w followed by "f") to do the same thing as the above "gf",
> only the new file will open in a new window.
>
> On top of that, you can use ^^ (ctrl+^) to toggle between your
> "current" file and your "alternate" file, so once you've got your
> two files set up (likely with "gf"), you can use the ^^ to jump
> back and forth quickly.
>
> One other thing that occurs to me is that if you've made edits on
> the file and want to jump to another file in the same window,
> you'll either need to write the file, or set the 'hidden' option
> so that buffers can be non-visible yet have modifications.
>
> Just a few more ideas.  Hope they help,
>
> -tim

One last note for the OP:

Tony mentioned ^Ww and ^WW to move forward or backward between windows.  I
prefer ^Wp, which moves to the "previous" window (ie, the last one you came
from).  This works whether you split up|down (horizontally), right|left
(vertically), top|bottom (at the edge of the gui) -- or you just go to some
non-adjacent window on the screen [which would likely be the case if you
used :top or :bottom split, or if you used :help after creating an adjacent
split with no Help window currently onscreen].

-Keith