Shells in shells

Working in multiple directories

While it’s common to work in one directory, it can be handy to work in multiple places at the same time. For this, Nu offers the concept of “shells”. As the name implies, they’re a way of running multiple shells in one, allowing you to quickly jump between working directories and more.

To get started, let’s enter a directory:

  1. /home/jonathant/Source/nushell(main)> enter ../book
  2. /home/jonathant/Source/book(main)> ls
  3. ────┬────────────────────┬──────┬────────┬─────────────
  4. # │ name │ type │ size │ modified
  5. ────┼────────────────────┼──────┼────────┼─────────────
  6. 0 404.html File 429 B 2 hours ago
  7. 1 CONTRIBUTING.md File 955 B 2 hours ago
  8. 2 Gemfile File 1.1 KB 2 hours ago
  9. 3 Gemfile.lock File 6.9 KB 2 hours ago

Entering is similar to changing directories (as we saw with the cd command). This allows you to jump into a directory to work in it. Instead of changing the directory, we now are in two directories. To see this more clearly, we can use the shells command to list the current directories we have active:

  1. /home/jonathan/Source/book(main)> shells
  2. ───┬────────┬────────────┬─────────────────────────
  3. # │ active │ name │ path
  4. ───┼────────┼────────────┼─────────────────────────
  5. 0 false filesystem /home/jt/Source/nushell
  6. 1 true filesystem /home/jt/Source/book
  7. 2 false filesystem /home/jt/Source/music
  8. ───┴────────┴────────────┴─────────────────────────

The shells command shows us there are three shells currently active: our original “nushell” source directory and now this new “book” directory.

We can jump between these shells with the n, p and g shortcuts, short for “next”, “previous” and “goto”:

  1. /home/jonathant/Source/book(main)> n
  2. /home/jonathant/Source/nushell(main)> p
  3. /home/jonathant/Source/book(main)> g 2
  4. /home/jonathant/Source/music(main)>

We can see the directory changing, but we’re always able to get back to a previous directory we were working on. This allows us to work in multiple directories in the same session.

Exiting the shell

You can leave a shell you have entered using the exit command. If this is the last open shell, Nu will quit.

You can always quit Nu, even if multiple shells are active by passing the --now flag to the exit command. Like so: exit --now