How to copy/select a whole file or buffer in Emacs?
EmacsKeyboard ShortcutsSelectionEmacs Problem Overview
Is there any command to select the whole file contents in Emacs?
For example, Control+a selects whole contents of a file in Notepad, Notepad++, etc.
I can select whole contents using the mouse, but it's inconvenient for large files. I found the basic Emacs commands here and here, but could not find what I am looking for.
Emacs Solutions
Solution 1 - Emacs
C-x h will select the entire buffer.
You can search for help within Emacs using the built-in help system.
C-h f will look for help for specific functions. In this case, you could have searched for whole-buffer
to find mark-whole-buffer
.
Other help commands of interest would be:
- C-h m to show available commands for the current modes
- C-h v for help related to variables
- C-h k to find which functions keys are bound to
- C-h w to see which key bindings are defined for a given function
- C-h ? to see which other options are available.
Note that C-x h will only highlight all the text. To actually copy the selected text, you must use C-w for cut (kill-region
) or M-w for copy (kill-ring-save
).
Solution 2 - Emacs
I use CUA, so I didn't like mark-whole-buffer
because it doesn't use a temporary region.
After much messing about, I finally achieved this using a keyboard macro saved to a function:
- Define a macro which selects the whole buffer
- Run
kmacro-name-last-macro
to name the macro - Use
insert-kbd-macro
to script it out - Now you can copy it into your config & map it to a key, like
C-a
Solution 3 - Emacs
Copying and selecting are related, but distinct, actions. A file and a buffer are also related, but distinct.
To copy a buffer, consider using jac.el. It handles both the "copying" and dealing with modes.