2012-06-21 37 views

回答

23

如果启用CUA选择模式:

M-Xcua-selection-modeRET在init文件

或永久:

(cua-selection-mode 1) 

然后可以使用其先进的矩形的编辑功能。

(或者如果你是一个cua-mode用户,那么你就不需要做。)

  • C-RET标记一个角落。
  • 将点移动到对角。
  • M-r做一个10正则表达式替换在标记的矩形内。
  • C-RET取消标记/退出矩形编辑。

有关文档,查找 “CUA矩形支持” MX在评论中标题find-libraryRETcua-baseRET

如果你不想使用的CUA矩形设施某种原因(也许如果你确实需要replace-string),使用apply-on-rectangle的自定义函数将非常简单地放在一起。

编辑:其实,稍微复杂一点比我预想的,但大部分代码是交互规范,对于“分隔” prefix参数的行为支持(包括基于replace-string)。

编辑2:我决定,这是值得做的一个功能更全面的方式:

下面提供CX[RM-%CX[RCM - %,(希望)按照您的预期行事。

(require 'rect) 

(defun my-search-replace-in-rectangle 
    (start end search-pattern replacement search-function literal) 
    "Replace all instances of SEARCH-PATTERN (as found by SEARCH-FUNCTION) 
with REPLACEMENT, in each line of the rectangle established by the START 
and END buffer positions. 

SEARCH-FUNCTION should take the same BOUND and NOERROR arguments as 
`search-forward' and `re-search-forward'. 

The LITERAL argument is passed to `replace-match' during replacement. 

If `case-replace' is nil, do not alter case of replacement text." 
    (apply-on-rectangle 
    (lambda (start-col end-col search-function search-pattern replacement) 
    (move-to-column start-col) 
    (let ((bound (min (+ (point) (- end-col start-col)) 
         (line-end-position))) 
      (fixedcase (not case-replace))) 
     (while (funcall search-function search-pattern bound t) 
     (replace-match replacement fixedcase literal)))) 
    start end search-function search-pattern replacement)) 

(defun my-replace-regexp-rectangle-read-args (regexp-flag) 
    "Interactively read arguments for `my-replace-regexp-rectangle' 
or `my-replace-string-rectangle' (depending upon REGEXP-FLAG)." 
    (let ((args (query-replace-read-args 
       (concat "Replace" 
         (if current-prefix-arg " word" "") 
         (if regexp-flag " regexp" " string")) 
       regexp-flag))) 
    (list (region-beginning) (region-end) 
      (nth 0 args) (nth 1 args) (nth 2 args)))) 

(defun my-replace-regexp-rectangle 
    (start end regexp to-string &optional delimited) 
    "Perform a regexp search and replace on each line of a rectangle 
established by START and END (interactively, the marked region), 
similar to `replace-regexp'. 

Optional arg DELIMITED (prefix arg if interactive), if non-nil, means 
replace only matches surrounded by word boundaries. 

If `case-replace' is nil, do not alter case of replacement text." 
    (interactive (my-replace-regexp-rectangle-read-args t)) 
    (when delimited 
    (setq regexp (concat "\\b" regexp "\\b"))) 
    (my-search-replace-in-rectangle 
    start end regexp to-string 're-search-forward nil)) 

(defun my-replace-string-rectangle 
    (start end from-string to-string &optional delimited) 
    "Perform a string search and replace on each line of a rectangle 
established by START and END (interactively, the marked region), 
similar to `replace-string'. 

Optional arg DELIMITED (prefix arg if interactive), if non-nil, means 
replace only matches surrounded by word boundaries. 

If `case-replace' is nil, do not alter case of replacement text." 
    (interactive (my-replace-regexp-rectangle-read-args nil)) 
    (let ((search-function 'search-forward)) 
    (when delimited 
     (setq search-function 're-search-forward 
      from-string (concat "\\b" (regexp-quote from-string) "\\b"))) 
    (my-search-replace-in-rectangle 
    start end from-string to-string search-function t))) 

(global-set-key (kbd "C-x r M-%") 'my-replace-string-rectangle) 
(global-set-key (kbd "C-x r C-M-%") 'my-replace-regexp-rectangle) 
+0

谢谢!不幸的是,我似乎只能在emacs GUI中用'C-RET'标记一个角落,而不是终端模式。 – user545424

+0

这将是由于您的终端不发送该序列。您可能会发现xterm更成功(另请参阅http://stackoverflow.com/questions/4337837/send-c-to-emacs-in-vt100-xterm-terminal-mac-os-xs-terminal)。我还用非CUA方法的更全面的代码更新了我的答案。 – phils

+0

功能请求:搜索矩形。我有一个巨大的专栏,我需要检查它是否有别的,但'DPP'。我使用了一些复杂的正则表达式 - 如果列很大 - 我得到正则表达式堆栈溢出。 – Adobe

0

如果您处于CUA模式,可以使用绑定到'M-r'的cua-replace-in-rectangle。

+0

但是这只会替换事件的第一次出现(默认vim replace-regexp行为)。 – Adobe

3

在最近的Emacs(例如Emacs的25),你可以做到这一点,利用M-%query-replace)或C-M-%query-replace-regexp)的开箱。使用M-x rectangle-mark-mode创建矩形区域。必要时使用C-x C-x,在标记之前放置点。然后只是查询替换。

但是,他们没有在Emacs 25中对replace-string进行相同的更改。你可以这样做,如果你喜欢,只需要以query-replace的方式添加参数region-noncontiguous-p即可。代码很简单:

(defun replace-string (from-string to-string &optional delimited start end backward 
         region-noncontiguous-p) 
    "..." 
    (declare (interactive-only 
      "use `search-forward' and `replace-match' instead.")) 
    (interactive 
    (let ((common 
      (query-replace-read-args 
      (concat "Replace" 
        (if current-prefix-arg 
         (if (eq current-prefix-arg '-) " backward" " word") 
        "") 
        " string" 
        (if (use-region-p) " in region" "")) 
      nil))) 
    (list (nth 0 common) (nth 1 common) (nth 2 common) 
      (if (use-region-p) (region-beginning)) 
      (if (use-region-p) (region-end)) 
      (nth 3 common) 
      (if (use-region-p) (region-noncontiguous-p))))) 
    (perform-replace 
    from-string to-string nil nil delimited nil nil start end backward region-noncontiguous-p)) 

或者,你可以下载库replace+.el并从那里使用的replace-string版本。它做你想要的。

FWIW,我刚刚提交Emacs bug#27897以将此功能添加到replace-string和其他几个命令在同一个库中,replace.el

+0

还有一个anzu bug(可能会替代你的'query-replace'版本):https://github.com/syohex/emacs-anzu/issues/69 –