【问题标题】:How can I delete the current line in Emacs?如何删除 Emacs 中的当前行?
【发布时间】:2010-10-18 11:11:41
【问题描述】:

与 vi 的 dd 等效的 emacs 是什么?我想删除当前行。试过 CTRL + k 但它只会从当前位置删除

【问题讨论】:

    标签: emacs


    【解决方案1】:
    C-a # Go to beginning of line
    C-k # Kill line from current point
    

    还有

    C-S-backspace   # Ctrl-Shift-Backspace
    

    调用M-x kill-whole-line

    如果您想设置不同的全局键绑定,请将其放入 ~/.emacs:

    (global-set-key "\C-cd" 'kill-whole-line)     # Sets `C-c d` to `M-x kill-whole-line`
    

    如果要删除多行整行,可以在命令前加一个数字:

    C-u 5 C-S-backspace    # deletes 5 whole lines
    M-5 C-S-backspace      # deletes 5 whole lines
    
    C-u C-S-backspace      # delete 4 whole lines. C-u without a number defaults to 4
    
    C-u -5 C-S-backspace   # deletes previous 5 whole lines
    M--5 C-S-backspace     # deletes previous 5 whole lines
    

    有时我也觉得C-x z 很有帮助:

    C-S-backspace         # delete 1 whole line
    C-x z                 # repeat last command
    z                     # repeat last command again. 
                          # Press z as many times as you wish. 
                          # Any other key acts normally, and ends the repeat command.
    

    【讨论】:

    • 这行得通。有没有办法使用一个命令来做到这一点?我是否必须创建自定义键绑定才能执行此操作?
    • 不知道C-x z,这真的很酷。顺便说一句,答案很好而准确。
    • 还有C-k C-k,有点像vim中的d$ S-j,但这可以很好地删除向前的行。
    • 为什么C-cd中没有空格?
    • @RenéG:空格是可选的。
    【解决方案2】:

    如果您不想终止该行(这会将其放入操作系统剪贴板并终止环),只需将其删除:

    (defun delete-current-line ()
      "Delete (not kill) the current line."
      (interactive)
      (save-excursion
        (delete-region
         (progn (forward-visible-line 0) (point))
         (progn (forward-visible-line 1) (point)))))
    

    【讨论】:

      【解决方案3】:

      另一种删除线而不将其放入杀伤环的方法:

      (defun delete-current-line ()
        "Deletes the current line"
        (interactive)
        (delete-region
         (line-beginning-position)
         (line-end-position)))
      

      这将把点留在空行的开头。为了摆脱这种情况,您可能希望在函数末尾添加类似(delete-blank-lines) 的内容,如本例所示,这可能不太直观:

      (defun delete-current-line ()
       "Deletes the current line"
       (interactive)
       (forward-line 0)
       (delete-char (- (line-end-position) (point)))
       (delete-blank-lines))
      

      【讨论】:

        【解决方案4】:

        而不是有单独的键来删除行,或者必须调用 前缀参数。您可以使用crux-smart-kill-line 这将“杀死到行尾并在下一行杀死整行 呼叫”。但如果您更喜欢 delete 而不是 kill,您可以使用 代码如下。

        对于点到字符串的操作(kill/delete)我推荐使用zop-to-char

        (defun aza-delete-line ()
          "Delete from current position to end of line without pushing to `kill-ring'."
          (interactive)
          (delete-region (point) (line-end-position)))
        
        (defun aza-delete-whole-line ()
          "Delete whole line without pushing to kill-ring."
          (interactive)
          (delete-region (line-beginning-position) (line-end-position)))
        
        (defun crux-smart-delete-line ()
          "Kill to the end of the line and kill whole line on the next call."
          (interactive)
          (let ((orig-point (point)))
            (move-end-of-line 1)
            (if (= orig-point (point))
                (aza-delete-whole-line)
              (goto-char orig-point)
              (aza-delete-line))))
        

        source

        【讨论】:

          【解决方案5】:

          从任何点删除(杀死)整行的最快/最简单的方法 就行了,不用选择任何东西,就是:

          C-w  ; kill-region
          

          它可以灵活地删除任何选中的内容,或者默认删除一行 如果没有选择任何内容。

          鉴于这个问题,您可能也对复制感兴趣 Vim 的“yank”,yy(尽管用 Emacs 的说法,“yank”令人困惑 Vim 的“放置”,p)。这是:

          M-w  ; kill-ring-save
          

          很好,一致,而且很容易记住。甚至轻微 类似于 Vim 的i_CTRL-W

          一旦你用上述任何一种方法将东西放入杀戮环, 您可能想要“拉动”(粘贴)它:

          M-y  ; yank-pop
          

          (注意 C-S-backspace 在终端 Emacs 中可能不起作用。)

          【讨论】:

            【解决方案6】:

            安装包whole-line-or-region,然后运行(whole-line-or-region-global-mode)。如果没有区域(选择)处于活动状态,这将使C-w 杀死整行。

            查看包的 GitHub 页面https://github.com/purcell/whole-line-or-region

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-03-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多