improve terminal buffers

This commit is contained in:
2025-10-13 18:56:05 +03:00
parent ad7c791d23
commit 9be8d154ca
15 changed files with 461 additions and 219 deletions

View File

@@ -1,3 +1,16 @@
-- Automatically create a scratch (no-file) buffer if Neovim starts with no files
vim.api.nvim_create_autocmd('VimEnter', {
callback = function()
-- Only trigger if no file arguments are passed
if vim.fn.argc() == 0 then
vim.cmd('enew') -- create new buffer
vim.bo.buftype = 'nofile'
vim.bo.bufhidden = 'wipe'
vim.bo.swapfile = false
end
end,
})
-- Highlight when yanking (copying) text
vim.api.nvim_create_autocmd('TextYankPost', {
callback = function()
@@ -5,13 +18,28 @@ vim.api.nvim_create_autocmd('TextYankPost', {
end,
})
-- Set filetype on new buffers
vim.api.nvim_create_user_command('Capture', function(opts)
local out = vim.fn.execute(opts.args)
vim.cmd('enew')
vim.bo.buftype = 'nofile'
vim.bo.bufhidden = 'hide'
vim.bo.swapfile = false
vim.bo.filetype = 'capture'
vim.api.nvim_buf_set_lines(0, 0, -1, false, vim.split(out, '\n'))
end, { nargs = '+', complete = 'command' })
-- Disable comment continuation only when using 'o'/'O', but keep it for <Enter>
vim.api.nvim_create_autocmd('FileType', {
pattern = '*',
callback = function()
vim.opt_local.formatoptions:remove('o')
end,
})
-- Show cursor line only in active window
vim.api.nvim_create_autocmd({ 'InsertLeave', 'WinEnter' }, {
callback = function()
if vim.w.auto_cursorline then
vim.wo.cursorline = true
vim.w.auto_cursorline = nil
end
end,
})
vim.api.nvim_create_autocmd({ 'InsertEnter', 'WinLeave' }, {
callback = function()
if vim.wo.cursorline then
vim.w.auto_cursorline = true
vim.wo.cursorline = false
end
end,
})

View File

@@ -1,44 +1,54 @@
vim.keymap.set(
'n',
'<leader>q',
vim.diagnostic.setloclist,
{ desc = 'Open diagnostic [Q]uickfix list' }
)
local map = vim.keymap.set
vim.keymap.set('i', 'jk', '<Esc>', { desc = 'Exit insert mode with jk' })
vim.keymap.set('c', 'jk', '<C-c>', { desc = 'Exit command/search mode with jk' })
vim.keymap.set('n', '<Esc>', '<cmd>nohlsearch<CR>', { desc = 'Clear highlights' })
map('n', '<leader>q', vim.diagnostic.setloclist)
-- Prevent "x" from overriding the register
vim.keymap.set('n', 'x', '"_x')
map({ 'i', 'c' }, 'jk', '<Esc>')
map('n', '<Esc>', '<cmd>nohlsearch<CR>')
-- Prevent overriding the register
map('n', 'x', '"_x')
-- Window Navigation
vim.keymap.set('n', '<C-h>', '<C-w>h', { desc = 'Move focus to the left window' })
vim.keymap.set('n', '<C-l>', '<C-w>l', { desc = 'Move focus to the right window' })
vim.keymap.set('n', '<C-j>', '<C-w>j', { desc = 'Move focus to the lower window' })
vim.keymap.set('n', '<C-k>', '<C-w>k', { desc = 'Move focus to the upper window' })
map('n', '<C-h>', '<C-w>h')
map('n', '<C-l>', '<C-w>l')
map('n', '<C-j>', '<C-w>j')
map('n', '<C-k>', '<C-w>k')
-- Tab management
vim.keymap.set('n', '<Leader>tn', ':tabnew<CR>', { desc = '[T]ab [N]ew' })
vim.keymap.set('n', '<Leader>tc', ':tabclose<CR>', { desc = '[T]ab [C]lose' })
vim.keymap.set('n', '<Leader>to', ':tabonly<CR>', { desc = '[T]ab [O]nly' })
vim.keymap.set('n', '<Leader>tl', ':tabnext<CR>', { desc = '[T]ab Next' })
vim.keymap.set('n', '<Leader>th', ':tabprevious<CR>', { desc = '[T]ab Previous' })
vim.keymap.set('n', '<Leader>tm.', ':tabmove +1<CR>', { desc = '[T]ab [M]ove Right' })
vim.keymap.set('n', '<Leader>tm,', ':tabmove -1<CR>', { desc = '[T]ab [M]ove Left' })
map('n', '<Leader>tn', ':tabnew<CR>')
map('n', '<Leader>tc', ':tabclose<CR>')
map('n', '<Leader>tl', ':tabnext<CR>')
map('n', '<Leader>th', ':tabprevious<CR>')
map('n', '<Leader>tm.', ':tabmove +1<CR>')
map('n', '<Leader>tm,', ':tabmove -1<CR>')
for i = 1, 9 do
vim.keymap.set(
'n',
string.format('<Leader>%d', i),
string.format('%dgt', i),
{ desc = string.format('[T]ab %d', i) }
)
map('n', string.format('<Leader>%d', i), string.format('%dgt', i))
end
-- Buffer Management
vim.keymap.set('n', '<Leader>bl', ':ls<CR>', { desc = '[B]uffer [L]ist' })
vim.keymap.set('n', '<Leader>bd', ':bdelete<CR>', { desc = '[B]uffer [D]elete' })
vim.keymap.set('n', ']b', ':bnext<CR>', { desc = '[B]uffer [N]ext' })
vim.keymap.set('n', '[b', ':bprevious<CR>', { desc = '[B]uffer [P]revious' })
vim.keymap.set('n', '<Leader>bb', ':b<Space>', { desc = '[B]uffer Select' })
vim.keymap.set('n', '<Leader>bo', ':bufdo bd|1bd<CR>', { desc = '[B]uffer Delete Others' })
map('n', '<Leader>bl', ':ls<CR>')
map('n', '<Leader>bd', ':bdelete<CR>')
map('n', ']b', ':bnext<CR>')
map('n', '[b', ':bprevious<CR>')
map('n', '<Leader>bb', ':b<Space>')
map('n', '<Leader>bo', ':bufdo bd|1bd<CR>')
-- Terminal
map('n', '<leader>tt', ':TermDefault<CR>')
map('n', '<leader>tr', ':TermRelative<CR>')
map('n', '<leader>ts', ':TermSplit<CR>')
map('n', '<leader>tv', ':TermVSplit<CR>')
-- Terminal mode mappings
local tn = '<C-\\><C-n>'
map('t', '<Esc>', tn)
map('t', 'jk', tn)
map('t', '<C-w>', tn .. '<C-w>')
map('t', '<C-h>', '<cmd>wincmd h<CR>')
map('t', '<C-j>', '<cmd>wincmd j<CR>')
map('t', '<C-k>', '<cmd>wincmd k<CR>')
map('t', '<C-l>', '<cmd>wincmd l<CR>')
-- File explorer
vim.keymap.set('n', '<leader>e', '<cmd>NvimTreeToggle<CR>')
vim.keymap.set('n', '<leader>E', '<cmd>NvimTreeOpen<CR>')

View File

@@ -1,23 +0,0 @@
-- Bootstrap lazy.nvim
local lazypath = vim.fn.stdpath('data') .. '/lazy/lazy.nvim'
if not (vim.uv or vim.loop).fs_stat(lazypath) then
local lazyrepo = 'https://github.com/folke/lazy.nvim.git'
local out =
vim.fn.system({ 'git', 'clone', '--filter=blob:none', '--branch=stable', lazyrepo, lazypath })
if vim.v.shell_error ~= 0 then
vim.api.nvim_echo({
{ 'Failed to clone lazy.nvim:\n', 'ErrorMsg' },
{ out, 'WarningMsg' },
{ '\nPress any key to exit...' },
}, true, {})
vim.fn.getchar()
os.exit(1)
end
end
vim.opt.rtp:prepend(lazypath)
require('lazy').setup({
spec = { { import = 'plugins' } },
change_detection = { notify = false },
rocks = { enabled = false },
})

View File

@@ -1,19 +1,28 @@
-- Map Leader
vim.g.mapleader = ' '
vim.g.maplocalleader = ' '
vim.g.have_nerd_font = true
-- use nvim-tree instead
vim.g.loaded_netrw = 1
-- Disable features
vim.loader.enable()
vim.g.loaded_gzip = 1
vim.g.loaded_tar = 1
vim.g.loaded_tarPlugin = 1
vim.g.loaded_zip = 1
vim.g.loaded_zipPlugin = 1
vim.g.loaded_getscript = 1
vim.g.loaded_getscriptPlugin = 1
vim.g.loaded_vimball = 1
vim.g.loaded_vimballPlugin = 1
vim.g.loaded_matchit = 1
vim.g.loaded_2html_plugin = 1
vim.g.loaded_rrhelper = 1
vim.g.loaded_netrw = 1 -- use nvim-tree instead
vim.g.loaded_netrwPlugin = 1
vim.g.loaded_matchparen = 1
vim.opt.formatoptions:remove('o')
vim.opt.modeline = true
vim.opt.modelines = 5
-- UI and appearance
vim.opt.termguicolors = true -- Disable TrueColor
-- UI
vim.g.have_nerd_font = true
vim.opt.termguicolors = true -- TrueColor
vim.opt.colorcolumn = '100' -- Vertical guide at column 100
vim.opt.signcolumn = 'no' -- Hide sign column
vim.opt.cursorline = true -- Highlight current line
@@ -21,35 +30,39 @@ vim.opt.guicursor = 'n-v-i-c:block' -- Block cursor shape
vim.opt.number = true -- Show absolute line numbers
vim.opt.relativenumber = true -- Show relative numbers
vim.opt.statusline = '%F%m%r%h%w%=%l,%c %P ' -- Custom statusline
vim.opt.swapfile = false
vim.opt.wrap = false -- Line wrapping
vim.opt.linebreak = true -- Wrap long lines at convenient points
vim.opt.breakindent = true -- Preserve indent when wrapping long lines
-- Editing behavior
-- Editing
vim.opt.shiftwidth = 2 -- Number of spaces to use for (auto)indent
vim.opt.tabstop = 2 -- Number of spaces that a <Tab> in file counts for
vim.opt.softtabstop = 2 -- Number of spaces when pressing <Tab> in insert mode
vim.opt.expandtab = true -- Use spaces instead of literal tab characters
vim.opt.autoindent = true -- Copy indent from the current line when starting a new one
vim.opt.smartindent = true -- Automatically inserts indents in code blocks (for C-like languages)
vim.opt.completeopt = { 'menuone' }
-- Scroll
-- Scroll and mouse
vim.opt.scrolloff = 10 -- Keep lines visible above/below cursor
vim.opt.mousescroll = 'hor:1,ver:1' -- Scroll lines/columns
vim.opt.mouse = 'a' -- Enable mouse mode
-- Search and substitution
-- Search
vim.opt.ignorecase = true -- Case-insensitive search
vim.opt.smartcase = true -- Smart-case search
vim.opt.inccommand = 'split' -- Live substitution preview
-- Splits and windows
-- Splits
vim.opt.splitright = true -- Vertical splits to the right
vim.opt.splitbelow = true -- Horizontal splits below
-- Performance and persistence
vim.opt.undofile = false -- Save undo history
vim.opt.undofile = true -- Save undo history
vim.opt.undodir = vim.fn.stdpath('state') .. '/undo'
vim.opt.swapfile = false
vim.opt.backup = false
vim.opt.writebackup = false
vim.opt.updatetime = 250 -- Faster updates
vim.opt.timeoutlen = 300 -- Shorter keymap timeout

View File

@@ -1,34 +1,62 @@
vim.api.nvim_create_autocmd("TermOpen", {
group = vim.api.nvim_create_augroup("custom-term-open", { clear = true }),
local term_group = vim.api.nvim_create_augroup('custom-term-open', { clear = true })
vim.api.nvim_create_autocmd('TermOpen', {
group = term_group,
callback = function()
vim.opt_local.number = false
vim.opt_local.relativenumber = false
vim.opt_local.scrolloff = 0
vim.bo.filetype = "terminal"
vim.cmd("startinsert")
vim.bo.filetype = 'terminal'
vim.cmd.startinsert()
end,
})
-- Open a relative terminal in the current files directory
vim.keymap.set("n", "<leader>ter", function()
vim.cmd.edit(string.format("term://%s//zsh", vim.fn.expand("%:p:h")))
end, { desc = "[T]erminal [R]elative" })
-- Close all terminal buffers before quitting
vim.api.nvim_create_autocmd('QuitPre', {
group = vim.api.nvim_create_augroup('shoutoff_terminals', { clear = true }),
callback = function()
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_loaded(buf) and vim.bo[buf].buftype == 'terminal' then
vim.api.nvim_buf_delete(buf, { force = true })
end
end
end,
})
-- Open a split terminal at the bottom
vim.keymap.set("n", "<leader>ts", function()
vim.cmd.new()
vim.cmd.wincmd("J")
-- Insert when re-entering a terminal window (after switching back)
vim.api.nvim_create_autocmd('BufEnter', {
group = term_group,
pattern = 'term://*',
callback = function()
if vim.bo.buftype == 'terminal' and vim.fn.mode() ~= 'i' then
vim.cmd.startinsert()
end
end,
})
local function open_default()
vim.cmd('terminal')
end
local function open_relative()
local shell = vim.o.shell or 'zsh'
local dir = vim.fn.expand('%:p:h')
vim.cmd(string.format('edit term://%s//%s', dir, shell))
end
local function open_split()
vim.cmd('new')
vim.cmd('wincmd J')
vim.api.nvim_win_set_height(0, 12)
vim.wo.winfixheight = true
vim.cmd.term()
end, { desc = "[T]erminal [S]plit" })
vim.cmd('term')
end
-- Simple terminal open
vim.keymap.set("n", "<Leader>tet", ":terminal<CR>", { desc = "[T]erminal" })
-- Terminal mode keymaps
vim.keymap.set("t", "<Esc>", "<C-\\><C-n>", { desc = "Terminal Normal Mode" })
vim.keymap.set("t", "jk", "<C-\\><C-n>", { desc = "Terminal Normal Mode" })
vim.keymap.set("t", "<C-w>", "<C-\\><C-n><C-w>", { desc = "Terminal Window Command" })
local function open_vertical()
vim.cmd('vsplit')
vim.cmd('term')
end
vim.api.nvim_create_user_command('TermDefault', open_default, {})
vim.api.nvim_create_user_command('TermRelative', open_relative, {})
vim.api.nvim_create_user_command('TermSplit', open_split, {})
vim.api.nvim_create_user_command('TermVSplit', open_vertical, {})

View File

@@ -1,5 +1,3 @@
vim.keymap.set('n', '<leader>e', '<cmd>NvimTreeToggle<CR>')
local function my_on_attach(bufnr)
local api = require('nvim-tree.api')
local opts = { buffer = bufnr }
@@ -95,7 +93,6 @@ return {
},
hijack_cursor = true,
hijack_unnamed_buffer_when_opening = true,
prefer_startup_root = true,
update_focused_file = {
enable = true,

View File

@@ -1,15 +1,15 @@
return {
{
'windwp/nvim-ts-autotag',
opts = {
autotag = {
enable = true,
enable_close = true,
enable_rename = true,
enable_close_on_slash = true,
},
},
},
-- {
-- 'windwp/nvim-ts-autotag',
-- opts = {
-- autotag = {
-- enable = true,
-- enable_close = true,
-- enable_rename = true,
-- enable_close_on_slash = true,
-- },
-- },
-- },
{
'windwp/nvim-autopairs',
event = 'InsertEnter',