我想使用Hexl模式手动从文本文件中删除一些特殊字符,但不知道如何在Hexl模式下删除任何内容。
我真正想做的是删除回车并保留换行符。
使用Hexl模式是否是正确的方法?
我想使用Hexl模式手动从文本文件中删除一些特殊字符,但不知道如何在Hexl模式下删除任何内容。
我真正想做的是删除回车并保留换行符。
使用Hexl模式是否是正确的方法?
不需要查找替换,只需要使用。
M-x delete-trailing-whitespace
你也可以通过以下方式设置文件编码
C-x RET f unix
糟糕,那个 ^J^M 需要输入为两个字面字符。
使用 c-q
c-j
,c-q
c-m
,替换字符串时使用 c-q
c-j
。
这个不需要使用hexl-mode。只需全局搜索并替换^J^M为^J即可,对我来说很有效。然后保存文件,杀死缓冲区,重新访问文件以显示新的文件模式(Unix vs DOS)。
M-%
对我来说不起作用。 - baxx如果您想删除回车符(通常显示为^M
),并保留换行符。 您只需访问文件而不进行任何转换即可:
M-x find-file-literally /path/to/file
因为带有回车符的文件通常在DOS模式下显示(隐藏回车符)。模式行可能会在左侧显示(DOS)
。
完成后,^M
将会显示出来,您可以像删除任何字符一样删除它们。
(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."))
来自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
文件中,并通过M-x xsteve-remove-control-M
运行它,或将其绑定到更容易的键上。它将在任何模式下删除^M
。