0

Emacs 26.1

I has keybinding

‘C-a C-SPC C-e M-w’ copies the current line without the newline.

But it's to long shorcut.

I want to change it to C-c

So as result when I press C-c to copies the current line without the newline.

Is it possible?

  • 2
    Don't touch C-c! Please read the Keybinding section in the Emacs manual, in particular the keys reserved for users. If you rebind C-c you will lose a host of functionality. The answer below explains that in more detail, but I didn't want a TL;DR visitor to get the wrong impression. – NickD Aug 8 at 15:20
1

Emacs commands are usually interactive function, but keyboard macro (i.e., a string or vector) works too, so you can write:

(global-set-key (kbd "C-o") (kbd "C-a C-SPC C-e M-w"))

An better (more rebust and efficient) option is writing some Lisp code, e.g.,

(defun your-copy-current-line ()
  (interactive)
  (kill-new
   (buffer-substring (line-beginning-position) (line-end-position))))

(global-set-key (kbd "C-o") #'your-copy-current-line)

Regarding to C-c, it is already occupied by default as a prefix key. I beilieve it's possible to unbind the prefix key.

| improve this answer | |
2

Use a keyboard macro. Keyboard macros are explained in chapter 17 of the Emacs manual, which is available online as well as inside emacs (type C-h i and select the Emacs manual).

In your case, you want to start recording the keyboard macro by typing F3, then typing out the keys you want the macro to execute (C-a C-SPC C-e M-w). Finally, end the recording by typing F4.

Now you can replay the macro by hitting F4. But F4 will replay only the most recently recorded macro, so you still want to save this macro for later use.

To do that, first give the macro a name by typing C-x C-k n, and then typing in some name like copy-current-line. Now open your emacs init file (~/.emacs.d/init.el by default on non-windows systems). Now run M-x insert-kbd-macro. It will ask you for the name of the macro you want to insert, but just leave it blank and hit return, so that it inserts the most recent macro. It will insert two lines that look like this:

(fset 'copy-current-line
      (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ([1 67108896 5 134217847] 0 "%d")) arg)))

Finally, add a keybinding by typing in code that looks like this:

(global-set-key (kbd "C-c C-c") 'copy-current-line)

When executed, this will bind your macro to the C-c C-c key sequence. I recommend not binding it to C-c directly, because C-c is normally a prefix key that lets you access a whole keyboard's worth of keyboard shortcuts. It's specifically reserved for users to add their own custom keybindings to, so you should feel free to arrange it however you like. As with all prefix keys, you can see a list of the assigned keyboard shortcuts by typing C-c C-h.

You should also edit the docstring so that it explains what the macro will do when called, so that the documentation for this key will be as useful as possible. (C-h k C-c C-c will pull up that documentation.)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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