peter.petrov
peter.petrov

Reputation: 39477

MongoDB - EDITOR variable - MongoDB shell - Windows 7

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.

Upvotes: 3

Views: 2359

Answers (3)

Stennie
Stennie

Reputation: 65363

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.

Upvotes: 4

Mofi
Mofi

Reputation: 49127

My answer is an addition on what Stennie wrote already above which was too long to add it as a comment.

It looks like mongo determines file change not by watching the file status (file size, file dates, file attributes) of the file passed to the called editor, but the execution state of the called editor application.

UltraEdit is configured by default to open all files in same instance. Therefore every call of uedit32.exe with one or more file names on command line results in starting a new instance of UltraEdit which loads the configuration settings, detects that a single instance should be used, and searches next in process list for an already running instance of UltraEdit. If there is already another instance of UltraEdit, the just started instance passes the names of the files to open to the other instance of UltraEdit and then terminates itself.

This behavior looks like nothing was changed on the file within the called editor for mongo.

There are several possibilities to change this behavior so that an instance of UltraEdit is always used when uedit32.exe is called by mongo with a file to edit.

First, there is the command line option /fni which means "force new instance". If this command line option is used in the editor's definition as first parameter after uedit32.exe, the new instance of UltraEdit created by the call of mongo never looks for an already running instance and opens all files on its command line in its own instance.

Second, the configuration item Miscellaneous in branch Application Layout of the Configuration in menu Advanced could be opened and the setting Allow multiple instances could be checked to work by default with multiple instances of UltraEdit.

Third, with setting Allow multiple instances not enabled, the setting Maintain separate process for each file opened from external application could be checked available since UltraEdit v18.10 to get UltraEdit working as editor for mongo.

Upvotes: 2

Neil Lunn
Neil Lunn

Reputation: 151132

Most of the documentation is naturally very Unix variant specific. This will be apparent in the documentation itself.

In your case, setting the variable in mongorc.js will provide something so launch on the edit command helper, but the result will not be returned as it would in the Unix way.

Upvotes: 1

Related Questions