Proposed By : Aleem Ahamed Shaik(shaikaleemahamed786@gmail.com)

1. The Problem

When developing or troubleshooting Neovim plugins, users often need to restart Neovim to see changes take effect. Currently, this process is manual and inefficient:

  • Users must quit Neovim.

  • Then, restart it manually via a terminal or script.

  • If they want to restore their session (open buffers, splits, cursor positions), they need to use a session manager or manually reopen everything.

This makes plugin development and debugging slow and frustrating.

2. The Proposed Solution

Introduce a :restart command that will allow Neovim to restart itself automatically. When a user runs :restart:

  • Neovim will save the current session (open files, window layout, cursor positions).

  • It will then start a new Neovim instance with the same configuration.

  • The session will be restored automatically, making it feel like a seamless restart.

This feature will improve the development experience by eliminating the need for manual restarts.


3. Implementation Details

a) Spawning a New Neovim Instance

To restart Neovim, we need to:

  1. Get the command-line arguments used to start Neovim.

  2. Start a new Neovim process with the same arguments.

  3. Close the current Neovim instance.

We can do this using Lua’s vim.loop.spawn(), which is a wrapper around libuv (an asynchronous I/O library that Neovim uses).

Sample Code:

local function restart_nvim()
    -- Get the current Neovim executable path
    local nvim_cmd = vim.v.progpath

    -- Get command-line arguments
    local args = vim.fn.argv()
    
    -- Create a new instance of Neovim
    vim.loop.spawn(nvim_cmd, {
        args = args,
        stdio = { nil, nil, nil } -- Detach from current instance
    }, function() 
        vim.cmd("qall!") -- Close the current instance
    end)
end
vim.api.nvim_create_user_command("Restart", restart_nvim, {})

b) Saving and Restoring the Session

Before quitting, Neovim should save the current session (open buffers, splits, and cursor positions) so that it can be restored after restarting.

Sample Code:

local session_file = vim.fn.stdpath("cache") .. "/session.vim"

local function save_session()
    vim.cmd("mksession! " .. session_file)
end

local function restore_session()
    if vim.fn.filereadable(session_file) == 1 then
        vim.cmd("source " .. session_file)
    end
end

Now, modify the restart function to save and restore the session:

local function restart_nvim()
    save_session() -- Save session before restart

    local nvim_cmd = vim.v.progpath
    local args = vim.fn.argv()
    
    vim.loop.spawn(nvim_cmd, {
        args = args,
        stdio = { nil, nil, nil }
    }, function() 
        vim.cmd("qall!") -- Close Neovim
    end)
end

vim.api.nvim_create_user_command("Restart", restart_nvim, {})
restore_session() -- Restore session when Neovim starts

c) Handling Edge Cases

  • If there are unsaved files, Neovim should prompt the user before quitting.

  • The restart should work across different platforms (Linux, macOS, Windows).

  • It should handle terminal multiplexer environments like tmux or screen.

To check for unsaved files:

local function has_unsaved_buffers()
    for _, buf in ipairs(vim.api.nvim_list_bufs()) do
        if vim.api.nvim_buf_get_option(buf, "modified") then
            return true
        end
    end
    return false
end

local function restart_nvim()
    if has_unsaved_buffers() then
        print("Warning: You have unsaved changes!")
        return
    end

    save_session()

    local nvim_cmd = vim.v.progpath
    local args = vim.fn.argv()
    
    vim.loop.spawn(nvim_cmd, {
        args = args,
        stdio = { nil, nil, nil }
    }, function() 
        vim.cmd("qall!")
    end)
end

Why This Solution?

  • Beginner Friendly: The solution is simple and uses built-in Neovim functions and Lua APIs.

  • Ready for GSOC:

    • Solves a real problem for plugin developers.

    • Uses proper session management.

    • Handles edge cases like unsaved files.

    • Follows best practices for process spawning.

  • Extensible: Users can enhance it by adding custom pre-restart or post-restart hooks.

This implementation ensures that Neovim restarts seamlessly while preserving the user’s workflow, making plugin development much smoother. 🚀