usr_07.txt 16 KB

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