1

From time to time I am using gdb through the gud Emacs interface to debug my C programs. Nevertheless, I find extremely annoying to not be able to browse the gdb command history by just using the Up and Down keys. In place of this behavior, the cursor can move anywhere in the emacs buffer even outside of the regular gdb prompt (which I find totally useless for my purpose).

Is there a way to force gud to take the Up and Down keys as get backward and forward in the command history. And, is there a way to prevent the cursor to move away from the gdb prompt ?

My idea would be to have a behavior similar to the M-x term terminal, but for gdb and having the benefit of gud.

  • 2
    Does M-p and M-n work? – wasamasa Oct 21 '15 at 16:42
  • Damn... that was so simple... I feel ashamed now !... Yes, it works exactly as I wanted... – perror Oct 21 '15 at 16:43
  • 1
    You can still rebind <up> and <down> to do the same things as M-p and M-n. Preventing the cursor to move away from the prompt is going to be tricky or impossible considering there's the usecase of selecting something outside it which needs to move point as it makes up the region. Would you still like me to turn the solution to this into an answer? – wasamasa Oct 21 '15 at 17:11
  • Yes, definitely. What you describe would fullfill 95% of my need. Preventing the cursor to move is just something that is not useful to me, not really something that bother me. – perror Oct 21 '15 at 19:17
4

Emacs does generally use C-p and C-n for moving the cursor up and down, <up> and <down> are usually bound to the same command. That's why you get the unfamiliar behaviour of the arrow keys not cycling the history. As a compromise M-p and M-n are bound in these contexts to history cycling commands. The only exception I'm aware of would be term-mode which captures nearly all of your input and passes it on to the application.

You can of course adjust the respective keymaps to get the familiar behaviour back, but be warned that this will prevent you from moving the cursor elsewhere with the keyboard (the mouse can still be used to position it anywhere though). The general approach to this is finding out what commands are bound to the respective keys (F1 k <key sequence>) and what keymap they belong to (Emacs 25 provides this in the output of F1 k, but as I'm not using that, I jump to the sources with the link present in the F1 k output and search these). Doing so reveals that the commands are previous-line/next-line and comint-previous-input/comint-next-input, the latter two being bound in comint-mode-map. Therefore, the following suffices:

(eval-after-load 'comint
  '(progn
    (define-key comint-mode-map (kbd "<up>") 'comint-previous-input)
    (define-key comint-mode-map (kbd "C-p") 'comint-previous-input)
    (define-key comint-mode-map (kbd "<down>") 'comint-next-input)
    (define-key comint-mode-map (kbd "C-n") 'comint-previous-input)))

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.