usr_23.txt 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. *usr_23.txt* Nvim
  2. VIM USER MANUAL - by Bram Moolenaar
  3. Editing other files
  4. This chapter is about editing files that are not ordinary files. With Vim you
  5. can edit files that are compressed. Some files need to be accessed over the
  6. internet. With some restrictions, binary files can be edited as well.
  7. |23.1| DOS, Mac and Unix files
  8. |23.2| Files on the internet
  9. |23.3| Binary files
  10. |23.4| Compressed files
  11. Next chapter: |usr_24.txt| Inserting quickly
  12. Previous chapter: |usr_22.txt| Finding the file to edit
  13. Table of contents: |usr_toc.txt|
  14. ==============================================================================
  15. *23.1* DOS, Mac and Unix files
  16. Back in the early days, the old Teletype machines used two characters to
  17. start a new line. One to move the carriage back to the first position
  18. (carriage return, <CR>), another to move the paper up (line feed, <LF>).
  19. When computers came out, storage was expensive. Some people decided that
  20. they did not need two characters for end-of-line. The Unix people decided
  21. they could use <New Line> or <NL> only for end-of-line. The Apple people
  22. standardized on <CR>. The Microsoft Windows folks decided to keep the old
  23. <CR><NL> (we use <NL> for line feed in the help text).
  24. This means that if you try to move a file from one system to another, you
  25. have line-break problems. The Vim editor automatically recognizes the
  26. different file formats and handles things properly behind your back.
  27. The option 'fileformats' contains the various formats that will be tried
  28. when a new file is edited. The following command, for example, tells Vim to
  29. try Unix format first and MS-DOS format second: >
  30. :set fileformats=unix,dos
  31. You will notice the format in the message you get when editing a file. You
  32. don't see anything if you edit a native file format. Thus editing a Unix file
  33. on Unix won't result in a remark. But when you edit a dos file, Vim will
  34. notify you of this:
  35. "/tmp/test" [dos] 3L, 71C ~
  36. For a Mac file you would see "[mac]".
  37. The detected file format is stored in the 'fileformat' option. To see
  38. which format you have, execute the following command: >
  39. :set fileformat?
  40. The three names that Vim uses are:
  41. unix <NL>
  42. dos <CR><NL>
  43. mac <CR>
  44. USING THE MAC FORMAT
  45. On Unix, <NL> is used to break a line. It's not unusual to have a <CR>
  46. character halfway in a line. Incidentally, this happens quite often in Vi
  47. (and Vim) scripts.
  48. On the Macintosh, where <CR> is the line break character, it's possible to
  49. have a <NL> character halfway in a line.
  50. The result is that it's not possible to be 100% sure whether a file
  51. containing both <CR> and <NL> characters is a Mac or a Unix file. Therefore,
  52. Vim assumes that on Unix you probably won't edit a Mac file, and doesn't check
  53. for this type of file. To check for this format anyway, add "mac" to
  54. 'fileformats': >
  55. :set fileformats+=mac
  56. Then Vim will take a guess at the file format. Watch out for situations where
  57. Vim guesses wrong.
  58. OVERRULING THE FORMAT
  59. If you use the good old Vi and try to edit an MS-DOS format file, you will
  60. find that each line ends with a ^M character. (^M is <CR>). The automatic
  61. detection avoids this. Suppose you do want to edit the file that way? Then
  62. you need to overrule the format: >
  63. :edit ++ff=unix file.txt
  64. The "++" string is an item that tells Vim that an option name follows, which
  65. overrules the default for this single command. "++ff" is used for
  66. 'fileformat'. You could also use "++ff=mac" or "++ff=dos".
  67. This doesn't work for any option, only "++ff" and "++enc" are currently
  68. implemented. The full names "++fileformat" and "++encoding" also work.
  69. CONVERSION
  70. You can use the 'fileformat' option to convert from one file format to
  71. another. Suppose, for example, that you have an MS-DOS file named README.TXT
  72. that you want to convert to Unix format. Start by editing the MS-DOS format
  73. file: >
  74. vim README.TXT
  75. Vim will recognize this as a dos format file. Now change the file format to
  76. Unix: >
  77. :set fileformat=unix
  78. :write
  79. The file is written in Unix format.
  80. ==============================================================================
  81. *23.2* Files on the internet
  82. Someone sends you an e-mail message, which refers to a file by its URL. For
  83. example:
  84. You can find the information here: ~
  85. ftp://ftp.vim.org/pub/vim/README ~
  86. You could start a program to download the file, save it on your local disk and
  87. then start Vim to edit it.
  88. There is a much simpler way. Move the cursor to any character of the URL.
  89. Then use this command: >
  90. gf
  91. With a bit of luck, Vim will figure out which program to use for downloading
  92. the file, download it and edit the copy. To open the file in a new window use
  93. CTRL-W f.
  94. If something goes wrong you will get an error message. It's possible that
  95. the URL is wrong, you don't have permission to read it, the network connection
  96. is down, etc. Unfortunately, it's hard to tell the cause of the error. You
  97. might want to try the manual way of downloading the file.
  98. Accessing files over the internet works with the netrw plugin. Currently URLs
  99. with these formats are recognized:
  100. ftp:// uses ftp
  101. rcp:// uses rcp
  102. scp:// uses scp
  103. http:// uses wget (reading only)
  104. Vim doesn't do the communication itself, it relies on the mentioned programs
  105. to be available on your computer. On most Unix systems "ftp" and "rcp" will
  106. be present. "scp" and "wget" might need to be installed.
  107. Vim detects these URLs for each command that starts editing a new file, also
  108. with ":edit" and ":split", for example. Write commands also work, except for
  109. http://.
  110. For more information, also about passwords, see |netrw|.
  111. ==============================================================================
  112. *23.3* Binary files
  113. You can edit binary files with Vim. Vim wasn't really made for this, thus
  114. there are a few restrictions. But you can read a file, change a character and
  115. write it back, with the result that only that one character was changed and
  116. the file is identical otherwise.
  117. To make sure that Vim does not use its clever tricks in the wrong way, add
  118. the "-b" argument when starting Vim: >
  119. vim -b datafile
  120. This sets the 'binary' option. The effect of this is that unexpected side
  121. effects are turned off. For example, 'textwidth' is set to zero, to avoid
  122. automatic formatting of lines. And files are always read in Unix file format.
  123. Binary mode can be used to change a message in a program. Be careful not to
  124. insert or delete any characters, it would stop the program from working. Use
  125. "R" to enter replace mode.
  126. Many characters in the file will be unprintable. To see them in Hex format: >
  127. :set display=uhex
  128. Otherwise, the "ga" command can be used to see the value of the character
  129. under the cursor. The output, when the cursor is on an <Esc>, looks like
  130. this:
  131. <^[> 27, Hex 1b, Octal 033 ~
  132. There might not be many line breaks in the file. To get some overview switch
  133. the 'wrap' option off: >
  134. :set nowrap
  135. BYTE POSITION
  136. To see on which byte you are in the file use this command: >
  137. g CTRL-G
  138. The output is verbose:
  139. Col 9-16 of 9-16; Line 277 of 330; Word 1806 of 2058; Byte 10580 of 12206 ~
  140. The last two numbers are the byte position in the file and the total number of
  141. bytes. This takes into account how 'fileformat' changes the number of bytes
  142. that a line break uses.
  143. To move to a specific byte in the file, use the "go" command. For
  144. example, to move to byte 2345: >
  145. 2345go
  146. USING XXD
  147. A real binary editor shows the text in two ways: as it is and in hex format.
  148. You can do this in Vim by first converting the file with the "xxd" program.
  149. This comes with Vim.
  150. First edit the file in binary mode: >
  151. vim -b datafile
  152. Now convert the file to a hex dump with xxd: >
  153. :%!xxd
  154. The text will look like this:
  155. 0000000: 1f8b 0808 39d7 173b 0203 7474 002b 4e49 ....9..;..tt.+NI ~
  156. 0000010: 4b2c 8660 eb9c ecac c462 eb94 345e 2e30 K,.`.....b..4^.0 ~
  157. 0000020: 373b 2731 0b22 0ca6 c1a2 d669 1035 39d9 7;'1.".....i.59. ~
  158. You can now view and edit the text as you like. Vim treats the information as
  159. ordinary text. Changing the hex does not cause the printable character to be
  160. changed, or the other way around.
  161. Finally convert it back with:
  162. >
  163. :%!xxd -r
  164. Only changes in the hex part are used. Changes in the printable text part on
  165. the right are ignored.
  166. See the manual page of xxd for more information.
  167. ==============================================================================
  168. *23.4* Compressed files
  169. This is easy: You can edit a compressed file just like any other file. The
  170. "gzip" plugin takes care of decompressing the file when you edit it. And
  171. compressing it again when you write it.
  172. These compression methods are currently supported:
  173. .Z compress
  174. .gz gzip
  175. .bz2 bzip2
  176. Vim uses the mentioned programs to do the actual compression and
  177. decompression. You might need to install the programs first.
  178. ==============================================================================
  179. Next chapter: |usr_24.txt| Inserting quickly
  180. Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: