I'm using the slick-cut function (as answered here by itsjeyd) for killing the entire line with minimal keystrokes. It's defined like so :-

(defun slick-cut (beg end)
   (if mark-active
       (list (region-beginning) (region-end))
     (list (line-beginning-position) (line-beginning-position 2)))))

(advice-add 'kill-region :before #'slick-cut)

But, I noticed this behaviour, whenever I kill a line, the cursor will be positioned to the beginning of the next line.

Before killing line 28 :- before killing line 28 After killing line 28 :- After killing line 28 Is there a way one can modify the slick-cut function so that, after killing a line, the cursor column position is also preserved. Like so :- enter image description here

If unable to preserve cursor position, is it possible to at least move the cursor position to the indentation level ?

  • Try wrapping the if block in save-excursion? Commented Jul 19, 2016 at 13:15

2 Answers 2


Here's a version that does what you want. Just bind it to a key.

(defun xah-cut-line-or-region ()
  "Cut current line, or text selection.
When `universal-argument' is called first, cut whole buffer (respects `narrow-to-region').

URL `http://ergoemacs.org/emacs/emacs_copy_cut_current_line.html'
Version 2016-07-29-stackoverflow"
  (if current-prefix-arg
      (progn ; not using kill-region because we don't want to include previous kill
        (kill-new (buffer-string))
        (delete-region (point-min) (point-max)))
    (if (use-region-p)
        (kill-region (region-beginning) (region-end) t)      
        (kill-region (line-beginning-position) (line-beginning-position 2))

Notice the (back-to-indentation) in the end. That does what you want. Thanks to @kaushalmodi for suggesting this.

  • 1
    You can also just use (back-to-indentation) instead. That fn does exactly that: Move point to the first non-whitespace character on this line. Commented Jul 19, 2016 at 15:32
  • @KaushalModi thanks. I change it to your suggestion.
    – Xah Lee
    Commented Jul 19, 2016 at 15:50
  • reposted with full code. I realized we can't just add the back-to-indentation to advice...
    – Xah Lee
    Commented Jul 19, 2016 at 16:00
  • 2
    @XahLee why can't we add back-to-indentation to slick-cut ? and what is the difference between xah-cut and slick-cut functions? I haven't any elisp yet. So, sorry for asking so many questions. Commented Jul 19, 2016 at 18:22
  • @ChakravarthyRaghunandan good question. slick-cut is based on emacs's “advice” feature. It determines the region, then calls kill-region via the advice mechanism. I think you can make it work by another post-command advice. But I personally avoid using advice, prefer separate function for simplicity. Thanks for asking.
    – Xah Lee
    Commented Jul 19, 2016 at 23:42

Here's a version that remembers column position on kill whole line.

(defun sto/kill-whole-line ()
  "Kill whole line but retain cursor position instead of moving to start of next line."
  (let ((n (current-column)))
    ;; do not move-to-column if that will add whitespaces at the end of line
    (if (< n (- (line-end-position) (line-beginning-position)))
      (move-to-column n t)

(defun sto/kill-line-or-region ()
  "If no region is selected kill the current line otherwise kill region."
  (if (use-region-p)
      (kill-region (region-beginning) (region-end) t)

(global-set-key (kbd "C-S-k") 'sto/kill-line-or-region)

  • 1
    Using setq without declaring variables with let clobbers the global scope. Can you improve your code, and add how to use it (how to bind it to a key)?
    – user12563
    Commented Apr 21, 2019 at 11:30
  • @user12563 Added changes
    – rofrol
    Commented Aug 14, 2022 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.