2012-10-11 15 views
0

原始字符串是這樣的:如何在emacs上完成這份工作?

# chrom,name,strand,txStart 

,並將結果如下:

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

有沒有人有一個快速的方法來做到這一點的想法?

+0

有始終只有四個單詞,還是任意數字? – dbr

回答

6

很多方法。

您可以使用替換中的\#計數器進行搜索和替換。這是基於零的,因此您需要在前端添加一個虛擬替換以用盡零,否則請使用替代表達式替換表達式\,(1+ \#)

你可以使用一個鍵盤宏,並插入一個計數器,C-XC-KTAB<F3>。您可以通過在開始錄製時提供前綴參數來播放該計數器。

在Emacs的24,你可以用CX數目爲r使用自定義格式字符串中的一個標記區域的線ñ,讓您的格式字符串可以爲# $%1d:

1

評估以下代碼並在輸入行上執行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))))