The difference between a Linux file and a Windows file in line break

Source: Internet
Author: User

Linux or UNIX files, and Windows files, when processing back and forth, if you do not pay attention to the difference between line breaks, may cause a program Error!!! A profound lesson ....

In the early days of the printer, it took two characters to start a new line. If you go to the end of a line, you want to quickly get back to the beginning of a new line, you need to print the needle quickly on paper, often leave a stain on the paper. The solution to this problem is to use two characters: one character <Return> to move to the first column, and another character <line feed> to add a row. After the computer is produced, the storage is more expensive, and people have different opinions on how to solve the old problem of carriage return and line changing. Unix people think that at the end of the arrival line to add a line <line feed> (LF), while the Mac people agree with the <Return> (CR) solution, Ms Stick to the old <return><line feed > (CRLF) method. This means that if you move a file from one system to another, you face a problem with a carriage return. The Vim editor automatically recognizes the difference in file format and handles it accordingly.

The fileformats option for handling file format issues. The following command tells Vim to make the UNIX file format the first choice, and the MS-DOS file format as the second choice:

:set fileformats=unix,dos

The detected file format is stored in the FileFormat option and can be queried using the following command:

:set fileformat?

We can also use the FileFormat option to convert the format of a file. For example, use the following command to convert an MS-DOS-formatted file named Readme.txt to a UNIX-formatted file.

:set fileformat=unix

By default, VIM considers a file to be composed of rows, and the last line of the file is a <EOL> terminator. If you want to set the file to end with the <EOL> terminator, you can use the following command:

:set endofline

If you want to set the file to not end with the <EOL> terminator, you can use the following command:

:set noendofline

Show line breaks

If you use the following command to enter <list Mode>, then you can clearly see the newline character represented by "$" and the tab character represented by "^i".

:set list

You can exit <list Mode> by using the following command:

:set nolist

Remove line breaks

You can remove a newline character with the following command:

:%s/\n//g

You can remove the carriage return "^m" in a DOS file with the following command:

:%s/\r//g

You can convert the DOS carriage return "^m" to a true line break with the following command:

:%s/\r/\r/g

You can use the following command to delete a space at the end of a line and a DOS return character. Instead of using the "/" character as the delimiter between the various parameters of the replacement command, it uses the "#" character. The command also uses the "\?" A regular expression that matches the character that appears before it 0 or 1 times.

:%s#\s*\r\?$##

You can delete three lines of blank lines with the following command:

:%s/^\n\{3}//

You can replace two consecutive empty lines with a blank line with the following command:

:%s/\n\n/\r/g

Wrap Line

By default, Vim does not wrap, which means we need to decide where to enter. But we can also let vim automatically wrap the line at the specified location by specifying the TEXTWIDTH option. For example, use the following command to specify that line wrapping occurs in 30 columns. The exact position of the newline may be less than the column width you specify, because the line wraps only when a space is encountered.

:set textwidth=29

The following command tells Vim to change the line when it reaches a space of 10 characters from the right:

:set wrapmargin=10

Given the width of the text, when we delete the first few words in a row, Vim does not move the text in the subsequent lines, but rather forms a few paragraphs of varying length, which does not look very good. There are several ways to deal with this problem:

One way to do this is to select the text in Visual mode and then gp format the selected paragraph with a command. Another way is to use gqmotion commands to complete the format. For example, to format 5 rows, you can use the command :gq4j . This command tells VIM to format the bank, and to format the following 4 lines, so that the format of the 5 line. If you want to format the entire paragraph, you can place the cursor on the first line of the paragraph, and then execute the command gq} . Instead gqip , you can format the current paragraph with a command, and it is not necessary to place the cursor on the first line of the paragraph.

If you want to format a row, you can use gqgq the command or précis-writers gqq .

Note: The option textwidth takes precedence over Wrapmargin. If you want the Wrapmargin option to take effect, you must set the TextWidth to 0 (which is also the default value).

Merge rows

Command J to merge two rows into one line, separating the two lines with a space.

By setting the Joinspace option, you can control the delimiter for merging two rows (if one line ends with a punctuation mark). If set :set nojoinspaces , merging two rows with the J command will be separated by a single space :set joinspaces , and if set, a combination of two rows with the J command will be separated by two spaces. If you do not want a space to separate the merged rows, you can use the gJ command.

We can also use :join commands to combine several lines into one line and use spaces to separate rows. If you do not want to include a space, you can use the :join! command.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.