You are not logged in. Login Now
 0-24   25-49   50-59        
 
Author Message
lyun
How to edit another file without save the current file in vi? Mark Unseen   Feb 23 09:57 UTC 2006

when editing a file using vi editer, i wanna open another file without 
saving the current one. How can i do it? please tell me.
59 responses total.
tod
response 1 of 59: Mark Unseen   Feb 23 11:51 UTC 2006

ctrl-z
vi
lyun
response 2 of 59: Mark Unseen   Feb 23 11:54 UTC 2006

i mean is there any command that can open another file while editing a 
file
kingjon
response 3 of 59: Mark Unseen   Feb 23 11:57 UTC 2006

:e

Of course, that will discard your changes to the original.

lyun
response 4 of 59: Mark Unseen   Feb 23 12:02 UTC 2006

i also donot wanna discard my changes, i will be back to this file 
soon and resume my edit of this file
kingjon
response 5 of 59: Mark Unseen   Feb 23 12:05 UTC 2006

I don't think it's possible, then. In vim you can open a new window (splitting
your screen), but if you change files you have to either save or discard
changes.

lyun
response 6 of 59: Mark Unseen   Feb 23 12:15 UTC 2006

think of this case, you have numerous C source files, and you have 
perform the following command to them: ctags *.c

then you place your cursor on a variable, press ^] to jump to the 
definition(in another file) of it, vi does it, but not closing the 
foregoing file 
remmers
response 7 of 59: Mark Unseen   Feb 23 13:53 UTC 2006

My thought is that vi is not the best editor for doing what you want to
do.  Suggestion:  Use emacs, which has an excellent ctags interface and
allows you to jump back and forth between open files without saving. 
Emacs also has a very good vi emulation mode if you're more comfortable
with vi's editing commands.

"Always use the right tool for the job."  -Mr. Natural
jep
response 8 of 59: Mark Unseen   Feb 23 14:04 UTC 2006

In vi, you can specify two files on the command line with a command 
like:

   vi file1 file2

This command will load file1.  If you type
   :n

it will load file2

At this point, you can switch between the two files with the command:

   CTRL-^

(that is, your CONTROL key and the caret or shift-6 character)

If you run the command:

   vi file*

and then use :n to edit the various files, the most recent two which 
were accessed will be accessible using CTRL-^.

You can also load a 2nd file with the command:

   :e

and then move between your original file and the 2nd file using CTRL-^.
fudge
response 9 of 59: Mark Unseen   Feb 23 14:04 UTC 2006

you just have to study it for 3 years before you learn how to cut, paste and
save...
remmers
response 10 of 59: Mark Unseen   Feb 23 14:20 UTC 2006

Re #8:  That method also appears to make you save the file before
editing the next one, which is what lyun said he doesn't want to do.
fudge
response 11 of 59: Mark Unseen   Feb 23 14:22 UTC 2006

re#9 [emacs that is]
lyun
response 12 of 59: Mark Unseen   Feb 23 14:45 UTC 2006

Re #8:  Will using command :e to open another file close my current 
file?
remmers
response 13 of 59: Mark Unseen   Feb 23 14:48 UTC 2006

I actually find it easier to remember how to cut, paste, and save in
emacs than in vi.  X Window versions of emacs will actually let you do
all that with the mouse in the standard ways.
jep
response 14 of 59: Mark Unseen   Feb 23 15:05 UTC 2006

re resp:12: Yes it does.  Sorry I didn't realize it before.  If you 
want to save the original version of a file, you'd have to back it up 
before editing it.  (Which is not a bad idea anyway if you ask me.)
lyun
response 15 of 59: Mark Unseen   Feb 23 15:11 UTC 2006

how can i open another file without closing the current file that may 
be saved indeed?
jep
response 16 of 59: Mark Unseen   Feb 23 15:22 UTC 2006

Can you repeat that?  I didn't follow what you were asking about.
lyun
response 17 of 59: Mark Unseen   Feb 23 15:30 UTC 2006

yep, now that is : i just wanna open another file when editing one, 
which may be saved but not closed.
jep
response 18 of 59: Mark Unseen   Feb 23 15:41 UTC 2006

When you open a 2nd file with:

   :e file2

The name of the previous file you were editing ("file1") is saved in a 
buffer.  When you use CTRL-^, that file name is re-loaded from disk.  
The contents are not saved in a buffer or in memory, just the name of 
the previous file.

I don't know of a way to keep the contents of the first file in memory, 
if that's what you mean. Why would you want to do that?
lyun
response 19 of 59: Mark Unseen   Feb 23 15:53 UTC 2006

Thank you, jep

i.e.: when i use CTRL-^, then the current file name was kept into a 
buffer, and the contents were saved to disk?

i gotta shutdown my computer now, i will come here tens of hours 
later, thanks again
jep
response 20 of 59: Mark Unseen   Feb 23 16:20 UTC 2006

No, CTRL-^ will not save the current file for you.  It will inform you 
the file hasn't been saved.  You have to save it yourself.  Once you do 
so, then you can use CTRL-^ to jump to the last file you had edited 
during the current session.
mcnally
response 21 of 59: Mark Unseen   Feb 23 17:39 UTC 2006

 I have two suggestions for lijun --

   1)  Use the "screen" command to manage your terminal session..
       You can then open another virtual terminal using a hotkey
       and start a separate vi session within that terminal.
       Switching back and forth between them uses another hotkey
       combination.  Screen is also highly useful in other ways.

   2)  Learn to use one of the multi-file-capable supersets of the
       vi editor.  remmers (jokingly?) suggests using one of the
       emacs vi modes (in typical emacs fashion there are even
       several competing versions to choose from) but I think
       that's overkill..  I'd strongly recommend using vim.  vim
       can edit multiple files at one time, saved or unsaved,
       and provides other nice functions, too (such as multiple-level
       undo..)


mcnally
response 22 of 59: Mark Unseen   Feb 23 18:56 UTC 2006

 Spelling it out a little more clearly, here's how lijun can try
 it out using vim.

 Start by editing a file, for example foo.c:

    vim foo.c

 Now make a few changes to the file (just to prove we can have
 more than one modified file open at a time, just as you asked..)

 Now use the vim windowing commands to create a new window:

    ^Wn  (that's control-W, followed by n.)

 Your cursor will be moved to the new window.  Open a second file
 for editing in that window, e.g.:

     :e bar.c

 Now you'll have foo.c open for editing in the bottom half of the
 screen and bar.c open for editing in the top half.  You can switch
 between the two using ^W^W and when you're done you can save
 either or neither.
naftee
response 23 of 59: Mark Unseen   Feb 23 21:54 UTC 2006

can anyone give me a good reason for using vi on a normal system ?
twenex
response 24 of 59: Mark Unseen   Feb 23 21:58 UTC 2006

OK, I'll bite.

Because once you've learnt it's quick to use and doesn't get in the way. (Who
REALLY needs all those instructions at the bottom of a nano window once
they've used it for the billionth time?)
 0-24   25-49   50-59        
Response Not Possible: You are Not Logged In
 

- Backtalk version 1.3.30 - Copyright 1996-2006, Jan Wolter and Steve Weiss