Что это за ^ M, которые продолжают появляться в моих файлах в emacs?

139
задан random 17 December 2015 в 15:34
поделиться

10 ответов

Someone is not converting their line-ending characters correctly.

I assume it's the Windows folk as they love their CRLF. Unix loves LF and Mac loved CR until it was shown the Unix way.

93
ответ дан 23 November 2019 в 22:59
поделиться

Я столкнулся с этой проблемой некоторое время назад. ^ M представляет собой возврат каретки, и поиск по Ctrl-Q Ctrl-M (это создает литерал ^ M) позволит вам получить дескриптор этого символа в Emacs. Я сделал что-то в этом роде:

M-x replace-string [ENTER] C-q C-m [ENTER] \n [ENTER]
1
ответ дан 23 November 2019 в 22:59
поделиться

As everyone has mentioned. It's different line ending style. MacOSX uses Unix line endings - i.e. LF (line feed).

Windows uses both CR (carriage return) & LF (line feed) as a line ending. Так как вы используете как Windows, так и Mac, именно в этом и заключается проблема.

Если вы создадите файл в Windows, а затем перенесете его на Mac, вы можете увидеть эти символы ^ M в конце строк.

Если вы хотите удалить их, вы можете сделать это очень легко в emacs. Просто выделите и скопируйте символ ^ M и выполните запрос, заменив ^ M на, и все готово.

РЕДАКТИРОВАТЬ: Некоторые другие ссылки, которые могут быть полезны. http://xahlee.org/emacs/emacs_adv_tips.html

Это поможет вам настроить emacs для использования определенного типа стиля окончания строки. http://www.emacswiki.org/emacs/EndOfLineTips

2
ответ дан 23 November 2019 в 22:59
поделиться

^M at the end of line in Emacs is indicating a carriage return (\r) followed by a line feed (\n). You'll often see this if one person edits files on Windows (where end of line is the combination of carriage return and newline characters) and you edit in Unix or Linux (where end of line is only a newline character).

The combination of characters is usually not harmful. If you're using source control, you may be able to configure the text file checkin format so that lines are magically adjusted for you. Alternatively, you may be able to use checkin and checkout triggers that will automatically "fix" the files for you. Or, you might just use a tool like dos2unix to manually adjust things.

4
ответ дан 23 November 2019 в 22:59
поделиться

вместо замены запроса вы также можете использовать Mx delete-trailing-whitespace

6
ответ дан 23 November 2019 в 22:59
поделиться

They have to do with the difference between DOS style line endings and Unix style. Check out the Wikipedia article. You may be able to find a dos2unix tool to help, or simply write a small script to fix them yourself.

Edit: I found the following Python sample code here:

string.replace( str, '\r', '' )
8
ответ дан 23 November 2019 в 22:59
поделиться

^M is 0x0d, i.e. the carriage return character. If your display looks like

line 1^M
line 2^M

then the file must have come from Windows because the standard newline sequence on Windows is CR LF (0x0d 0x0a) whereas the standard newline sequence consists solely of LF on Unices.

If the file had come from a Mac OS 9 or earlier system, you would see it as

line 1^Mline 2^M

because there would be no line feeds following the carriage returns.

30
ответ дан 23 November 2019 в 22:59
поделиться

In git-config, set core.autocrlf to true to make git automatically convert line endings correctly for your platform, e.g. run this command for a global setting:

git config --global core.autocrlf true
101
ответ дан 23 November 2019 в 22:59
поделиться

Добавьте следующее в свой ~ / .emacs (или eqiuvalent)

(defun dos2unix ()
  "Replace DOS eolns CR LF with Unix eolns CR"
  (interactive)
    (goto-char (point-min))
      (while (search-forward "\r" nil t) (replace-match "")))

, и тогда вы сможете просто использовать Mx dos2unix .

5
ответ дан 23 November 2019 в 22:59
поделиться
[

]См. также:[

] [

][]Скрытие ^M в emacs[][

] [

]Будьте осторожны, если вы решили удалить символы ^M и повторно отправить в свою команду. После этого они могут увидеть файл без возврата каретки.[

]
1
ответ дан 23 November 2019 в 22:59
поделиться
Другие вопросы по тегам:

Похожие вопросы: