题
如何在emacs中快速编写以下代码?
\newcommand{\cA}{\mathcal A}
\newcommand{\cB}{\mathcal B}
\newcommand{\cC}{\mathcal C}
...
...
\newcommand{\cY}{\mathcal Y}
\newcommand{\cZ}{\mathcal Z}
有没有比写字更快的方法
A
B
C
D
.
.
.
Y
Z
然后在每一行上做宏?(将 A 更改为 ewcommand{\cA}{\mathcal A})
解决方案
一般来说,当你第一次面对这类问题时,你会使用键盘宏,就像JB已经说过的那样。
第二次,查看Steve Yegge撰写的这篇非常有趣的文章: http://steve-yegge.blogspot.com/2006/06/shiny-and-new-emacs-22.html ,其中包含与您完全一样的问题解决方案。
为了您的方便和我自己的照明,我实际上继续进行测试:
我会从
开始A ... A
26次
并做一个
M-x replace-regexp
A \\newcommand{\\c\,(string (+ ?A \#))}{\\mathcal \,(string (+ ?A \#))}
其他提示
我同意键盘宏会以最快的速度获得结果。
更有趣的是程序化方法:
(loop
for n from (string-to-char "A") to (string-to-char "Z")
for c = (char-to-string n)
do (insert (concat "\\newcommand{\\c" c "}{\\mathcal " c "}\n")))
A到Z只有26行。与自然使用键盘宏,imho。
相比,你会浪费更多时间来自动生成这取决于你的背景。我只需输入 M - !
和:
perl -e"print map {q(\newcommand{\c).这取决于你的背景。我只需输入 M - !
和:
<*>.q(}{\mathcal ).qq(这取决于你的背景。我只需输入 M - !
和:
<*>}\n)} A..Z
你知道吗 矩形选择 ?
还有 string-rectangle
:
- 将点(光标)放在文本的开头,标记 (
C-SPC
) - 将点放在最后一行的开头
- 类型
M-x string-rectangle
- 输入一个字符串 ( ewcommand{\c)
这将在标记之后的每一行之前插入该字符串。
我没有足够的业力或其他任何评论,但我想改善HD的风格。
以下是原文:
(loop
for n from (string-to-char "A") to (string-to-char "Z")
for c = (char-to-string n)
do (insert (concat "\\newcommand{\\c" c "}{\\mathcal " c "}\n")))
首先,Emacs Lisp具有chars的读者语法。您可以只编写?X
,而不是(string-to-char&quot; X&quot;)
。然后,您可以使用printf样式的格式
而不是 char-to-string
和 concat
来生成最终结果:
(loop for n from ?A to ?Z
do (insert (format "\\newcommand{\\c%s}{\\mathcal %s}\n" n n)))
现在它很简洁,无需考虑进入 M - :
提示符。
我还要指出TeX也有宏,如果这确实是TeX。
编辑:Joe Casadonte的另一种风格建议; (incf foo)
比(setq foo(+ foo 1))
更容易输入。
或者如果你想一次一个地进行交互式方式
(defun somefun(inputval)
(interactive "sInputVal: ")
(insert ( format "\\newcommand{\\c%s}{\\mathcal %s}" inputval inputval) )
)
每次你想要文本时,只需eval和M-x somefun
不完全是答案。
对于不使用emacs或vim的人,我想补充一点,您可以打开Excel并使用其自动填充功能和文本连接功能来生成此类代码。
我喜欢HD的循环比我的好一点,但这是一种替代的,更通用的方法:
(defun my-append (str)
"Substitute A-Z in STR and insert into current buffer.
Looks for '--HERE--' (without quotes) in string STR, substitutes the
letters A-Z in the string and then inserts the resulting string into
the current buffer."
(interactive "sInput String: ")
(let ((lcv 0)
letter newstr)
(while (< lcv 26)
(setq letter (+ ?A lcv))
(insert (replace-regexp-in-string "--HERE--" (char-to-string letter) str t t) "\n")
(setq lcv (+ lcv 1)))))
两者结合起来不应该太难。