如何去掉 CR (^M) 并保留 LF (^J) 字符?

发布于 2024-07-29 02:45:03 字数 119 浏览 8 评论 0原文

我正在尝试使用 Hexl 模式手动从文本文件中删除一些特殊字符,但不知道如何在 Hexl 模式下删除任何内容。

我真正想要的是删除回车符并保留换行符。
Hexl 模式是执行此操作的正确方法吗?

I am trying to use Hexl mode to manually remove some special characters from a text file and don't see how to delete anything in Hexl mode.

What I really want is to remove carriage return and keep linefeed characters.
Is Hexl mode the right way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

櫻之舞 2024-08-05 02:45:03

无需寻找替代。 就用吧。

M-x delete-trailing-whitespace

您还可以通过设置文件编码

C-x RET f unix

No need to find replace. Just use.

M-x delete-trailing-whitespace

You can also set the file encoding through

C-x RET f unix
陌生 2024-08-05 02:45:03

哎呀。 ^J^M 需要作为两个文字字符输入。
使用 cq cjcq cm 并使用 cq <代码>cj。

Oops. That ^J^M needs to be entered as two literal characters.
Use c-q c-j, c-q c-m and for the replacement string, use c-q c-j.

一个人练习一个人 2024-08-05 02:45:03

为此不需要 hexl 模式。 只需对 ^J^M 进行全局搜索并替换为 ^J 对我有用。 :) 然后保存文件,终止缓冲区,然后重新访问该文件,以便窗口显示新的文件模式(Unix 与 DOS)。

No need for hexl-mode for this. Just do a global-search-and-replace of ^J^M with ^J Works for me. :) Then save the file, kill the buffer, and revisit the file so the window shows the new file mode (Unix vs DOS).

心在旅行 2024-08-05 02:45:03

还有一个名为 unix2dos/dos2unix 的命令行工具,专门用于转换行结尾。

There's also a command-line tool called unix2dos/dos2unix that exists specifically to convert line endings.

败给现实 2024-08-05 02:45:03

假设您希望将 DOS 编码文件更改为 UNIX 编码,请使用 Mx set-buffer-file-coding-system (Cx RET f) 将编码系统设置为“unix”并保存文件。

Assuming you want a DOS encoded file to be changed into UNIX encoding, use M-x set-buffer-file-coding-system (C-x RET f) to set the coding-system to "unix" and save the file.

没有心的人 2024-08-05 02:45:03

如果要删除回车符(通常显示为 ^M)并保留换行符。 您可以直接访问该文件而不进行任何转换:

M-x find-file-literally /path/to/file

因为带有回车符的文件通常在 DOS 模式下显示(隐藏回车符)。 模式行可能会在左侧显示 (DOS)

完成此操作后,^M 将显示,您可以像删除任何字符一样删除它们。

If you want to remove a carriage return (usually displayed as ^M) and leave the line feed. You can just visit the file w/out any conversion:

M-x find-file-literally /path/to/file

Because a file with carriage returns is generally displayed in DOS mode (hiding the carriage returns). The mode line will likely display (DOS) on the left side.

Once you've done that, the ^M will show up and you can delete them like you would any character.

从来不烧饼 2024-08-05 02:45:03

您不需要使用十六进制模式。 相反:

  • 以显示那些 ^M 的方式打开文件。 请参阅上面的 Mx find-file-literally /path/to/file。 在 XEmacs 中,您还可以执行 Cu Cx Cf 并选择二进制编码。
  • 选择您想要替换的字符串,然后使用 Mw
  • do M-%(查询替换)复制它,然后使用 Cy 粘贴您想要复制的内容,
  • 当提示您替换它时,
  • 可以按 Enter ! 现在替换所有出现的情况

要点是,即使您不知道如何输入要替换的内容,您也可以随时选择/复制它。

You don't need to use hexl-mode. Instead:

  • open file in a way that shows you those ^M's. See M-x find-file-literally /path/to/file above. In XEmacs you can also do C-u C-x C-f and select binary encoding.
  • select the string you want replace and copy it using M-w
  • do M-% (query replace) and paste what you want to copy using C-y
  • present Enter when prompted to what replace it with
  • possible press ! now to replace all occurrences

The point is that even if you don't how to enter what you are trying to replace, you can always select/copy it.

绝影如岚 2024-08-05 02:45:03

(在十六进制模式下)我不确定您是否可以删除字符。 我总是将它们转换为空格或其他字符,切换到常规文本编辑器,然后在那里删除它们。

(in hexl mode) I'm not sure that you can delete characters. I've always converted them to spaces or some other character, switched to the regular text editor, and deleted them there.

温柔女人霸气范 2024-08-05 02:45:03

我使用这个功能:

(defun l/cr-sanitise ()
  "Make sure current buffer uses unix-utf8 encoding.
If necessary remove superfluous ^M. Buffer will need to be saved
for changes to be permanent."
  (interactive)
    (set-buffer-file-coding-system 'utf-8-unix)
    (delete-trailing-whitespace)
    (message "Please save buffer to persist encoding changes."))

I use this function:

(defun l/cr-sanitise ()
  "Make sure current buffer uses unix-utf8 encoding.
If necessary remove superfluous ^M. Buffer will need to be saved
for changes to be permanent."
  (interactive)
    (set-buffer-file-coding-system 'utf-8-unix)
    (delete-trailing-whitespace)
    (message "Please save buffer to persist encoding changes."))
腹黑女流氓 2024-08-05 02:45:03

来自 http://www.xsteve.at/prg/emacs/xsteve-functions .el

;02.02.2000
(defun xsteve-remove-control-M ()
  "Remove ^M at end of line in the whole buffer."
  (interactive)
  (save-match-data
    (save-excursion
      (let ((remove-count 0))
        (goto-char (point-min))
        (while (re-search-forward (concat (char-to-string 13) "$") (point-max) t)
          (setq remove-count (+ remove-count 1))
          (replace-match "" nil nil))
        (message (format "%d ^M removed from buffer." remove-count))))))

将其添加到您的 .emacs 中,并通过 Mx xsteve-remove-control-M 运行它或将其绑定到更简单的键。 它会在任何模式下去除 ^M

From http://www.xsteve.at/prg/emacs/xsteve-functions.el:

;02.02.2000
(defun xsteve-remove-control-M ()
  "Remove ^M at end of line in the whole buffer."
  (interactive)
  (save-match-data
    (save-excursion
      (let ((remove-count 0))
        (goto-char (point-min))
        (while (re-search-forward (concat (char-to-string 13) "$") (point-max) t)
          (setq remove-count (+ remove-count 1))
          (replace-match "" nil nil))
        (message (format "%d ^M removed from buffer." remove-count))))))

Add this to your .emacs and run it via M-x xsteve-remove-control-M or bind it to a easier key. It will strip the ^Ms in anymode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文