emacs学习2

emacs学习2

emacs学习2

1 dired高级用法

1.1 如何在两个独立的window中复制文件

✍ 把下面这句放在emacs初始化文件中:

(setq dired-dwim-target t)

⎋ 用法:调用C-x 2,另个window分别进入不同的dired目录,现在,当你键入"C"复制文件, 另一个dired目录将成为默认目标,同样"R"(rename)也可以这样使用。

1.2 如何直接进入文件所处的文件夹

✍ 在文件打开状态,如何直接进入文件所处的文件夹,并定位在这个文件。

⎋ 使用: 【Ctrl+x Ctrl+j】

1.3 如何在dired下批量修改文件名(% R/% r)

✍ 例如,进入dired中,列出目录:

  ~/lisp:
  total used in directory 16 available 24997024
  drwxrwxrwx  1 Everyone Everyone    0 02-24 20:33 .
  drwxrwxrwx  1 Everyone Everyone    0 1970-01-01  ..
. -rw-rw-rw-  1 Everyone Everyone  626 02-26 22:35 aui_ABC-kdk1kj.html
. -rw-rw-rw-  1 Everyone Everyone  626 02-26 22:37 aui_ABC-kdk2kj.html
. -rw-rw-rw-  1 Everyone Everyone  626 02-26 22:37 aui_ABC-kdk3kj.html
. -rw-rw-rw-  1 Everyone Everyone  620 02-27 13:49 aui_ABC-kdk4kj.html
  -rw-rw-rw-  1 Everyone Everyone   96 03-02 16:52 mylisp.el
  -rw-rw-rw-  1 Everyone Everyone  649 02-29 22:38 simple
  -rw-rw-rw-  1 Everyone Everyone 4838 02-29 23:27 simplified
  -rw-rw-rw-  1 Everyone Everyone 4836 03-02 11:28 traditional
  -rw-rw-rw-  1 Everyone Everyone 1007 02-29 14:12 x002-1.html
  -rw-rw-rw-  1 Everyone Everyone  636 02-29 09:07 x002-2.html
  -rw-rw-rw-  1 Everyone Everyone  636 02-29 20:41 x003-1.html
  -rw-rw-rw-  1 Everyone Everyone  635 02-29 20:41 x003-2.html

✍ 假设选中auiABC-kdk1kj.html auiABC-kdk2kj.html auiABC-kdk3kj.html auiABC-kdk4kj.html,4个文件,要将其中“ABC”替换为“EDF”,输入"M-x dired-do-rename-regexp"(% R/% r),再输入"\(\w*\)ABC\(\w*.\w+\)",按回车,再输 入"\1EDF\2",按回车,再连续按"y",以上4个文件修改为auiEDF-kdk4kj.html auiEDF-kdk3kj.html auiEDF-kdk2kj.html auiEDF-kdk1kj.html。

✍ 最后,目录变为:

  ~/lisp:
  total used in directory 16 available 24997024
. -rw-rw-rw-  1 Everyone Everyone  620 02-27 13:49 aui_EDF-kdk4kj.html
. -rw-rw-rw-  1 Everyone Everyone  626 02-26 22:37 aui_EDF-kdk3kj.html
. -rw-rw-rw-  1 Everyone Everyone  626 02-26 22:37 aui_EDF-kdk2kj.html
. -rw-rw-rw-  1 Everyone Everyone  626 02-26 22:35 aui_EDF-kdk1kj.html
  drwxrwxrwx  1 Everyone Everyone    0 02-24 20:33 .
  drwxrwxrwx  1 Everyone Everyone    0 1970-01-01  ..
  -rw-rw-rw-  1 Everyone Everyone   96 03-02 16:52 mylisp.el
  -rw-rw-rw-  1 Everyone Everyone  649 02-29 22:38 simple
  -rw-rw-rw-  1 Everyone Everyone 4838 02-29 23:27 simplified
  -rw-rw-rw-  1 Everyone Everyone 4836 03-02 11:28 traditional
  -rw-rw-rw-  1 Everyone Everyone 1007 02-29 14:12 x002-1.html

1.4 如何在dired下动态替换文件内容(Q)

