首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Emacs:删除一行而不发送到杀死环

Emacs:删除一行而不发送到杀死环
EN

Unix & Linux用户
提问于 2011-12-08 20:06:58
回答 3查看 2.2K关注 0票数 6

我想使用C-k删除一行,而不将其发送到kill-ring

我的.emacs文件中有以下内容

代码语言:javascript
复制
(delete-selection-mode 1)

但这似乎只适用于C-d (delete-char)

我还读过这个线程中描述的解决方案:如何删除文本而不杀环?,但是我没有看到任何解决这个精确问题的方法。

EN

回答 3

Unix & Linux用户

回答已采纳

发布于 2011-12-09 01:29:42

代码语言:javascript
复制
(defun delete-line (&optional arg)
  (interactive "P")
  (flet ((kill-region (begin end)
                      (delete-region begin end)))
    (kill-line arg)))

也许这不是最好的解决方案,但它似乎有效。您可能需要将“删除行”绑定到某个全局键,例如

代码语言:javascript
复制
(global-set-key [(control shift ?k)] 'delete-line)
票数 4
EN

Unix & Linux用户

发布于 2014-06-11 12:13:29

辛斯克的回答对我来说并不适用于emacs 24。

但这确实做到了:

代码语言:javascript
复制
;; Ctrl-K with no kill
(defun delete-line-no-kill ()
  (interactive)
  (delete-region
   (point)
   (save-excursion (move-end-of-line 1) (point)))
  (delete-char 1)
)
(global-set-key (kbd "C-k") 'delete-line-no-kill)
票数 3
EN

Unix & Linux用户

发布于 2018-06-08 15:02:54

我遵循的方法是重写kill-line以使用delete-region而不是kill-regionkill-regiondelete-region的功能几乎相同。最大的不同是前者保存了在杀死环中删除的内容。后者没有。用这个替换来重写函数保留了kill-line的确切行为,没有任何副作用。

代码语言:javascript
复制
(defun my/kill-line (&optional arg)
  "Delete the rest of the current line; if no nonblanks there, delete thru newline.
With prefix argument ARG, delete that many lines from point.
Negative arguments delete lines backward.
With zero argument, delete the text before point on the current line.

When calling from a program, nil means \"no arg\",
a number counts as a prefix arg.

If `show-trailing-whitespace' is non-nil, this command will just
delete the rest of the current line, even if there are no nonblanks
there.

If option `kill-whole-line' is non-nil, then this command deletes the whole line
including its terminating newline, when used at the beginning of a line
with no argument.

If the buffer is read-only, Emacs will beep and refrain from deleting
the line."
  (interactive "P")
  (delete-region
   (point)
   (progn
     (if arg
         (forward-visible-line (prefix-numeric-value arg))
       (if (eobp)
           (signal 'end-of-buffer nil))
       (let ((end
              (save-excursion
                (end-of-visible-line) (point))))
         (if (or (save-excursion
                   ;; If trailing whitespace is visible,
                   ;; don't treat it as nothing.
                   (unless show-trailing-whitespace
                     (skip-chars-forward " \t" end))
                   (= (point) end))
                 (and kill-whole-line (bolp)))
             (forward-visible-line 1)
           (goto-char end))))
     (point))))

(global-set-key (kbd "C-k") 'my/kill-line)
票数 1
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/26360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档