neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

usr_07.txt (15953B)


      1 *usr_07.txt*	Nvim
      2 
      3 
      4 	     VIM USER MANUAL	by Bram Moolenaar
      5 
      6 
      7 		  Editing more than one file
      8 
      9 
     10 No matter how many files you have, you can edit them without leaving Vim.
     11 Define a list of files to work on and jump from one to the other.  Copy text
     12 from one file and put it in another one.
     13 
     14 |07.1|	Edit another file
     15 |07.2|	A list of files
     16 |07.3|	Jumping from file to file
     17 |07.4|	Backup files
     18 |07.5|	Copy text between files
     19 |07.6|	Viewing a file
     20 |07.7|	Changing the file name
     21 
     22     Next chapter: |usr_08.txt|  Splitting windows
     23 Previous chapter: |usr_06.txt|  Using syntax highlighting
     24 Table of contents: |usr_toc.txt|
     25 
     26 ==============================================================================
     27 *07.1*	Edit another file
     28 
     29 So far you had to start Vim for every file you wanted to edit.  There is a
     30 simpler way.  To start editing another file, use this command: >
     31 
     32 :edit foo.txt
     33 
     34 You can use any file name instead of "foo.txt".  Vim will close the current
     35 file and open the new one.  If the current file has unsaved changes, however,
     36 Vim displays an error message and does not open the new file:
     37 
     38 E37: No write since last change (use ! to override) ~
     39 
     40 Note:
     41 Vim puts an error ID at the start of each error message.  If you do
     42 not understand the message or what caused it, look in the help system
     43 for this ID.  In this case: >
     44 
     45 	:help E37
     46 
     47 At this point, you have a number of alternatives.  You can write the file
     48 using this command: >
     49 
     50 :write
     51 
     52 Or you can force Vim to discard your changes and edit the new file, using the
     53 force (!) character: >
     54 
     55 :edit! foo.txt
     56 
     57 If you want to edit another file, but not write the changes in the current
     58 file yet, you can make it hidden: >
     59 
     60 :hide edit foo.txt
     61 
     62 The text with changes is still there, but you can't see it.  This is further
     63 explained in section |22.4|: The buffer list.
     64 
     65 ==============================================================================
     66 *07.2*	A list of files
     67 
     68 You can start Vim to edit a sequence of files.  For example: >
     69 
     70 vim one.c two.c three.c
     71 
     72 This command starts Vim and tells it that you will be editing three files.
     73 Vim displays just the first file.  After you have done your thing in this
     74 file, to edit the next file you use this command: >
     75 
     76 :next
     77 
     78 If you have unsaved changes in the current file, you will get an error
     79 message and the ":next" will not work.  This is the same problem as with
     80 ":edit" mentioned in the previous section.  To abandon the changes: >
     81 
     82 :next!
     83 
     84 But mostly you want to save the changes and move on to the next file.  There
     85 is a special command for this: >
     86 
     87 :wnext
     88 
     89 This does the same as using two separate commands: >
     90 
     91 :write
     92 :next
     93 
     94 
     95 WHERE AM I?
     96 
     97 To see which file in the argument list you are editing, look in the window
     98 title.  It should show something like "(2 of 3)".  This means you are editing
     99 the second file out of three files.
    100   If you want to see the list of files, use this command: >
    101 
    102 :args
    103 
    104 This is short for "arguments".  The output might look like this:
    105 
    106 one.c [two.c] three.c ~
    107 
    108 These are the files you started Vim with.  The one you are currently editing,
    109 "two.c", is in square brackets.
    110 
    111 
    112 MOVING TO OTHER ARGUMENTS
    113 
    114 To go back one file: >
    115 
    116 :previous
    117 
    118 This is just like the ":next" command, except that it moves in the other
    119 direction.  Again, there is a shortcut command for when you want to write the
    120 file first: >
    121 
    122 :wprevious
    123 
    124 To move to the very last file in the list: >
    125 
    126 :last
    127 
    128 And to move back to the first one again: >
    129 
    130 :first
    131 
    132 There is no ":wlast" or ":wfirst" command though!
    133 
    134 You can use a count for ":next" and ":previous".  To skip two files forward: >
    135 
    136 :2next
    137 
    138 
    139 AUTOMATIC WRITING
    140 
    141 When moving around the files and making changes, you have to remember to use
    142 ":write".  Otherwise you will get an error message.  If you are sure you
    143 always want to write modified files, you can tell Vim to automatically write
    144 them: >
    145 
    146 :set autowrite
    147 
    148 When you are editing a file which you may not want to write, switch it off
    149 again: >
    150 
    151 :set noautowrite
    152 
    153 
    154 EDITING ANOTHER LIST OF FILES
    155 
    156 You can redefine the list of files without the need to exit Vim and start it
    157 again.  Use this command to edit three other files: >
    158 
    159 :args five.c six.c seven.h
    160 
    161 Or use a wildcard, like it's used in the shell: >
    162 
    163 :args *.txt
    164 
    165 Vim will take you to the first file in the list.  Again, if the current file
    166 has changes, you can either write the file first, or use ":args!" (with !
    167 added) to abandon the changes.
    168 
    169 
    170 DID YOU EDIT THE LAST FILE?
    171 						*arglist-quit*
    172 When you use a list of files, Vim assumes you want to edit them all.  To
    173 protect you from exiting too early, you will get this error when you didn't
    174 edit the last file in the list yet:
    175 
    176 E173: 46 more files to edit ~
    177 
    178 If you really want to exit, just do it again.  Then it will work (but not when
    179 you did other commands in between).
    180 
    181 ==============================================================================
    182 *07.3*	Jumping from file to file
    183 
    184 To quickly jump between two files, press CTRL-^ (on English-US keyboards the ^
    185 is above the 6 key).  Example: >
    186 
    187 :args one.c two.c three.c
    188 
    189 You are now in one.c. >
    190 
    191 :next
    192 
    193 Now you are in two.c.  Now use CTRL-^ to go back to one.c.  Another CTRL-^ and
    194 you are back in two.c.  Another CTRL-^ and you are in one.c again.  If you now
    195 do: >
    196 
    197 :next
    198 
    199 You are in three.c.  Notice that the CTRL-^ command does not change the idea
    200 of where you are in the list of files.  Only commands like ":next" and
    201 ":previous" do that.
    202 
    203 The file you were previously editing is called the "alternate" file.  When you
    204 just started Vim CTRL-^ will not work, since there isn't a previous file.
    205 
    206 
    207 PREDEFINED MARKS
    208 
    209 After jumping to another file, you can use two predefined marks which are very
    210 useful: >
    211 
    212 `"
    213 
    214 This takes you to the position where the cursor was when you left the file.
    215 Another mark that is remembered is the position where you made the last
    216 change: >
    217 
    218 `.
    219 
    220 Suppose you are editing the file "one.txt".  Somewhere halfway through the
    221 file you use "x" to delete a character.  Then you go to the last line with "G"
    222 and write the file with ":w".  You edit several other files, and then use
    223 ":edit one.txt" to come back to "one.txt".  If you now use `" Vim jumps to the
    224 last line of the file.  Using `. takes you to the position where you deleted
    225 the character.  Even when you move around in the file `" and `. will take you
    226 to the remembered position.  At least until you make another change or leave
    227 the file.
    228 
    229 
    230 FILE MARKS
    231 
    232 In section |03.10| was explained how you can place a mark in a file with "mx"
    233 and jump to that position with "`x".  That works within one file.  If you edit
    234 another file and place marks there, these are specific for that file.  Thus
    235 each file has its own set of marks, they are local to the file.
    236   So far we were using marks with a lowercase letter.  There are also marks
    237 with an uppercase letter.  These are global, they can be used from any file.
    238 For example suppose that we are editing the file "foo.txt".  Go to halfway
    239 down the file ("50%") and place the F mark there (F for foo): >
    240 
    241 50%mF
    242 
    243 Now edit the file "bar.txt" and place the B mark (B for bar) at its last line:
    244 >
    245 GmB
    246 
    247 Now you can use the "'F" command to jump back to halfway of foo.txt.  Or edit
    248 yet another file, type "'B" and you jump to the end of bar.txt.
    249 
    250 The file marks are remembered until they are placed somewhere else.  Thus you
    251 can place the mark, do hours of editing and still be able to jump back to that
    252 mark.
    253   It's often useful to think of a simple connection between the mark letter
    254 and where it is placed.  For example, use the H mark in a header file, M in
    255 a Makefile and C in a C code file.
    256 
    257 To see where a specific mark is, give an argument to the ":marks" command: >
    258 
    259 :marks M
    260 
    261 You can also give several arguments: >
    262 
    263 :marks MCP
    264 
    265 Don't forget that you can use CTRL-O and CTRL-I to jump to older and newer
    266 positions without placing marks there.
    267 
    268 ==============================================================================
    269 *07.4*	Backup files
    270 
    271 Usually Vim does not produce a backup file.  If you want to have one, all you
    272 need to do is execute the following command: >
    273 
    274 :set backup
    275 
    276 The name of the backup file is the original file with a  ~  added to the end.
    277 If your file is named data.txt, for example, the backup file name is
    278 data.txt~.
    279   If you do not like the fact that the backup files end with ~, you can
    280 change the extension: >
    281 
    282 :set backupext=.bak
    283 
    284 This will use data.txt.bak instead of data.txt~.
    285   Another option that matters here is 'backupdir'.  It specifies where the
    286 backup file is written.  The default, to write the backup in the same
    287 directory as the original file, will mostly be the right thing.
    288 
    289 Note:
    290 When the 'backup' option isn't set but the 'writebackup' is, Vim will
    291 still create a backup file.  However, it is deleted as soon as writing
    292 the file was completed successfully.  This functions as a safety
    293 against losing your original file when writing fails in some way (disk
    294 full is the most common cause; being hit by lightning might be
    295 another, although less common).
    296 
    297 
    298 KEEPING THE ORIGINAL FILE
    299 
    300 If you are editing source files, you might want to keep the file before you
    301 make any changes.  But the backup file will be overwritten each time you write
    302 the file.  Thus it only contains the previous version, not the first one.
    303   To make Vim keep the original file, set the 'patchmode' option.  This
    304 specifies the extension used for the first backup of a changed file.  Usually
    305 you would do this: >
    306 
    307 :set patchmode=.orig
    308 
    309 When you now edit the file data.txt for the first time, make changes and write
    310 the file, Vim will keep a copy of the unchanged file under the name
    311 "data.txt.orig".
    312   If you make further changes to the file, Vim will notice that
    313 "data.txt.orig" already exists and leave it alone.  Further backup files will
    314 then be called "data.txt~" (or whatever you specified with 'backupext').
    315   If you leave 'patchmode' empty (that is the default), the original file
    316 will not be kept.
    317 
    318 ==============================================================================
    319 *07.5*	Copy text between files
    320 
    321 This explains how to copy text from one file to another.  Let's start with a
    322 simple example.  Edit the file that contains the text you want to copy.  Move
    323 the cursor to the start of the text and press "v".  This starts Visual mode.
    324 Now move the cursor to the end of the text and press "y".  This yanks (copies)
    325 the selected text.
    326   To copy the above paragraph, you would do: >
    327 
    328 :edit thisfile
    329 /This
    330 vjjjj$y
    331 
    332 Now edit the file you want to put the text in.  Move the cursor to the
    333 character where you want the text to appear after.  Use "p" to put the text
    334 there. >
    335 :edit otherfile
    336 /There
    337 p
    338 
    339 Of course you can use many other commands to yank the text.  For example, to
    340 select whole lines start Visual mode with "V".  Or use CTRL-V to select a
    341 rectangular block.  Or use "yy" to yank a single line, "yaw" to yank-a-word,
    342 etc.
    343   The "p" command puts the text after the cursor.  Use "P" to put the text
    344 before the cursor.  Notice that Vim remembers if you yanked a whole line or a
    345 block, and puts it back that way.
    346 
    347 
    348 USING REGISTERS
    349 
    350 When you want to copy several pieces of text from one file to another, having
    351 to switch between the files and writing the target file takes a lot of time.
    352 To avoid this, copy each piece of text to its own register.
    353   A register is a place where Vim stores text.  Here we will use the
    354 registers named a to z (later you will find out there are others).  Let's copy
    355 a sentence to the f register (f for First): >
    356 
    357 "fyas
    358 
    359 The "yas" command yanks a sentence like before.  It's the "f that tells Vim
    360 the text should be placed in the f register.  This must come just before the
    361 yank command.
    362   Now yank three whole lines to the l register (l for line): >
    363 
    364 "l3yy
    365 
    366 The count could be before the "l just as well.  To yank a block of text to the
    367 b (for block) register: >
    368 
    369 CTRL-Vjjww"by
    370 
    371 Notice that the register specification "b is just before the "y" command.
    372 This is required.  If you would have put it before the "w" command, it would
    373 not have worked.
    374   Now you have three pieces of text in the f, l and b registers.  Edit
    375 another file, move around and place the text where you want it: >
    376 
    377 "fp
    378 
    379 Again, the register specification "f comes before the "p" command.
    380   You can put the registers in any order.  And the text stays in the register
    381 until you yank something else into it.  Thus you can put it as many times as
    382 you like.
    383 
    384 When you delete text, you can also specify a register.  Use this to move
    385 several pieces of text around.  For example, to delete-a-word and write it in
    386 the w register: >
    387 
    388 "wdaw
    389 
    390 Again, the register specification comes before the delete command "d".
    391 
    392 
    393 APPENDING TO A FILE
    394 
    395 When collecting lines of text into one file, you can use this command: >
    396 
    397 :write >> logfile
    398 
    399 This will write the text of the current file to the end of "logfile".  Thus it
    400 is appended.  This avoids that you have to copy the lines, edit the log file
    401 and put them there.  Thus you save two steps.  But you can only append to the
    402 end of a file.
    403   To append only a few lines, select them in Visual mode before typing
    404 ":write".  In chapter 10 you will learn other ways to select a range of lines.
    405 
    406 ==============================================================================
    407 *07.6*	Viewing a file
    408 
    409 Sometimes you only want to see what a file contains, without the intention to
    410 ever write it back.  There is the risk that you type ":w" without thinking and
    411 overwrite the original file anyway.  To avoid this, edit the file read-only.
    412   To start Vim in readonly mode, use this command: >
    413 
    414 vim -R file
    415 
    416 On Unix this command should do the same thing: >
    417 
    418 view file
    419 
    420 You are now editing "file" in read-only mode.  When you try using ":w" you
    421 will get an error message and the file won't be written.
    422   When you try to make a change to the file Vim will give you a warning:
    423 
    424 W10: Warning: Changing a readonly file ~
    425 
    426 The change will be done though.  This allows for formatting the file, for
    427 example, to be able to read it easily.
    428   If you make changes to a file and forgot that it was read-only, you can
    429 still write it.  Add the ! to the write command to force writing.
    430 
    431 If you really want to forbid making changes in a file, do this: >
    432 
    433 vim -M file
    434 
    435 Now every attempt to change the text will fail.  The help files are like this,
    436 for example.  If you try to make a change you get this error message:
    437 
    438 E21: Cannot make changes, 'modifiable' is off ~
    439 
    440 You could use the -M argument to setup Vim to work in a viewer mode.  This is
    441 only voluntary though, since these commands will remove the protection: >
    442 
    443 :set modifiable
    444 :set write
    445 
    446 ==============================================================================
    447 *07.7*	Changing the file name
    448 
    449 A clever way to start editing a new file is by using an existing file that
    450 contains most of what you need.  For example, you start writing a new program
    451 to move a file.  You know that you already have a program that copies a file,
    452 thus you start with: >
    453 
    454 :edit copy.c
    455 
    456 You can delete the stuff you don't need.  Now you need to save the file under
    457 a new name.  The ":saveas" command can be used for this: >
    458 
    459 :saveas move.c
    460 
    461 Vim will write the file under the given name, and edit that file.  Thus the
    462 next time you do ":write", it will write "move.c".  "copy.c" remains
    463 unmodified.
    464   When you want to change the name of the file you are editing, but don't
    465 want to write the file, you can use this command: >
    466 
    467 :file move.c
    468 
    469 Vim will mark the file as "not edited".  This means that Vim knows this is not
    470 the file you started editing.  When you try to write the file, you might get
    471 this message:
    472 
    473 E13: File exists (use ! to override) ~
    474 
    475 This protects you from accidentally overwriting another file.
    476 
    477 ==============================================================================
    478 
    479 Next chapter: |usr_08.txt|  Splitting windows
    480 
    481 Copyright: see |manual-copyright|  vim:tw=78:ts=8:noet:ft=help:norl: