MongoDB - EDITOR variable - MongoDB shell - Windows 7

mongodb, shell

Solution

The `EDITOR` environment variable works on Windows, but it looks like there are a few factors that affect usability as at MongoDB 2.4.

When you use the `edit` command, the `mongo` shell writes a temporary JavaScript file with the contents of that variable and launches the external editor with this file path. The shell session then waits for the editor to exit and checks the return code.

This doesn't work well if:

the external editor has a multiple-document interface and there are already other documents open

the external editor doesn't return the expected 0 (no errors) return code

Workarounds

Limit yourself to using a single document editor for your `EDITOR` setting. While `notepad.exe` works, there is likely a more capable editor that includes JavaScript syntax highlighting.

If you are developing complex JavaScript functions, you probably want to have those in an external file. You can use `load("/path/to/file.js")` to reload the latest version of a JS file into the shell.

UltraEdit

UltraEdit has some extra user experience wrinkles (I tested with UltraEdit V20):

(works with luck) if you don't have UltraEdit open before you invoke `edit` from the `mongo` shell and don't open any extra tabs, it will work as an external editor. You should be able to successfully use `edit` from the `mongo` shell to launch UltraEdit, edit a variable, and see the results saved when you close UltraEdit. UltraEdit prompts to save the updated JS file in the same location.

(doesn't work) if UltraEdit was already open and has other document tabs, it has different behaviour. The `edit` command from the `mongo` shell will open a new tab, but when you close this tab (to save changes) UltraEdit prompts with a "Save as" dialog. UltraEdit saves the JS file, but apparently doesn't return the expected exit code so the `mongo` shell is unaware that the JS file has been updated and should be reloaded.

EDIT: Thanks to UltraEdit Expert's suggestions, I found a configuration setting that makes UltraEdit work nicely:

- Open the Configuration settings (Advanced > Configuration menu)

- Under Application Layout > Miscellaneous enable the checkbox for the setting Maintain separate process for each file opened from external application

- Restart UltraEdit (the setting doesn't appear to take effect until the next launch)

With the "separate process" setting enabled, you should now be able to use UltraEdit as an external `EDITOR` and see the changes reflected when you close the editing tab opened from the `mongo` shell.

Problem

Is the EDITOR variable feature really working on Windows 7? I am reading a text saying that once we set the EDITOR variable in .mongorc.js we can just type in the shell: edit var_name and var_name will be loaded in the EDITOR (UltraEdit in my case) for editing. Once we're done making the changes, we can just save and exit the editor; then the variable will be parsed and loaded back into the shell. The part in bold italic does not work for me. All it does is offering me to save some js file on my desktop?! Any ideas? EDIT: I noticed that if I set it to Notepad ("notepad.exe"), it works OK. But if I set it to UltraEdit ("uedit32.exe"), it does not work the way one would expect.

Original source