Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Emacs keybindings

I have two operations that I do all the time in Emacs:

  1. Create a new buffer and paste the clipboard. C-S-n
  2. Close the current buffer. C-S-w
  3. Switch to the last viewed buffer. C-TAB

I feel like a keyboard acrobat when doing the first two operations. I think it would be worth trying some custom key bindings and macros.

A few questions about this customization:

  1. How would I make a macro for #1?
  2. Are these good key bindings? (I know this is a bit subjective, but they might be used by something popular that I don't use.)
  3. Has anyone found a C-TAB macro that will act like Alt+Tab in Linux/Windows? Specifically, I want to have a stack of buffers according to the last viewed timestamp (most recent on top). I want to continue cycling through the stack until I let go of the Ctrl key. When the Ctrl key is released, I want the current buffer to get an updated position on the stack.
like image 865
User1 Avatar asked May 27 '10 15:05

User1


2 Answers

Have you tried using vertically or horizontally split windows for this (via C-x 3 or C-x 2)? It seems like it would give you fewer steps - even if you implement something like you're talking about.

I find split windows really speed up copy and pasting operations. I use the arrow keys on my num pad to switch among windows (windmove-left/-right/-up/-down), so it's only one key to press and you go to the window you want.

I guess this is a little different from what you're asking for, but it sounds like it might help speed things along a bit.

C-x left and C-x right cycle through buffers, but you have to hit it multiple times, you can't just keep the key pressed down.


For creating a macro for #1, you just start a macro, hit the keys you usually do to create a new buffer, and stop the macro.

So it would be something like:

C-x ( C-x b NEW RET C-x )

You can then save NEW to a file once you're done pasting, so you can use the macro again to create a new buffer. C-x e to try out the macro. If it works you can save it into your init.el file. This is done with:

M-x name-last-kbd-macro

Then you'll get a prompt to enter the name of your choice. This is only good for the current session. Then you save the named macro to your initialization file. First you open your .emacs or init.el file. Then you place point where you want the macro definition to go, then you type:

M-x insert-kbd-macro

Now you can run your macro using its name via M-x <macroname> . You can bind your macro to keys too (in your .emacs or init.el file):

(global-set-key (kbd "C-c a") '<macroname>)

For example this is how your init.el would look after creating a macro that opens a new buffer called NEW that is not associated with a file and binding this macro to C-c n:

;; Creates a new unassociated buffer called NEW
(fset 'new-buffer "\C-xbNEW\C-m");
;; Shortcut for new-buffer
(global-set-key (kbd "C-c n") 'new-buffer)

You can also throw in the paste, buffer close, and buffer switching operations. I guess you'd have to save the buffer to a file manually.

Some resources

  • Information about macros on EmacsWiki

  • Possibly useful: Swap text between buffers

like image 122
Peter Ajtai Avatar answered Nov 03 '22 22:11

Peter Ajtai


  1. start by invoking start-kbd-macro, finish by with end-kbd-macro. Afterwards you may immediately test the new macro with call-last-kbd-macro. If you're happy with the result you might want to save the macro.
  2. Emacs generally doesn't use C-S keybindings and they are easy to use, so I'd call them good. They might cause problems if you're using the terminal version of Emacs, but I assume that's not the case with you.
  3. I use this simple snippet:

    (global-set-key (kbd "<C-tab>") 'bury-buffer)

bury-buffer basically makes the current buffer the last in the buffer-list so you'll be able to cycle buffers in a predictable order.

like image 41
Bozhidar Batsov Avatar answered Nov 03 '22 22:11

Bozhidar Batsov