frostbite
frostbite

Reputation: 648

Does the mongo shell support vi editing?

I need to carry out some very tedious command line ops on my mongodb. Has anyone tried setting the shell editing to vi? Would be very useful if I can search my command history and change/replace.

Upvotes: 6

Views: 2827

Answers (1)

Adam Comerford
Adam Comerford

Reputation: 21692

You can set the EDITOR environment variable to point to vi/vim and MongoDB will use it when you issue the edit shell command, or you can use the same EDITOR variable in javascript to override the environment value. Hence you can add the setting to your .mongorc.js file to ensure it is set as you wish each time without manual intervention.

Example from the mongo shell:

> EDITOR='/usr/bin/vim'
/usr/bin/vim
> db
test
> edit db

Which fired up vim for me as follows (abbreviated output for brevity):

  1 test
~                                                                               
~                                                                               
~                                                                               
"/tmp/mongo_edit1449005704.js" [noeol] 1L, 4C

In terms of your command history, that is kept in the file ~/.dbshell (as long as you are using something newer than 2.2.0) and can be edited directly.

Something to note: you can use Ctrl-r to reverse search and edit on the command line (hit Ctrl-r again to go one match further back, Ctrl-s to go forward etc.), just like you can in modern BASH shells. For a full list of the keyboard shortcuts, have a look here.

Upvotes: 7

Related Questions