usr_23.txt ForVim version 9.2. Last change: 2026 Feb 14 VIM USER MANUALbyBramMoolenaar Editing other filesThis chapteris about editing files that are not ordinary files. With Vim youcan edit files that are compressed or encrypted. Some files need to beaccessed over the internet. With some restrictions, binary files can beeditedas well.23.1 DOS,Mac andUnix files23.2 Files on theinternet23.3 Encryption23.4 Binary files23.5 Compressed files Next chapter:usr_24.txt Inserting quickly Previous chapter:usr_22.txt Finding the file to editTable of contents:usr_toc.txt==============================================================================23.1 DOS,Mac andUnix filesBack in the early days, the old Teletype machines used two characters tostarta new line. One to move the carriage back to the first position(carriage return,<CR>), another to move the paper up (line feed,<LF>). When computers came out, storage was expensive. Some people decided thatthey did not need two characters for end-of-line. The UNIX people decidedthey could use <New Line> or<NL> only for end-of-line. The Apple peoplestandardized on<CR>. The Microsoft Windows folks decided to keep the old<CR><NL> (we use<NL> for line feed in thehelp text). This means that if you try to movea file from one system to another, youhave line-break problems. The Vim editor automatically recognizes thedifferent file formats and handles things properly behind your back. The option'fileformats' contains thevarious formats that will be triedwhena new fileis edited. The following command, for example, tells Vim totry UNIX format first andMS-DOS format second::set fileformats=unix,dosYou will notice the format in the message you get when editinga file. Youdon't see anything if you edita native file format. Thus editingaUnix fileonUnix won't result ina remark. But when you editados file, Vim willnotify you of this:"/tmp/test" [dos] 3L, 71CForaMac file you would see "[mac]". The detected file formatis stored in the'fileformat' option. To seewhich format you have, execute the following command::set fileformat?The three names that Vim uses are:unix<NL>dos<CR><NL>mac<CR>USING THE MAC FORMATOn Unix,<NL>is used to breaka line. It's not unusual to havea<CR>character halfwaya line. Incidentally, this happens quite often inVi (andVim) scripts. On the Macintosh, where<CR>is the line break character, it's possible tohavea<NL> character halfwaya line. The resultis that it's not possible to be 100% sure whethera filecontaining both<CR> and<NL> charactersisaMac oraUnix file. Therefore,Vim assumes that onUnix you probably won't editaMac file, and doesn't checkfor this type of file. To check for this format anyway, add "mac" to'fileformats'::set fileformats+=macThen Vim will takea guessat the file format. Watch out for situations whereVim guesses wrong.OVERRULING THE FORMATIf you use the good oldVi and try to edit anMS-DOS format file, you willfind that each line ends witha ^M character. (^Mis<CR>). The automaticdetection avoids this. Suppose youdo want to edit the file that way? Thenyou need to overrule the format::edit ++ff=unix file.txtThe "++"stringis an item that tells Vim that an option name follows, whichoverrules the default for this single command. "++ff"is used for'fileformat'. You could also use "++ff=mac" or "++ff=dos". This doesn't work for any option, only "++ff" and "++enc" are currentlyimplemented. The full names "++fileformat" and "++encoding" also work.CONVERSIONYou can use the'fileformat' option to convert from one file format toanother. Suppose, for example, that you have anMS-DOS file named README.TXTthat you want to convert to UNIX format. Start by editing theMS-DOS formatfile:vim README.TXTVim will recognize thisasados format file. Now change the file format toUNIX::set fileformat=unix:writeThe fileis written inUnix format.==============================================================================23.2 Files on theinternetSomeone sends you an e-mail message, which refers toa file by its URL. Forexample:You can find the information here:https://ftp.nluug.nl/pub/vim/READMEYou could starta program todownload the file, saveit on your local disk andthen start Vim to edit it. Thereisa much simpler way. Move the cursor to any character of the URL.Then use this command:gfWitha bit of luck, Vim will figure out which program to use for downloadingthe file,downloadit and edit the copy. To open the file ina newwindow useCTRL-W f. If something goes wrong you will get an error message. It's possible thatthe URLis wrong, you don't have permission to read it, thenetwork connectionis down, etc. Unfortunately, it's hard to tell the cause of the error. Youmight want to try the manual way of downloading the file.Accessing files over theinternet works with thenetrw plugin. Currently URLswith these formats are recognized:ftp:// usesftprcp:// usesrcpscp:// usesscphttp:// uses wget (reading only)Vim doesn'tdo the communication itself,it relies on the mentioned programsto be available on your computer. On mostUnix systems "ftp" and "rcp" willbe present. "scp" and "wget" might need to be installed.Vim detects these URLs for each command that starts editinga new file, alsowith ":edit" and ":split", for example. Write commands also work, except forhttp://.For more information, also about passwords, seenetrw.==============================================================================23.3 EncryptionSome information you prefer to keep to yourself. For example, whenwritinga test ona computer that students also use. You don't want clever studentsto figure outa way to read the questions before the exam starts. Vim canencrypt the file for you, which gives you some protection. To start editinga new file with encryption, use the "-x" argument to startVim. Example:vim -x exam.txtVim prompts you fora key used for encrypting and decrypting the file:Enter encryption key:Carefully type the secret key now. You cannot see the characters you type,they will be replaced by stars. To avoid the situation thata typing mistakewill cause trouble, Vim asks you to enter the key again:Enter same key again:You can now edit this file normally andput in all your secrets. When youfinish editing the file and tell Vim to exit, the fileis encrypted andwritten. When you edit the file with Vim,it will ask you to enter the same keyagain. You don't need to use the "-x" argument. You can also use the normal":edit" command. Vim addsa magicstring to the file by whichit recognizesthat the file was encrypted. If you try toview this file using another program, all you getis garbage.Also, if you edit the file with Vim and enter the wrong key, you get garbage.Vim does not havea mechanism to check if the keyis the right one (this makesit much harder to break the key).SWITCHING ENCRYPTION ON AND OFFTo disable theencryption ofa file, set the'key' option to an empty string::set key=The next time you write the file this will be done without encryption. Setting the'key' option to enableencryptionis nota good idea, becausethe password appears in the clear. Anyone shoulder-surfing can read yourpassword. To avoid this problem, the ":X" command was created. It asks you for anencryption key, just like the "-x" argument did::XEnter encryption key: ******Enter same key again: ******LIMITS ON ENCRYPTIONTheencryption algorithm used by Vimis not very strong. Itis good enough tokeep out the casual prowler, but not good enough to keep outa cryptologyexpert with lots of time on his hands. The text in the swap file and theundofileis also encrypted. However, thisis done block-by-block and may reducethe time needed to cracka password. You can disable the swap file, but thena crash will cause you to lose your work, since Vim keeps all the text inmemory only. Theundo file can be disabled with the only disadvantage thatyou can'tundo after unloading the buffer. To avoid usinga swap file, supply the-n argument on the command line.For example, to edit the encrypted file "file.txt" withouta swap file use thefollowing command:vim -x -n file.txtWhen already editinga file, the swapfile can be disabled with::setlocal noswapfileSince thereis no swapfile,recovery will be impossible. Save the filea bitmore often to avoid the risk of losing your changes.While the fileis in memory,itis in plain text. Anyone with privilege canlook in the editor's memory and discover the contents of the file. If you useaviminfo file, be aware that the contents of textregisters arewritten out in the clearas well. If you really want to secure the contents ofa file, editit only onaportable computer not connected toa network, use goodencryption tools, andkeep the computer locked up ina big safe when not in use.==============================================================================23.4 Binary filesYou can edit binary files with Vim. Vim wasn't really made for this, thusthere area few restrictions. But you can reada file, changea character andwriteit back, with the result that only that one character was changed andthe fileis identical otherwise. To make sure that Vim does not use its clever tricks in the wrong way, addthe "-b" argument whenstarting Vim:vim -b datafileThis sets the'binary' option. The effect of thisis that unexpected sideeffects are turned off. For example,'textwidth'is set to zero, to avoidautomaticformatting of lines. And files are always read inUnix file format.Binary mode can be used to changea message ina program. Be careful not toinsert or delete any characters,it would stop the program from working. Use"R" to enter replace mode.Many characters in the file will be unprintable. To see them in Hex format::set display=uhexOtherwise, the "ga" command can be used to see the value of the characterunder the cursor. The output, when the cursoris on an<Esc>, looks likethis:<^[> 27, Hex 1b, Octal 033There might not be many line breaks in the file. To get some overview switchthe'wrap' option off::set nowrapBYTE POSITIONTo see on which byte you are in the file use this command:g CTRL-GThe outputis verbose: Col 9-16 of 9-16; Line 277 of 330; Word 1806 of 2058; Byte 10580 of 12206The last two numbers are the byte position in the file and the total number ofbytes. This takes into account how'fileformat' changes the number of bytesthata line break uses. To move toa specific byte in the file, use the "go" command. Forexample, to move to byte 2345:2345goUSING XXDA real binary editor shows the text in two ways:asitis and in hex format.You cando this in Vim by first converting the file with the "xxd" program.This comes with Vim. First edit the file in binary mode:vim -b datafileNow convert the file toa hex dump with xxd::%!xxdThe text will look like this:0000000: 1f8b 0808 39d7 173b 0203 7474 002b 4e49 ....9..;..tt.+NI0000010: 4b2c 8660 eb9c ecac c462 eb94 345e 2e30 K,.`.....b..4^.00000020: 373b 2731 0b22 0ca6 c1a2 d669 1035 39d9 7;'1.".....i.59.You can nowview and edit the textas you like. Vim treats the informationasordinary text. Changing the hex does not cause the printable character to bechanged, or the other way around. Finally convertit back with::%!xxd -rOnly changes in the hex part are used. Changes in the printable text part onthe right are ignored.See the manual page of xxd for more information.==============================================================================23.5 Compressed filesThisis easy: You can edita compressed file just like any other file. The"gzip"plugin takes care of decompressing the file when you edit it. Andcompressingit again when you write it. These compression methods are currently supported:.Zcompress.gzgzip.bz2bzip2Vim uses the mentioned programs todo the actual compression anddecompression. You might need toinstall the programs first.==============================================================================Next chapter:usr_24.txt Inserting quicklyCopyright: seemanual-copyright vim:tw=78:ts=8:noet:ft=help:norl: