如何在emacs中完成这项工作?

如何在emacs中完成这项工作?,emacs,Emacs,原始字符串如下所示: # chrom,name,strand,txStart # $1: chrom # $2: name # $3: strand # $4: txStart 结果应该是这样的: # chrom,name,strand,txStart # $1: chrom # $2: name # $3: strand # $4: txStart 有人想到了一种快速的方法吗?评估下面的代码并在输入行上执行foo函数 (require 'cl) (defun

原始字符串如下所示:

  # chrom,name,strand,txStart
 # $1: chrom
 # $2: name
 # $3: strand
 # $4: txStart
结果应该是这样的:

  # chrom,name,strand,txStart
 # $1: chrom
 # $2: name
 # $3: strand
 # $4: txStart

有人想到了一种快速的方法吗?

评估下面的代码并在输入行上执行
foo
函数

(require 'cl)

(defun foo ()
  (interactive)
  (let* ((str (buffer-substring-no-properties
               (line-beginning-position) (line-end-position)))
         (words-str (and (string-match "# \\(.+\\)$" str)
                         (match-string 1 str)))
         (buf (get-buffer-create "*tmp*")))
    (unless words-str
      (error "Line should be '# word1,word2,...'"))
    (with-current-buffer buf
      (erase-buffer)
      (loop with index = 1
            for word in (split-string words-str ",")
            do
            (progn
              (insert (format "# $%d: %s\n" index word))
              (incf index)))
      (pop-to-buffer buf))))
很多方法

您可以使用替换中的
\\\code>计数器进行搜索和替换。这是基于零的,因此您需要在前面添加一个虚拟替换以使用零,或者使用elisp替换表达式
\,(1+\)

您可以使用键盘宏,并使用C-xC-kTAB或插入计数器。开始录制时,可以通过提供前缀参数为该计数器设定种子


在Emacs 24上,您可以使用带有C-uC-xrN的自定义格式字符串对标记区域的行进行编号,因此您的格式字符串可以是
#$%1d:

始终只有四个字,还是任意数字?