2011-07-15 23 views
17

是否有與相反的M-q,某種unfill-paragraph-functionM-q的反函數,不完整段落函數

如果我有撤消數據,那當然很容易。我所要求的只是在剛剛從磁盤讀取文件之後,將段落中的行合併爲一個長整行的能力。這樣就可以將文本粘貼到期望每個段落只有一個換行符的表單(網頁表單等)中。

在過去,我關閉了auto-fill,創建了一個宏來刪除一個EOL並移動到下一行,並重復應用它,但這會讓人很累。

回答

21

Here's the answer。總之:

(defun unfill-paragraph() 
    "Replace newline chars in current paragraph by single spaces. 
This command does the reverse of `fill-paragraph'." 
    (interactive) 
    (let ((fill-column 90002000)) 
    (fill-paragraph nil))) 

(defun unfill-region (start end) 
    "Replace newline chars in region by single spaces. 
This command does the reverse of `fill-region'." 
    (interactive "r") 
    (let ((fill-column 90002000)) 
    (fill-region start end))) 

更新:我已經打包這件事here它可以從MarmaladeMelpa安裝。

+0

完美。謝謝。 – Calaf

+5

我很好奇這個整數。它來自哪裏,是否有理由不使用「最積極的fixnum」呢? – phils

+0

您必須問原作者,但是在每個Emacs版本和變體中可能不存在「最正面 - 修復」。我也很好奇。 – sanityinc

7

另請參閱M- ^(刪除縮進)。

它將當前行連接到上一行,所以如果您從段落的最後一行開始,您可以繼續按M- ^,直到所有行都合併爲止。

+0

這種方法沒有考慮註釋語法之類的東西,儘管它*至少足夠聰明以在適當的標點符號之後插入空格(取決於模式)。 – phils

+0

phils:「如果有填充前綴,請從該行的開頭刪除它」。在第一個非填充前綴列的點處,您可以使用C-x。 (set-fill-prefix),然後M- ^會按照你想要的方式工作。 M-^並不像M-q那樣自己找出填充前綴,這真是令人遺憾。 –

+0

整潔..它開箱即用.. – Calaf

0

我第一次使用@ sanityinc的解決方案一段時間,直到我來到Stefan Monnier's Unfill Paragraph in EmacsWiki。看起來更加強大

;;; Stefan Monnier <foo at acm.org>. It is the opposite of fill-paragraph  
(defun unfill-paragraph (&optional region) 
    "Takes a multi-line paragraph and makes it into a single line of text." 
    (interactive (progn (barf-if-buffer-read-only) '(t))) 
    (let ((fill-column (point-max)) 
     ;; This would override `fill-column' if it's an integer. 
     (emacs-lisp-docstring-fill-column t)) 
    (fill-paragraph nil region))) 

;; Handy key definition 
(define-key global-map "\M-Q" 'unfill-paragraph) 

(defun unfill-region (beg end) 
    "Unfill the region, joining text paragraphs into a single 
logical line. This is useful, e.g., for use with 
`visual-line-mode'." 
    (interactive "*r") 
    (let ((fill-column (point-max))) 
    (fill-region beg end))) 

;; Handy key definition 
(define-key global-map "\C-\M-Q" 'unfill-region) 

鍵綁定使命令變得更容易。