Floating toggle-able terminal in Neovim in 50 lines of Lua

TJ DeVries
18 Dec 202411:09

Summary

TLDRIn this tutorial, the creator demonstrates how to create a floating terminal in Neovim, offering a step-by-step guide to set up a terminal window that can be toggled on and off. The floating terminal is designed to retain its state, preserving the buffer and running processes across sessions. The video also covers how to set up custom keybindings and commands for easy access to the terminal. The guide provides a clear, concise explanation with practical examples, making it accessible to both beginner and experienced Neovim users.

Takeaways

  • 😀 **Create a Floating Window**: The script demonstrates how to create a floating terminal window in Neovim using Lua, which can be toggled with a keymap.
  • 😀 **State Management**: The floating window's buffer and window states are stored and managed across Neovim sessions, ensuring persistence between toggles.
  • 😀 **Reusable Floating Window**: The window is designed to open and close repeatedly, reusing the same buffer and retaining the terminal state (even for long-running tasks like builds).
  • 😀 **Key Mapping for Toggling**: A custom key mapping (`space TT`) is used to toggle the visibility of the floating terminal, allowing quick access.
  • 😀 **Terminal Mode Keybinding**: The terminal mode is configured to exit using the `Escape` key or a custom shortcut, enhancing usability in terminal buffers.
  • 😀 **Customizable Terminal Appearance**: The floating window's appearance can be adjusted, including options like rounded or solid borders, providing flexibility in visual design.
  • 😀 **Buffer Type Check**: A check ensures that the terminal buffer is correctly identified as a terminal, preventing issues when opening or closing the floating window.
  • 😀 **Simplicity of Code**: The floating terminal implementation is compact and minimal, totaling only around 50 lines of code, making it accessible for customization.
  • 😀 **Terminal Persistence**: Even after closing and reopening the floating terminal, ongoing processes like builds or tests continue running in the background, ensuring no disruption to workflows.
  • 😀 **Sponsor Segment with Humor**: The video concludes with a lighthearted sponsor message encouraging viewers to 'adopt a reply guy' on social media, blending humor and content.

Q & A

  • What is the main objective of the script in the transcript?

    -The main objective is to create a floating terminal window in Neovim that can be toggled on and off, with the state (buffer and window) preserved across multiple sessions. It also allows running background tasks, such as build scripts, inside the floating terminal.

  • How do you create a floating window in Neovim using Lua?

    -A floating window is created using the `nvim_open_win()` function, which accepts parameters like width, height, and position relative to the screen. The function also takes an optional border style, such as 'rounded', to style the floating window.

  • How does the script handle the preservation of the terminal state?

    -The script uses a state table to store the `buf` (buffer) and `win` (window) values. These values are updated when the floating window is created and are reused to ensure the same terminal buffer is retained when toggling the terminal on and off.

  • What is the purpose of using the 'vim.api.nvim_create_buf()' function?

    -'vim.api.nvim_create_buf()' is used to create a new buffer in Neovim. This buffer is then associated with the floating terminal window, allowing terminal commands and output to be displayed within it.

  • Why is the buffer's 'buftype' set to 'terminal'?

    -Setting the buffer's 'buftype' to 'terminal' ensures that the buffer behaves like a terminal, enabling interactive features like running shell commands or background tasks inside the floating window.

  • What command is used to toggle the floating terminal window?

    -The `toggle_floating_terminal()` function is used to toggle the floating terminal window. It checks if the window is valid and either hides it or creates a new floating window based on the current state.

  • How can you bind a key to toggle the floating terminal?

    -The script uses `vim.api.nvim_set_keymap()` to bind a key mapping to the floating terminal toggle function. For example, `Space + TT` is mapped to the `FlowTerminal` command, which toggles the terminal window.

  • How does the script handle running background tasks, such as build scripts, in the terminal?

    -Once the terminal is opened, the script allows tasks like a build script to run in the background. When the terminal window is reopened, the previously running task will continue, maintaining the terminal's state between sessions.

  • What happens when you close the floating terminal?

    -When the floating terminal is closed (e.g., by pressing `Ctrl-D`), the terminal session ends. If the terminal is toggled on again, it will reopen in a new buffer, and any background tasks running in the previous terminal will no longer be active unless restarted.

  • What is the role of the `vim.api.nvim_create_user_command()` function in the script?

    -The `vim.api.nvim_create_user_command()` function is used to define a custom Neovim command, `FlowTerminal`, which calls the `toggle_floating_terminal()` function. This allows users to toggle the floating terminal through a command in addition to key mappings.

Outlines

plate

Cette section est réservée aux utilisateurs payants. Améliorez votre compte pour accéder à cette section.

Améliorer maintenant

Mindmap

plate

Cette section est réservée aux utilisateurs payants. Améliorez votre compte pour accéder à cette section.

Améliorer maintenant

Keywords

plate

Cette section est réservée aux utilisateurs payants. Améliorez votre compte pour accéder à cette section.

Améliorer maintenant

Highlights

plate

Cette section est réservée aux utilisateurs payants. Améliorez votre compte pour accéder à cette section.

Améliorer maintenant

Transcripts

plate

Cette section est réservée aux utilisateurs payants. Améliorez votre compte pour accéder à cette section.

Améliorer maintenant
Rate This

5.0 / 5 (0 votes)

Étiquettes Connexes
NeovimFloating TerminalState PersistenceTerminal SetupKeybindingNeovim TipsLinux ToolsDeveloper ProductivityTerminal ModeNeovim PluginVim Configuration
Besoin d'un résumé en anglais ?