Emacs: Copy/Cut All or Selection

By Xah Lee. Date: . Last updated: .

This page shows you emacs commands to copy/cut whole buffer, or text text selection if there's one.

Copy All or Text Selection

(defun xah-copy-all-or-region ()
  "Put the whole buffer content to `kill-ring', or text selection if there's one.
Respects `narrow-to-region'.
URL `http://ergoemacs.org/emacs/emacs_copy_cut_all_or_region.html'
Version 2015-08-22"
  (interactive)
  (if (use-region-p)
      (progn
        (kill-new (buffer-substring (region-beginning) (region-end)))
        (message "Text selection copied."))
    (progn
      (kill-new (buffer-string))
      (message "Buffer content copied."))))

This command saves you a step of doing selecting whole buffer first.

You should define a key for it. [see Emacs: How to Define Keys]

Cut All or Text Selection

(defun xah-cut-all-or-region ()
  "Cut the whole buffer content to `kill-ring', or text selection if there's one.
Respects `narrow-to-region'.
URL `http://ergoemacs.org/emacs/emacs_copy_cut_all_or_region.html'
Version 2015-08-22"
  (interactive)
  (if (use-region-p)
      (progn
        (kill-new (buffer-substring (region-beginning) (region-end)))
        (delete-region (region-beginning) (region-end)))
    (progn
      (kill-new (buffer-string))
      (delete-region (point-min) (point-max)))))

Copy/Paste Topic

  1. Copy/Paste and kill-ring History
  2. Copy/Cut Current Line If No Selection
  3. Copy/Cut All or Selection
  4. Paste or Paste Previous
  5. Show kill-ring
  6. Copy to Register
  7. 1 Key to Copy/Paste from Register
  8. Copy Append
  9. Copy Rectangle Region to kill-ring
  10. Sync Copy/Paste Clipboards with Linux X11

Enhanced Editing Commands Topic

  1. Select Line, between Quotes, Extend Selection
  2. Copy/Cut Current Line If No Selection
  3. Copy/Cut All or Selection
  4. Paste or Paste Previous
  5. Delete Whitespace around Cursor
  6. Move Cursor to Beginning of Line/Paragraph
  7. Move Cursor by Text Block
  8. Move Cursor to Bracket/Quote
  9. Move Cursor to Punctuation
  10. Delete Text Block
  11. Toggle Comment Current Line
  12. Insert Date Time
Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

If you have a question, put $5 at patreon and message me.