I would like to accomplish following

  • Set mark
  • Advance to end of sentence
  • Copy selection -> macro start here
  • Open journal using ^c c
  • Paste copied item
  • Save using C-x C-s
  • Exit using C-c C-c Revert to the buffer from which the word was copied

I recorded this macro. But it fails to work. Kindly help

(fset 'pt "\C-ccj\C-v\C-x \C-s \C-c\C-c")

  • 2
    Are the spaces going to cause problems? It looks like you have C-x <SPC> C-s <SPC> C-c C-c in there to me Nov 28 '18 at 11:33
  • What is the "journal" you're opening? It may be that the macro requires a particular mode to be enabled. Nov 28 '18 at 13:15
  • Instead of writing out a (fset ...) form, try recording the macro in the usual way (say, with <f3> and <f4> ---it's easier to spot typos if you do it "live").
    – Omar
    Nov 29 '18 at 1:24

One way to accomplish what you want is using keyboard macros, as you've done. It's my understanding that keyboard macros simply record the sequence of commands issued and replays them. That approach may run into problems if the current environment isn't identical to the one in which the macro was recorded. And that may be your problem: the macro expects certain conditions to be true which no longer are. Perhaps org-journal-mode isn't enabled?

Unfortunately, the list of things which could be wrong is tremendous. You're likely not to get much of a response because of this.

However, Emacs is basically a REPL. Another way to accomplish your goal is to write your own function by hand. This is a bit more work, but Emacs makes it (relatively) easy for you.

In Emacs, you can see what a particular key does by using describe-key. This is bound to C-h k by default. For example, to see what happens when you press C-c <SPC>, you would do C-h k C-c <SPC>. You would see that it runs set-mark-command. If you wanted to learn more about set-mark-command, you could then use describe-function which is bound to C-h f by default.

Doing this for each of your actions, we get the following:

| Actions to Accomplish      | Key Pressed | Bound command         |
| Set mark                   | C-<SPC>     | set-mark-command      |
| Advance to end of sentence | C-e         | org-end-of-line       |
| Copy selection             | M-w         | kill-ring-save        |
| Open journal               | C-c C-j     | org-journal-new-entry |
| Paste copied item          | C-y         | org-yank              |
| Save buffer                | C-x C-s     | save-buffer           |
| Exit the journal           | ???         | ???                   |

From here, you could craft a function that does specifically what you want. The details about how to do that fill a book...

Fortunately, there's a wonderful book1 called An Introduction to Programming in Emacs Lisp.

I recommend having a go at the problem in this way. Yes, it's a little more effort at first, but understanding how to tackle a program in this manner is the key what makes Emacs so powerful. (It's also fun!) If you get stuck along the way, you can then ask focused questions which are more likely to help you. Happy hacking!

1 This book is often packaged with Emacs and can be accessed it via C-h i.

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.