Movatterモバイル変換


[0]ホーム

URL:


Quick links:help overview ·quick reference ·user manual toc ·reference manual toc·faq
Go to keyword (shortcut:k)
Site search (shortcut:s)
usr_07.txt  ForVim version 9.2.  Last change: 2026 Feb 14     VIM USER MANUALbyBramMoolenaar  Editing more than one fileNo matter how many files you have, you can edit them without leaving Vim.Definealist of files to work on and jump from one to the other.  Copy textfrom one file andputit in another one.07.1  Edit another file07.2Alist of files07.3  Jumping from file to file07.4  Backup files07.5  Copy text between files07.6  Viewinga file07.7  Changing the file name     Next chapter:usr_08.txt  Splittingwindows Previous chapter:usr_06.txt  Usingsyntax highlightingTable of contents:usr_toc.txt==============================================================================07.1  Edit another fileSo far you had to start Vim for every file you wanted to edit.  Thereisasimpler way.  To start editing another file, use this command::edit foo.txtYou can use any file name instead of "foo.txt".  Vim will close the currentfile and open the new one.  If the current file has unsaved changes, however,Vim displays an error message and does not open the new file:E37: No write since last change (use ! to override)Note:Vim puts an error IDat the start of each error message.  If youdonot understand the message or what caused it, look in thehelp systemfor this ID.  In this case::help E37At this point, you havea number of alternatives.  You can write the fileusing this command::writeOr you can force Vim todiscard your changes and edit the new file, using theforce (!) character::edit! foo.txtIf you want to edit another file, but not write the changes in the currentfile yet, you can makeit hidden::hide edit foo.txtThe text with changesis still there, but you can't see it.  Thisis furtherexplained insection22.4: The buffer list.==============================================================================07.2Alist of filesYou can start Vim to edita sequence of files.  For example:vim one.c two.c three.cThis command starts Vim and tellsit that you will be editing three files.Vim displays just the first file.  After you have done your thing in thisfile, to edit the next file you use this command::nextIf you have unsaved changes in the current file, you will get an errormessage and the ":next" will not work.  Thisis the same problemas with":edit" mentioned in the previous section.  Toabandon the changes::next!But mostly you want to save the changes and move on to the next file.  Thereisa special command for this::wnextThis does the sameas using two separate commands::write:nextWHERE AM I?To see which file in the argumentlist you are editing, look in thewindowtitle.  It should show something like "(2 of 3)".  This means you are editingthe second file out of three files.   If you want to see thelist of files, use this command::argsThisis short for "arguments".  The output might look like this:one.c [two.c] three.cThese are the files you started Vim with.  The one you are currently editing,"two.c",is in square brackets.MOVING TO OTHER ARGUMENTSTogo back one file::previousThisis just like the ":next" command, except thatit moves in the otherdirection.  Again, thereisa shortcut command for when you want to write thefile first::wpreviousTo move to the very last file in the list::lastAnd to move back to the first one again::firstThereis no ":wlast" or ":wfirst" command though!You can useacount for ":next" and ":previous".  To skip two files forward::2nextAUTOMATIC WRITINGWhen moving around the files and making changes, you have to remember to use":write".  Otherwise you will get an error message.  If you are sure youalways want to write modified files, you can tell Vim to automatically writethem::set autowriteWhen you are editinga file which you may not want to write, switchit offagain::set noautowriteEDITING ANOTHER LIST OF FILESYou can redefine thelist of files without the need to exit Vim and startitagain.  Use this command to edit three other files::args five.c six.c seven.hOr usea wildcard, like it's used in the shell::args *.txtVim will take you to the first file in the list.  Again, if the current filehas changes, you can either write the file first, or use ":args!" (with!added) toabandon the changes.DID YOU EDIT THE LAST FILE?arglist-quitWhen you usealist of files, Vim assumes you want to edit them all.  Toprotect you fromexiting too early, you will get this error when you didn'tedit the last file in thelist yet:E173: 46 more files to editIf you really want to exit, justdoit again.  Thenit will work (but not whenyou did other commands in between).==============================================================================07.3  Jumping from file to fileTo quickly jump between two files, pressCTRL-^ (on English-US keyboards the^is above the 6 key).  Example::args one.c two.c three.cYou are now in one.c.:nextNow you are in two.c.  Now useCTRL-^ togo back to one.c.  AnotherCTRL-^ andyou are back in two.c.  AnotherCTRL-^ and you are in one.c again.  If you nowdo::nextYou are in three.c.  Notice that theCTRL-^ command does not change the ideaof where you are in thelist of files.  Only commands like ":next" and":previous"do that.The file you were previously editingis called the "alternate" file.  When youjust started VimCTRL-^ will not work, since there isn'ta previous file.PREDEFINED MARKSAfter jumping to another file, you can use two predefined marks which are veryuseful:`"This takes you to the position where the cursor was when you left the file.Anothermark thatis rememberedis the position where you made the lastchange:`.Suppose you are editing the file "one.txt".  Somewhere halfway through thefile you use "x" to deletea character.  Then yougo to the last line with "G"and write the file with ":w".  You edit several other files, and then use":edit one.txt" to come back to "one.txt".  If you now use`" Vim jumps to thelast line of the file.  Using`. takes you to the position where you deletedthe character.  Even when you move around in the file`" and`. will take youto the remembered position.  At least until you make another change or leavethe file.FILE MARKSInsection03.10 was explained how you can placeamark ina file with "mx"and jump to that position with "`x".  That works within one file.  If you editanother file and place marks there, these are specific for that file.  Thuseach file has its own set of marks, they are local to the file.   So far we were using marks withalowercase letter.  There are also markswith anuppercase letter.  These are global, they can be used from any file.For example suppose that we are editing the file "foo.txt".  Go to halfwaydown the file("50%") and place theFmark there (F for foo):50%mFNow edit the file "bar.txt" and place theBmark (B for bar)at its last line:GmBNow you can use the "'F" command to jump back to halfway foo.txt.  Or edit yetanother file, type "'B" and you areat theend of bar.txt again.The file marks are remembered until they are placed somewhere else.  Thus youcan place the mark,do hours of editing and still be able to jump back to thatmark.   It's often useful to think ofa simple connection between themarkletterand whereitis placed.  For example, use theHmark ina header file,M ina Makefile andC inaC code file.To see wherea specificmark is, give an argument to the ":marks" command::marks MYou can also give several arguments::marks MCPDon't forget that you can useCTRL-O andCTRL-I to jump to older and newerpositions without placing marks there.==============================================================================07.4  Backup filesUsually Vim does not produceabackup file.  If you want to have one, all youneed todois execute the following command::set backupThe name of thebackup fileis the original file witha~  added to the end.If your fileis named data.txt, for example, thebackup file nameisdata.txt~.   If youdo not like the fact that thebackup filesend with ~, you canchange the extension::set backupext=.bakThis will use data.txt.bak instead of data.txt~.   Another option that matters hereis'backupdir'.  Itspecifies where thebackup fileis written.  The default, to write thebackup in the samedirectoryas the original file, will mostly be the right thing.Note:When the'backup' option isn't set but the'writebackup' is, Vim willstill createabackup file.  However,itis deletedas soonaswritingthe file was completed successfully.  Thisfunctionsasa safetyagainst losing your original file whenwriting fails in some way (diskfullis the most common cause; being hit by lightning might beanother, althoughless common).KEEPING THE ORIGINAL FILEIf you are editing source files, you might want to keep the file before youmake any changes.  But thebackup file will be overwritten each time you writethe file.  Thusit only contains the previous version, not the first one.   To make Vim keep the original file, set the'patchmode' option.  Thisspecifies the extension used for the firstbackup ofa changed file.  Usuallyyou woulddo this::set patchmode=.origWhen you now edit the file data.txt for the first time, make changes and writethe file, Vim will keepa copy of the unchanged file under the name"data.txt.orig".   If you make further changes to the file, Vim will notice that"data.txt.orig" already exists and leaveit alone.  Furtherbackup files willthen be called "data.txt~" (or whatever you specified with'backupext').   If you leave'patchmode' empty (thatis the default), the original filewill not be kept.==============================================================================07.5  Copy text between filesThis explains how to copy text from one file to another.  Let's start withasimple example.  Edit the file that contains the text you want to copy.  Movethe cursor to the start of the text and press "v".  This startsVisual mode.Now move the cursor to theend of the text and press "y".  This yanks (copies)the selected text.   To copy the above paragraph, you would do::edit thisfile/Thisvjjjj$yNow edit the file you want toput the text in.  Move the cursor to thecharacter where you want the text to appear after.  Use "p" toput the textthere.:edit otherfile/TherepOf course you can use many other commands toyank the text.  For example, toselect whole lines startVisual mode with "V".  Or useCTRL-V to selectarectangular block.  Or use "Y" toyanka single line, "yaw" to yank-a-word,etc.   The "p" command puts the text after the cursor.  Use "P" toput the textbefore the cursor.  Notice that Vim remembers if you yankeda whole line orablock, and putsit back that way.USING REGISTERSWhen you want to copy several pieces of text from one file to another, havingto switch between the files andwriting the target file takesa lot of time.To avoid this, copy each piece of text to its own register.A registerisa place where Vim stores text.  Here we will use theregisters nameda toz (later you will find out there are others).  Let's copyasentence to thef register (f for First):"fyasThe "yas" command yanksasentence like before.  It's the "f that tells Vimthe text should be placed in thef register.  Thismust come just before theyank command.   Nowyank three whole lines to thel register (l for line):"l3YThecount could be before the "l justas well.  Toyanka block of text to theb (for block) register:CTRL-Vjjww"byNotice that the register specification "bis just before the "y" command.Thisis required.  If you would haveputit before the "w" command,it wouldnot have worked.   Now you have three pieces of text in the f,l andb registers.  Editanother file, move around and place the text where you want it:"fpAgain, the register specification "f comes before the "p" command.   You canput theregisters in any order.  And the text stays in the registeruntil youyank something else into it.  Thus you canputitas many timesasyou like.When you delete text, you can also specifya register.  Use this to moveseveral pieces of text around.  For example, to delete-a-word and writeit inthew register:"wdawAgain, the register specification comes before the delete command "d".APPENDING TO A FILEWhen collecting lines of text into one file, you can use this command::write >> logfileThis will write the text of the current file to theend of "logfile".  Thusitis appended.  This avoids that you have to copy the lines, edit the log fileandput them there.  Thus you save two steps.  But you can only append to theend ofa file.   To append onlya few lines, select them inVisual mode before typing":write".  In chapter 10 you will learn other ways to selecta range of lines.==============================================================================07.6  Viewinga fileSometimes you only want to see whata file contains, without the intention toever writeit back.  Thereis the risk that you type ":w" without thinking andoverwrite the original file anyway.  To avoid this, edit the file read-only.   To start Vim in readonly mode, use this command:vim -R fileOnUnix this command shoulddo the same thing:view fileYou are now editing "file" in read-only mode.  When you try using ":w" youwill get an error message and the file won't be written.   When you try to makea change to the file Vim will give youa warning:W10: Warning: Changing a readonly fileThe change will be done though.  This allows forformatting the file, forexample, to be able to readit easily.   If you make changes toa file and forgot thatit was read-only, you canstill write it.  Add the! to the write command to force writing.If you really want to forbid making changes ina file,do this:vim -M fileNow every attempt to change the text will fail.  Thehelp files are like this,for example.  If you try to makea change you get this error message:E21: Cannot make changes, 'modifiable' is offYou could use the-M argument to setup Vim to work ina viewer mode.  Thisisonly voluntary though, since these commands will remove the protection::set modifiable:set write==============================================================================07.7  Changing the file nameA clever way to start editinga new fileis by using an existing file thatcontains most of what you need.  For example, you startwritinga new programto movea file.  You know that you already havea program that copiesa file,thus you start with::edit copy.cYou can delete the stuff you don't need.  Now you need to save the file undera new name.  The ":saveas" command can be used for this::saveas move.cVim will write the file under the given name, and edit that file.  Thus thenext time youdo ":write",it will write "move.c".  "copy.c" remainsunmodified.   When you want to change the name of the file you are editing, but don'twant to write the file, you can use this command::file move.cVim willmark the fileas "not edited".  This means that Vim knows thisis notthe file you started editing.  When you try to write the file, you might getthis message:E13: File exists (use ! to override)This protects you from accidentally overwriting another file.==============================================================================Next chapter:usr_08.txt  SplittingwindowsCopyright: seemanual-copyright  vim:tw=78:ts=8:noet:ft=help:norl:

Quick links:help overview ·quick reference ·user manual toc ·reference manual toc·faq


[8]ページ先頭

©2009-2026 Movatter.jp