Home Explore Blog CI



neovim

3rd chunk of `runtime/doc/terminal.txt`
56cd383a2a0eb04885a1f1087b33e9c1d173d7819453efb40000000100000fa6
    end
  })

To try it out, select the above code and source it with `:'<,'>lua` (or
`g==`), then run this command in a :terminal buffer: >

    printf "\033]7;file://./foo/bar\033\\"

OSC 52: write to system clipboard		*terminal-osc52*

Applications in the :terminal buffer can write to the system clipboard by
emitting an OSC 52 sequence. Example: >

    printf '\033]52;;%s\033\\' "$(echo -n 'Hello world' | base64)"

Nvim uses the configured |clipboard| provider to write to the system
clipboard. Reading from the system clipboard with OSC 52 is not supported, as
this would allow any arbitrary program in the :terminal to read the user's
clipboard.

OSC 52 sequences sent from the :terminal buffer do not emit a |TermRequest|
event. The event is handled directly by Nvim and is not forwarded to plugins.

OSC 133: shell integration			*terminal-osc133* *shell-prompt*

Shells can emit semantic escape sequences (OSC 133) to mark where each prompt
starts and ends. The start of a prompt is marked by sequence `OSC 133 ; A ST`,
and the end by `OSC 133 ; B ST`.

You can configure your shell "rc" (e.g. ~/.bashrc) to emit OSC 133 sequences,
or your terminal may attempt to do it for you (assuming your shell config
doesn't interfere).

- fish: https://fishshell.com/docs/current/relnotes.html#improved-terminal-support
- kitty: https://sw.kovidgoyal.net/kitty/shell-integration/
- powershell: https://learn.microsoft.com/en-us/windows/terminal/tutorials/shell-integration#powershell-pwshexe
- vscode: https://code.visualstudio.com/docs/terminal/shell-integration

To configure bash to mark the start of each prompt, set $PROMPT_COMMAND: >bash

    # Prompt start:
    PROMPT_COMMAND='printf "\033]133;A\007"'
<
                                                *terminal_]]* *terminal_[[*
The |]]| and |[[| motions jump to the next/previous prompts, if your shell
emits OSC 133 as described above.

						*shell-prompt-signs*
To annotate each terminal prompt with a sign, call |nvim_buf_set_extmark()|
from a |TermRequest| handler: >lua

    vim.api.nvim_create_autocmd('TermOpen', {
      command = 'setlocal signcolumn=auto',
    })
    local ns = vim.api.nvim_create_namespace('my.terminal.prompt')
    vim.api.nvim_create_autocmd('TermRequest', {
      callback = function(args)
        if string.match(args.data.sequence, '^\027]133;A') then
          local lnum = args.data.cursor[1]
          vim.api.nvim_buf_set_extmark(args.buf, ns, lnum - 1, 0, {
            sign_text = '▶',
            sign_hl_group = 'SpecialChar',
          })
        end
      end,
    })
<
==============================================================================
Status Variables				*terminal-status*

Terminal buffers maintain some buffer-local variables and options. The values
are initialized before TermOpen, so you can use them in a local 'statusline'.
Example: >vim
    :autocmd TermOpen * setlocal statusline=%{b:term_title}

- *b:term_title*  Terminal title (user-writable), typically displayed in the
  window title or tab title of a graphical terminal emulator. Terminal
  programs can set this by emitting an escape sequence.
- |'channel'|  Terminal PTY |job-id|.  Can be used with |chansend()| to send
  input to the terminal.
- The |TermClose| event gives the terminal job exit code in the |v:event|
  "status" field. For example, this autocommand outputs the terminal's exit
  code to |:messages|: >vim
    autocmd TermClose * echom 'Terminal exited with status '..v:event.status

Use |jobwait()| to check if the terminal job has finished: >vim
    let running = jobwait([&channel], 0)[0] == -1
<
==============================================================================
:Termdebug plugin	*terminal-debug* *terminal-debugger* *package-termdebug*

The Terminal debugging plugin can be used to debug a program with gdb and view
the source code in a Vim window.  Since this is completely contained inside
Vim this also works remotely over an ssh connection.

Starting ~
							*termdebug-starting*

Title: Nvim Terminal OSC Codes, Shell Integration, Status Variables, and Termdebug Plugin
Summary
This section discusses OSC codes for terminal integration, specifically OSC 133 for marking shell prompt start and end, including configuration examples for fish, kitty, powershell, and vscode. It covers using `nvim_buf_set_extmark()` to annotate prompts with signs. Additionally, the section describes terminal status variables like `b:term_title` and the `'channel'` option, which can be used in statuslines, and the `TermClose` event for accessing the terminal job exit code. Finally, it introduces the Termdebug plugin for debugging programs with gdb within Nvim.