✍ emacs交互式替换命令,dired多文件替换`dired-do-query-replace-regexp'

✍ 现在有一批html文件,其中包含数以千计wikipedia链接,这些链接写的不一致,有的带 下划线,有的不带,有的是全路径名,现在想要将链接名更改成wikipedia网页文件名

<a href="http://en.wikipedia.org/wiki/Stanislaw_Szukalski">kin</a>
<a href="http://en.wikipedia.org/wiki/Stanislaw_Szukalski">Stanislaw_Szukalski</a>
<a href="http://en.wikipedia.org/wiki/Stanislaw_Szukalski">http://en.wikipedia.org/wiki/Stanislaw_Szukalski</a>
    ⇓
<a href="http://en.wikipedia.org/wiki/Stanislaw_Szukalski">Stanislaw Szukalski</a>     

✍ 要达到目的,需要取得wikipedia网页文件名,将甚中的下划线"_"改为空格" ",替换链 接名

✍ emacs中可以使用函数作为替换内容, 先输入要修改的内容,再输入 \,(function),function是elsip函数,在函数中取得wikipedia网页文件名

✍ 函数编写

(defun wikipedia-link-replacement ()
  "Returns a canonical form of Wikipedia link from a regex match.

This function is used for query-replace-regexp, to turn the
following forms of links:

<a href=\"http://en.wikipedia.org/wiki/event\">event</a>
<a href=\"http://en.wikipedia.org/wiki/Middle_distance\">Middle_distance</a>
<a href=\"http://en.wikipedia.org/wiki/Middle_distance_track_event\">Middle_distance_track_event</a>
<a href=\"http://en.wikipedia.org/wiki/Sapir-Whorf_Hypothesis\">Sapir-Whorf_Hypothesis</a>

into a cannonical form. Basically, the link text needs to have
“_” replaced by space. Also, it shouldn't match links that's
already in canonical form, nor matching non-wikipedia link texts.

The regex to be used for this function is:
<a href=\"http://\(..\)\.wikipedia.org/wiki/\([^\"]+\)\">.+</a>

To use this function, call query-replace-regexp, then in the replacement prompt give:
\\,(wikipedia-link-replacement)
."
  (let (langCode articlePath linkText linkText2 returnText)
    (setq langCode (match-string 1))
    (setq articlePath (match-string 2))
    (setq linkText (match-string 3))
    (setq linkText2 (replace-regexp-in-string "_" " " articlePath))
    (setq returnText
          (concat "<a href=\"http://"
                  langCode ".wikipedia.org/wiki/"
                  articlePath "\">" linkText2 "</a>" ))
    returnText ) )

⎋ 操作,进入dired,mark多个文件,输入`dired-do-query-replace-regexp'命令,输入正 则表达式:<a href=\"http://\(..\)\.wikipedia.org/wiki/\([\"]+\)\">.+</a>,再输入 \,(wikipedia-link-replacement)

2 小技巧

2.1 动态替换文件内容

✍ 在网页文件中将类似`&#945'替换成Unicode字符,可以用emacs交互式替换命令,文件内替 换`query-replace-regexp',可以用\,(function),function是elsip函数,在函数中取得欲 替换的内容,处理后输出

(defun ff ()
  "temp function. Returns a string based on current regex match.
This is for the regex: &#\\([0-9]+\\);"
  (char-to-string (string-to-number (match-string 1)))
  )

✍ 简单的办法:输入`query-replace-regexp' Return `&#\([0-9]+\);' Return `\,(char-to-string (string-to-number \1))'

✍ 更好的办法:`\,(char-to-string \#1) \,(format "%c" \#1)'

2.2 修改文件编码

✍ 我使用emacs编写latex,在家里系统是debain6.0,文件默认编码是utf-8,办公室里系统 是win7,文件默认编码是gbk。latax编译时根据编码读取字体,所以在debian6.0中必须是 `utf-8-unix',在win7中必须是`chinese-gbk-dos',我经常需要手动更改文件(C-x ret f) 编码。

✍ 可以使用函数自动完成这个工作,我有两个函数,分别将文件编码转换为 `utf-8-unix'和'chinese-gbk-dos'。使用时进入dired模式,mark需要转换的文件,执行 `to-unix-eol-on-marked-file'或`to-gbk-eol-on-marked-file'。

(defun to-unix-eol (fPath)
  "Change file's line ending to unix convention."
  (let (myBuffer)
    (setq myBuffer (find-file fPath))
    (set-buffer-file-coding-system 'utf-8-unix)
    (save-buffer)
    (kill-buffer myBuffer)
   )
)

(defun to-gbk-eol (fPath)
  "Change file's line ending to gbk convention."
  (let (myBuffer)
    (setq myBuffer (find-file fPath))
    (set-buffer-file-coding-system 'chinese-gbk-dos)
    (save-buffer)
    (kill-buffer myBuffer)
   )
)

(defun to-unix-eol-on-marked-file ()
  "Change marked files ending to unix convention"
  (interactive)
  (mapc 'to-unix-eol (dired-get-marked-files)))

(defun to-gbk-eol-on-marked-file ()
  "Change marked files ending to gbk convention"
  (interactive)
  (mapc 'to-gbk-eol (dired-get-marked-files)))

Date: 2013-04-17 23:03:04 CST

Author: machine of awareness

Org version 7.8.06 with Emacs version 23

Validate XHTML 1.0
原文地址:https://www.cnblogs.com/machine/p/2866939.html