53

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.

smonff
  • 3,399
  • 3
  • 36
  • 46
Sajib Mahmood
  • 3,382
  • 3
  • 37
  • 50
  • 1
    related: http://stackoverflow.com/questions/4886745/emacs-what-is-the-shortcut-key-to-clear-buffer/4887184#4887184 – Tyler Mar 20 '14 at 20:53

3 Answers3

94

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).

keelerm
  • 2,873
  • 20
  • 12
  • 3
    I tried C-x h but it's not working. It puts the cursor in the beginning of the file and says mark set. But if I try to paste then it does not paste the file contents. – Sajib Mahmood Mar 20 '14 at 20:51
  • 4
    You probably don't have transient-mark-mode enabled. Do M-x tranient-mark-mode. This will show your selected region. Once you do C-x h, make sure you are copying with C-w. – keelerm Mar 20 '14 at 20:52
  • Thanks. It's working after I did M-x transient-mark-mode. Thanks again for the quick response. – Sajib Mahmood Mar 20 '14 at 20:58
  • 3
    Sajib Mahmood: Note that `C-x h` should still work regardless of that mode; you just won't *see* the marked region. So `C-x h M-w` should always copy the buffer's contents. Also, `transient-mark-mode` is enabled by default, so you may want to figure out why it's disabled in your config. – phils Mar 20 '14 at 22:08
  • You are right, @phils. C-x h and then M-w copies the content. Thanks. Without the transient mode it was not showing that it has selected the whole file. I'll look into my configuration. – Sajib Mahmood Mar 20 '14 at 22:48
  • @phils, transient-mark-mode is enabled by default from GNU Emacs 23 and onwards. I was using GNU Emacs 21.4.1. May be that's why it was not enabled by default. – Sajib Mahmood Mar 20 '14 at 22:57
  • 3
    Good point. Also, 21.4 is nine years old. Updating would be well worth considering. – phils Mar 20 '14 at 23:00
  • I made a binding for that: (global-set-key (kbd "C-x C-a") 'mark-whole-buffer) – lmedinas Mar 22 '14 at 16:14
  • When I type `C-x h` I only get a right help file `Key translations Starting With C-x:` and not the mark all in the text. What do I do wrong? As a workaround, I had to mark everything with the mouse now. – questionto42 Dec 28 '21 at 16:53
0

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
muhmud
  • 4,474
  • 2
  • 15
  • 22
0

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.

dat
  • 1,580
  • 1
  • 21
  • 30