Skip to content

copy edits to shell-line-editing doc #409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 74 additions & 55 deletions draft/shell-line-editing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@
Interacting with the Mongo Shell
================================


Introduction
------------

The MongoDB shell is a interactive javascript shell interface to MongoDB. It is invoked by running `mongo` or `mongo.exe`.


The MongoDB shell is a interactive javascript shell interface to
MongoDB. It is invoked by running :program:`mongo` or ``mongo.exe``.

Keyboard Shortcuts
------------------

The MongoDB shell tries to follow familiar keyboard shortcuts as found in bash and emacs.
The MongoDB shell tries to follow familiar keyboard shortcuts as found
in bash and emacs.

The following table illustrates the various keystrokes supported by the MongoDB shell:
The following table illustrates the various keystrokes supported by the
MongoDB shell:

You will notice that we try to accommodate multiple keybinding styles and as a result some functions have multiple bindings that will work.
You will notice that MongoDB accommodates multiple keybinding styles,
and as a result some functions have multiple bindings that will work.

=================== =====================================
Keystroke Function
Expand Down Expand Up @@ -70,71 +71,89 @@ Meta-> end-of-history
Custom Prompt
-------------

The shell's prompt can be customized by creating variable 'prompt' in the shell. It can be any arbitrary javascript, including a function that returns a string. This flexibility allows for additional information to be displayed in the prompt.
The shell's prompt can be customized by creating the variable ``prompt``
in the shell. It can be any arbitrary javascript, including a function
that returns a string. This flexibility allows for additional
information to be displayed in the prompt.

The following three examples should give you a good enough idea to get you started.
The following three examples should give you a good enough idea to get
you started.

A prompt that contains the number of commands issued:
.. example::

.. code-block:: javascript
A prompt that contains the number of commands issued:

> cmdCount = 1;
> prompt = function() {
... return (cmdCount++) + "> ";
... }
1> command
2> anothercommand
3>
.. code-block:: javascript

To make the prompt look a bit more familiar, we can make it database@host$:
> cmdCount = 1;
> prompt = function() {
... return (cmdCount++) + "> ";
... }
1> command
2> anothercommand
3>

.. code-block:: javascript
.. example::

> host = db.serverStatus().host; \\ since host should not change
> prompt = function() {
... return db+"@"+host+"$ ";
... }
[email protected]$ use monkeys
switched to db monkeys
[email protected]$
A ``database@host$`` prompt:

You could use the prompt to do a bit of database monitoring as well:
.. code-block:: javascript

.. code-block:: javascript
> host = db.serverStatus().host; \\ since host should not change
> prompt = function() {
... return db+"@"+host+"$ ";
... }
[email protected]$ use monkeys
switched to db monkeys
[email protected]$

.. example::

A prompt that also performs database monitoring:

> prompt = function() {
... return "Uptime:"+db.serverStatus().uptime+" Files:"+db.stats().objects+" > ";
... }
Uptime:5897 Files:6 > db.monkeys.save({name : "James"});
Uptime:5948 Files:7 >
.. code-block:: javascript

> prompt = function() {
... return "Uptime:"+db.serverStatus().uptime+" Files:"+db.stats().objects+" > ";
... }
Uptime:5897 Files:6 > db.monkeys.save({name : "James"});
Uptime:5948 Files:7 >

Using a real editor
Using a Real Editor
-------------------

MongoDB 2.1+ includes the ability to use an external editor. Just run ``edit nameOfVariableOrFunction`` and the MongoDB shell will open whatever editor you have defined in your $EDITOR environment variable. When you are finished editing simply save and exit (:wq in Vim). If you wish to discard your changes, you can either not save or make your editor exit with an error (:cq in Vim or (kill-emacs 1) in Emacs).
MongoDB version 2.1+ includes the ability to use an external editor. To
do so, run ``edit nameOfVariableOrFunction`` and the MongoDB shell will
open whatever editor you have defined in your ``$EDITOR`` environment
variable. When you are finished editing, simply save and exit (``:wq``
in Vim). If you wish to discard your changes, you can either not save or
make your editor exit with an error (``:cq`` in Vim or ``kill-emacs 1``
in Emacs).

.. code-block:: bash

$ EDITOR=vim mongo --nodb
$ EDITOR=vim mongo --nodb

.. code-block:: javascript

MongoDB shell version: 2.1.0
> function f() {}
> edit f
> f
function f() {
print("this really works");
}
> f()
this really works
> o = {}
{ }
> edit o
> o
{ "soDoes" : "this" }
>

.. note::
It is possible that the code in functions will be slightly modified by the JavaScript compiler when you try to edit it again. For example it may convert 1+1 in to 2 and strip out comments. The actual changes will vary based on the version of JavaScript used, but should not effect the semantics of the code, only its appearance.
MongoDB shell version: 2.1.0
> function f() {}
> edit f
> f
function f() {
print("this really works");
}
> f()
this really works
> o = {}
{ }
> edit o
> o
{ "soDoes" : "this" }
>

.. note:: It is possible that the code in functions will be slightly
modified by the JavaScript compiler when you try to edit it again.
For example, it may convert ``1+1`` to ``2`` or strip out comments.
The actual changes will vary based on the version of JavaScript used
but should not affect the semantics of the code, only the appearance.