tags:

views:

176

answers:

3

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 keybindings and macros.

A few questions about this customization:

  1. How would I make a macro for #1?
  2. Are these good keybindings (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 Ctrl-Tab macro that will act like Alt-Tab in Linux/Windows? Specifically, I want it 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.
+1  A: 
  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.

Bozhidar Batsov
A: 
  1. I wouldn't make a macro for that but write a function like someone else posted on this page. Instead of (cua-paste nil) you could also use (yank). I'm not sure which one's better and why.
  2. I don't like them that much. For things that I use often I'd like to do as little finger acrobatics as possible, so that would mean modifier+key instead of modifier1+modifier2+key.. or use a function key if you don't feel tied to the homerow.
  3. no comment
aerique
+3  A: 

Have you tried using vertically or horizontally split windows for this? (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 time, 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, stop the macro.

So it would be something like:

C-x ( C-x b NEW 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 the pointer where you want to macro definition to go, then you type:

M-x insert-kbd-macro

Now you can run you macro using its name by 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 too. I guess you'd have to save the buffer to a file manually.

Macros: http://www.emacswiki.org/emacs/KeyboardMacros

Possibly useful: Swap text between buffers - http://www.gnu.org/software/emacs/elisp/html_node/Swapping-Text.html#Swapping-Text

Peter Ajtai