Answers:
与贾斯汀的建议类似,CM-SPACE为您提供“ mark-sexp”,它将选择平衡对等,报价等,然后您可以执行Cw或其他操作使其消失。如果要在删除之前先查看要删除的内容...
是! Emacs中的VIM ci“命令等效于... ci” :-)
http://www.emacswiki.org/emacs-de/Vimpulse
只是偶然发现了这个问题;这是为我工作的自定义解决方案:
(defun seek-backward-to-char (chr)
"Seek backwards to a character"
(interactive "cSeek back to char: ")
(while (not (= (char-after) chr))
(forward-char -1)))
(defun delete-between-pair (char)
"Delete in between the given pair"
(interactive "cDelete between char: ")
(seek-backward-to-char char)
(forward-char 1)
(zap-to-char 1 char)
(insert char)
(forward-char -1))
然后将delete-between-pair绑定到您喜欢的任何键。对我来说,我将它束缚在Cz i上。
恐怕我不了解VIM的ci功能,但是您是否看过Emacs regexp replace?我无法说出确切的语义或比较中使用起来多么容易,但这就是我想要的东西。
Magnars(EmacsRocks网站的作者)编写了此插件,可以完全按照您的要求进行操作。
https://github.com/magnars/change-inner.el
显然,您也可以使用Evil模式。
这是我的版本,该版本将删除(或包括)匹配的一对字符内的所有内容。字符对在列表中定义,以便知道匹配的开始/结束字符。我已将其映射为“ Cc i”以进行更改,并将其映射为“ Cc a”以进行所有更改。
它还会将删除的字符复制到剪贴板,以便以后粘贴。
; Re-create ci" ca"...
(defun seek-backward-to-char (chr)
"Seek backwards to a character"
(interactive "cSeek back to char: ")
(while (not (= (char-after) chr))
(forward-char -1)))
(setq char-pairs
'(( ?\" . ?\" )
( ?\' . ?\' )
( ?\( . ?\) )
( ?\[ . ?\] )
( ?\{ . ?\} )
( ?< . ?> )))
(defun get-char-pair (chr)
(let ((result ()))
(dolist (x char-pairs)
(setq start (car x))
(setq end (cdr x))
(when (or (= chr start) (= chr end))
(setq result x)))
result))
(defun get-start-char (chr)
(car (get-char-pair chr)))
(defun get-end-char (chr)
(cdr (get-char-pair chr)))
(defun seek-to-matching-char (start end count)
(while (> count 0)
(if (= (following-char) end)
(setq count (- count 1))
(if (= (following-char) start)
(setq count (+ count 1))))
(forward-char 1)))
(defun seek-backward-to-matching-char (start end count)
(if (= (following-char) end)
(forward-char -1))
(while (> count 0)
(if (= (following-char) start)
(setq count (- count 1))
(if (= (following-char) end)
(setq count (+ count 1))))
(if (> count 0)
(forward-char -1))))
(defun delete-between-pair (char)
"Delete in between the given pair"
(interactive "cDelete between char: ")
(seek-backward-to-matching-char (get-start-char char) (get-end-char char) 1)
(forward-char 1)
(setq mark (point))
(seek-to-matching-char (get-start-char char) (get-end-char char) 1)
(forward-char -1)
(kill-region mark (point)))
(defun delete-all-pair (char)
"Delete in between the given pair and the characters"
(interactive "cDelete all char: ")
(seek-backward-to-matching-char (get-start-char char) (get-end-char char) 1)
(setq mark (point))
(forward-char 1)
(seek-to-matching-char (get-start-char char) (get-end-char char) 1)
(kill-region mark (point)))
(global-set-key (kbd "C-c i") 'delete-between-pair)
(global-set-key (kbd "C-c a") 'delete-all-pair)
这是我在Vim中所缺少的,并且zap-to-char
似乎并没有正确地做到这一点。
这是我谦虚地尝试重新创建“ ci”和“ ca”的尝试:
(defun change-outer (str)
(interactive "sChange outer: ")
(condition-case nil
(search-backward str (line-beginning-position))
(error (search-forward str (line-end-position))
(forward-char -1)))
(kill-sexp)
)
(defun change-inner (str)
(interactive "sChange inner: ")
(condition-case nil
(search-backward str (line-beginning-position))
(error (search-forward str (line-end-position))
(forward-char -1)))
(push-mark)
(forward-sexp)
(forward-char -1)
(exchange-point-and-mark)
(forward-char 1)
(kill-region (point) (mark))
)
通常情况下,条件情况不是必需的,因为search-forward / search-backward的第三个(可选)参数旨在指示在搜索失败的情况下该怎么做。但是由于某种原因,将第二个搜索作为第一个搜索的第三个参数会产生奇怪的行为。
我在这里尝试了解决方案,但是发现每个解决方案都需要某种方式,因此我想到了这一点。它接受开始或结束定界符,并使用内置的Emacs函数避免为定界符使用转换表。
(defun change-inner (prefix character)
"Kill region inside delimiters, using either beginning or
ending delimiter. With prefix arg, kill including delimiters."
(interactive "p\nc")
(let ((initial-point (point))
(start)
(end)
(move-point-by (if (> prefix 1) 0 1)))
(condition-case nil
(progn
;; Search forward for given char
(search-forward (char-to-string character))
(setq end (- (point) move-point-by))
(condition-case nil
(backward-sexp)
(error (backward-list)))
(setq start (+ (point) move-point-by))
(kill-region start end)
(or prefix (forward-char)))
(error (progn
;; Reset and search backward for given char
(goto-char initial-point)
(search-backward (char-to-string character))
(setq start (+ (point) move-point-by))
(condition-case nil
(forward-list)
(error (forward-sexp))))
(setq end (- (point) move-point-by))
(kill-region start end)
(or prefix (backward-char))))))
(global-set-key (kbd "M-i") 'change-inner)
(defun change-outer ()
(interactive)
(let ((current-prefix-arg '(4)))
(call-interactively 'change-inner)))
(global-set-key (kbd "M-o") 'change-outer)