2024-03-08: Using Vim Terminal

Vim provides a ":terminal" command, which opens up a terminal window inside the editor. This can be useful to directly copy-paste snippets of code between an active REPL and your editor window.

Open the Terminal

For example, I have Scheme48 installed, with its home directory on the path. I can open a Scheme 48 REPL inside Vim using:

:term scheme48.bat

This opens the window above the current one - to open the window to the side, use:

:vert :term scheme48.bat

This places the cursor in the REPL, and you can evaluate simple expressions:

Split Vim window and terminal, showing evaluation of an expression in the terminal.

Exit the Terminal

To exit the terminal, type CTRL-W N to enter normal mode, then :q!

Evaluate Code in Text Window

Let's type a simple expression into the text window and execute it in the terminal:

  1. Type CTRL-W CTRL-W to switch to the text window
  2. Type (+ 4 3)
  3. ESC-yy to yank the expression into the unnamed register
  4. CTRL-W CTRL-W to switch to the Scheme 48 REPL
  5. CTRL-W " " to paste the unnamed register contents into the REPL
  6. You may need to press ENTER to evaluate the expression

You can use Vim's registers to hold different test expressions, or use the REPL history to repeat test expressions while redefining functions.

In the following example, the num-fn definition is altered while re-evaluating the map test expression.

  1. On the define statement, use yy to yank into unnamed buffer
  2. CTRL-W CTRL-W goes to the REPL and then CTRL-W " " pastes the definition
  3. " a y y yanks the map test expression into register a
  4. CTRL-W CTRL-W goes to the REPL and CTRL-W " a pastes the test expression
  5. After redefining the definition, we can repeat step 4 to reevaluate the test expression Split Vim window and terminal, showing expressions from the text window evaluated in the terminal.

Finally, CTRL-W N switches the terminal buffer to normal mode, so Vim's usual commands can be used to copy out the responses.

Type i to return to insert mode.


Page from Peter's Scrapbook, output from a VimWiki on 2024-03-08.