The Lone C++ Coder's Blog

The Lone C++ Coder's Blog

The continued diary of an experienced C++ programmer. Thoughts on C++ and other languages I play with, Emacs, functional, non functional and sometimes non-functioning programming.

Timo Geusch

2-Minute Read

I’m spending a lot of time in the MongoDB shell at the moment, so of course I went to see if someone had built an Emacs mode to support the MongoDB shell. Google very quickly pointed me at endofunky’s inf-mongo mode, which implements a basic shell interaction mode with MongoDB using comint. We have a winner, well, almost. The mode does exactly what it says on the tin, but I wanted a little more, namely being able to scroll through my command history. Other repl modes like Cider have this functionality already, so it couldn’t be too hard to implement, could it?

Turns out that it’s really easy, because inf-mongo is built on comint, and comint already provides the commands comint-previous-input and comint-next-input. Armed with those commands, all it takes to support scrolling through the command history is to add a couple of key mappings to the sparse mode map for inf-mongo, with the resulting mode map looking like this:

(defvar inf-mongo-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "C-x C-e")  'mongo-send-last-sexp)
    (define-key map (kbd "<M-up>")   'comint-previous-input)
    (define-key map (kbd "<M-down>") 'comint-next-input)

With the above map in place, scrolling through the commands in the current session is enabled. I’m going to send a pull request to endofunky, but for right now a fork with the above change in place can be also found here.

Disclaimer: While I work for MongoDB as a Consulting Engineer, this is my personal blog. All posts in here are my own and only reflect my opinion and not the opinion of my employer.

Recent Posts



A developer's journey. Still trying to figure out this software thing after several decades.