Tip 25Restarting Terminal Processes When Resuming a Session
images/neovim-only.png

If you’re running Neovim, you might also want your session file to record details of which processes were running in terminal buffers. By taking care to name your terminal buffers appropriately, you can make your processes resume when you restore a session.

Preparation

The source code that accompanies this book includes a webapp directory. Switch to that directory and install the dependencies:

=> $ cd code/webapp
=> $ npm install

This directory contains a simple web server. You can start the server by running:

=> $ npm run server

You’ll also find a small test suite, which you can run with:

=> $ npm run test

We’ll use these commands to illustrate the examples in this tip, so make sure you can run them before you proceed.

Saving a Session Captures Terminal Buffers by Name

To get started, open the app.js file in Neovim:

=> $ nvim app.js

Now you’re going to use two slightly different methods to start the webserver. First, open a new terminal buffer in a split:

=> :split | terminal

Press i to activate Terminal mode, then start the webserver on port 3001:

=> » PORT=3001 npm run server

Next, you’ll start a second webserver on port 3002 with this one-liner:

=> :split | terminal PORT=3002 npm run server

Side by side, the two terminal buffers look similar. There’s one significant difference I want to draw your attention to: the buffer names. You can list them by running:

=> :ls
<= 1 a "app.js" line 1
 2 #a- "term://.//76529:/usr/local/bin/bash" line 9
 3 %a- "term://.//76606:PORT=3002 npm run server" line 6

These can be generalized as term://{cwd}//{pid}:{cmd}. We can ignore the {cwd} and {pid}, but the {cmd} is significant. In one terminal buffer, the {cmd} is /usr/local/bin/bash, while in the other one it’s PORT=3002 npm run server.

Save the current session then quit:

=> :mksession!
=> :qa!

Then, restart Neovim using the -S flag to restore the session you just recorded:

=> $ nvim -S

You should now see two terminal buffers: one containing a webserver process, and the other containing a bash shell. This doesn’t faithfully reproduce the session you recorded, which had two webserver processes running side by side. So what’s going on here?

When you save a session, the name of each buffer is recorded. In one case, you used :terminal PORT=3002 npm run server to launch the webserver process directly. When the session was restored, Neovim re-created this buffer by running: :edit term://PORT=3002 npm run server, which restarts the webserver process.

In the other case, you used :terminal to start a bash shell, then ran the npm run server command inside of the shell to launch the webserver. When the session was restored, Neovim re-created this buffer by running :edit term:///usr/local/bin/bash, which restarts the bash shell. Commands that were entered in the shell are not recorded in the session, although you may be able to retrieve them from your bash history.

Generally speaking, if you start a process using :terminal {cmd}, the {cmd} process will be restarted when a session is restored. Whereas if you use :terminal to start a shell, restoring your session will restore the shell. It doesn’t matter what commands you executed in the original shell; they won’t be recorded as part of a Vim session.

Renaming a Terminal Buffer

When it comes to saving and restoring sessions, the name of a terminal buffer is significant. You’ll be pleased to learn that you can change the name of a terminal buffer after creating it. To see how this works, let’s set up a new scenario. Open the app.js and test/app-test.js files, each in their own tab pages:

=> $ nvim -p app.js test/app-test.js

In the first tab page, create a new terminal:

=> :split | terminal

Then switch to Terminal mode and run:

=> » PORT=3001 npm run server

Switch to the next tab and create a new terminal there:

=> :tabnext
=> :split | terminal

Then switch to Terminal mode and run:

=> » PORT=3002 npm run test:watch

Now take a look at the buffer names:

=> :ls
<=  1 a "app.js" line 1
  2 #a "test/app-test.js" line 1
  3 a- "term://.//78008:/usr/local/bin/bash" line 0
  4 %a- "term://.//78095:/usr/local/bin/bash" line 18

You already know what would happen if you were to save your session now and restore it: you would end up with two terminal buffers, each running a bash shell. It would be better if restoring a session would cause both the webserver and the test runner processes to start up again. Let’s make it so.

You can use the :file {name} command to rename your terminal buffers (:help :file_f). Activate the window containing the webserver, then run:

=> :file term://PORT=3001 npm run server

Next, activate the window containing the test runner and run:

=> :file term://PORT=3002 npm run test:watch

Inspect the buffer list to check that it’s worked, then record your session and quit Neovim:

=> :ls
<=  1 a "app.js" line 1
  2 #a "test/app-test.js" line 1
  3 a- "term://PORT=3001 npm run server" line 0
  4 %a- "term://PORT=3002 npm run test:watch" line 18
=> :mksession!
=> :qa!

Now, restart Neovim and load the session you just recorded:

=> $ nvim -S

You should find that both the webserver and the test runner processes have been restarted.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.240.21