diff options
| -rw-r--r-- | init.lua | 37 | ||||
| -rw-r--r-- | plugin/50-color.lua | 4 |
2 files changed, 36 insertions, 5 deletions
@@ -73,6 +73,16 @@ local function terminal_put_register() vim.api.nvim_put({ value }, "c", true, true) end +-- Toggle "follow" mode for the current terminal: while on, leaving terminal mode jumps to the +-- bottom (G) so the view tracks the latest output. Shown as `+T` in the statusline (50-color.lua). +local function toggle_terminal_follow() + if vim.bo.buftype ~= "terminal" then + vim.notify("TerminalFollow only applies to terminal buffers", vim.log.levels.WARN) + return + end + vim.b.terminal_follow = not vim.b.terminal_follow +end + -- Kill from the cursor to the end of the command line, akin to Readline's kill-line local function cmdline_kill_line() local pos = vim.fn.getcmdpos() -- 1-based byte index of the cursor @@ -134,6 +144,11 @@ vim.api.nvim_create_user_command( show_buf_diff_orig, { desc = "Show diff between current buffer and file" } ) +vim.api.nvim_create_user_command( + "TerminalFollow", + toggle_terminal_follow, + { desc = "Toggle following terminal output" } +) ------------------------------------------------------------------------------------------------------------------------ -- Keymaps (there's more in the different plugins) @@ -187,11 +202,25 @@ vim.keymap.set("t", "<Esc>", "<C-\\><C-n>", { desc = "Exit terminal mode" }) vim.keymap.set("t", "<M-[>", "<Esc>", { desc = "Send <Esc> to the terminal program" }) vim.keymap.set("t", "<C-o>", "<C-\\><C-o>", { desc = "Execute one normal-mode command" }) vim.keymap.set("t", "<C-r>", terminal_put_register, { desc = "Paste register" }) +vim.keymap.set("n", "<Leader>F", "<Cmd>TerminalFollow<CR>", { desc = "Toggle following terminal output" }) -- stylua: ignore end -vim.api.nvim_create_autocmd( - "TermOpen", - { pattern = "*", command = "startinsert", desc = "Enter terminal mode" } -) +vim.api.nvim_create_autocmd("TermOpen", { + desc = "Enter terminal mode and follow output by default", + group = vim.g.dotfiles.augroup, + callback = function() + vim.b.terminal_follow = true + vim.cmd.startinsert() + end, +}) +vim.api.nvim_create_autocmd("TermLeave", { + desc = "Follow terminal output (jump to bottom) when enabled, see :TerminalFollow", + group = vim.g.dotfiles.augroup, + callback = function() + if vim.b.terminal_follow then + vim.cmd.normal({ "G", bang = true }) + end + end, +}) ------------------------------------------------------------------------------------------------------------------------ -- Options diff --git a/plugin/50-color.lua b/plugin/50-color.lua index 4cdbad0..c88e34b 100644 --- a/plugin/50-color.lua +++ b/plugin/50-color.lua @@ -80,11 +80,13 @@ local function spell_status() end --[[ Custom status flags: `+F` when formatting is on for the buffer, `+H` when LSP inlay hints are on, -`+R` when disk-change autoreload is enabled (see 50-autoreload.lua), followed by the spell flag. ]] +`+R` when disk-change autoreload is enabled (see 50-autoreload.lua), `+T` when terminal follow is on +(see :TerminalFollow), followed by the spell flag. ]] local function statuses() return (vim.b.format and "+F" or "") .. (vim.lsp.inlay_hint.is_enabled({ bufnr = 0 }) and "+H" or "") .. (vim.g.autoreload and "+R" or "") + .. (vim.b.terminal_follow and "+T" or "") .. (vim.o.spell and spell_status() or "") end |
