I use Emacs with Cucumber (cucumber.io). Cucumber outputs lines like this:

Scenario: Jeff returns a faulty microwave              # /somepath/file1.feature:12
    Given Jeff has bought a microwave for $100         # /somepath/steps/file2.rb:5
    And he has a receipt
    When he returns the microwave
    Then Jeff should be refunded $100
          Exception blah
          /path/someotherdir/sourcefile.rb:123:error xxx occured

Scenario: Some other scenario...                       # /somepath/file1.feature:23
    Given ...

Everything works splendidly with the compilation buffers. I have made some entries in compilation-error-regexp-alist that locate those comments as "info" (in addition to whatever compiler/runtime errors from the Ruby environment) and am able to jump through everything.

Now, my question:

I would find it very handy if I could not only step between the "next" (M-n) and "previous" (M-p) steps in a completely linear fashion, but if I could separately do a "next scenario" (C-M-n) and "previous scenario" (C-M-p) binding. Those would ignore everything and just jump between the lines starting with Scenario:.

Is such a thing possible?

1 Answer 1


The shortcut for the next error is C-x ` (backquote, command next-error) and there is no keyboard shortcut for "previous-error".

Going to the next scenario seems totally doable with some keyboard macros or some elisp.

I'd try with macros first:

  • start registering one (F3 or C-x ()
  • go to the compilation buffer,
  • search for the text "Scenario: ", visit it if needed,
  • stop defining the macro (F4 or the closing parenthesis).

You can save it.

An elisp function would be nice though. I'd go with the same process (does it look right ?). I'll use M-x ielm to try things out and I'll search elisp methods in manuals and with the built-in help: C-x h k + a keypress to see the function that is called by this keyboard action.

The result will look grossely like:

(defun cucumber-next-scenario ()
    (interactive) ;; to call it with M-x
    (search-forward "Scenario: ")
    ;; press enter ?

and… that's it ?

edit: function by the OP:

(defun my-previous-scenario () 
  "Jump to the previous cucumber scenario in the compilation buffer" 
  (switch-to-buffer-other-window "*compilation*") 
  (search-backward "Szenario:") 
  (execute-kbd-macro (kbd "<return>")))
  • 1
    There's previous-error in Emacs 25.1. What's your version? In any case, next-error takes an optional ARG. If ARG is negative, it moves backwards. So previous-error would be (next-error -1). Mar 29, 2017 at 22:24
  • Thanks, @Ehvince! In hindsight, creating a macro/defun or a simple search function is quite obvious (including the "press enter" bit, nice touch), I guess my brain was just locked into the next-/previous-error thingy from fiddling with regexpes for recognizing the actual errors too long. ;) (Oh, btw, I just mentioned the keyboard shortcuts for regular next/previous to be able to illustrate that I'd like "more" keys, was maybe a bit confusing to write them with my own binding instead of the default one).
    – AnoE
    Mar 29, 2017 at 22:34
  • 1
    In Emacs 25.1, M-g M-n and M-g M-p are bound to next-error and previous-error, respectively.
    – npostavs
    Mar 29, 2017 at 22:41
  • @TianxiangXiong oops you are right, there is a previous-error on 24.3 also (edited), only no built-in keyboard shortcut. Glad they appear on Emacs 25.
    – Ehvince
    Mar 30, 2017 at 8:33
  • @Ehvince, I now ended up with this, which works well (including when failing, i.e., buffer or search string not found): (defun my-previous-scenario () (interactive) "Jump to the previous cucumber scenario in the compilation buffer" (switch-to-buffer-other-window "*compilation*") (search-backward "Szenario:") (execute-kbd-macro (kbd "<return>"))) (similar for the "next" function) - feel free to edit it into your answer.
    – AnoE
    Mar 30, 2017 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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