From 83505fd0a204200b251f7c21085021b5b718f4fd Mon Sep 17 00:00:00 2001 From: lightofshadow Date: Fri, 20 Mar 2026 15:29:18 +0000 Subject: [PATCH] Better comments, nerdtree settings --- init.vim | 152 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 108 insertions(+), 44 deletions(-) diff --git a/init.vim b/init.vim index 3ed4213..225bd99 100644 --- a/init.vim +++ b/init.vim @@ -1,3 +1,4 @@ +" Basic editor behavior set relativenumber set number set autoindent @@ -5,29 +6,30 @@ set clipboard+=unnamed " Use system clipboard set termguicolors set cursorline set ts=4 sw=4 -"gets rid of --INSERT-- useful bc of lighline" +" Lightline already shows the current mode, so hide the default mode text. set noshowmode set mouse=a -"splits" +" Open new splits in the more common IDE-like directions. set splitright set splitbelow +" Plugins call plug#begin() -"file explorer" +" File explorer Plug 'preservim/nerdtree' -"status line" +" Status line Plug 'itchyny/lightline.vim' -"color themes" +" Colorschemes Plug 'gilgigilgil/anderson.vim' Plug 'sainnhe/sonokai' -"automatic parentheses" +" Automatic bracket and quote pairing Plug 'jiangmiao/auto-pairs' -"syntax highlighting" +" Syntax highlighting and structural selection Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'} -"shortcut to comment multiple lines" +" Commenting helpers Plug 'numToStr/Comment.nvim' - +" Completion and snippet support Plug 'neovim/nvim-lspconfig' Plug 'hrsh7th/cmp-nvim-lsp' Plug 'hrsh7th/cmp-buffer' @@ -35,7 +37,7 @@ Plug 'hrsh7th/cmp-path' Plug 'hrsh7th/cmp-cmdline' Plug 'hrsh7th/nvim-cmp' -" For vsnip users. +" Snippet backend used by nvim-cmp Plug 'hrsh7th/cmp-vsnip' Plug 'hrsh7th/vim-vsnip' @@ -43,15 +45,56 @@ call plug#end() let $NVIM_TUI_ENABLE_TRUE_COLOR=1 -" Start NERDTree fullscreen if no file specified, otherwise open in split +" NERDTree startup behavior +" - No file argument: open the tree fullscreen. +" - File argument or stdin: open the tree on the side and return focus to the file. +" - If the tree is the last remaining window, quit Neovim. autocmd StdinReadPre * let s:std_in=1 +autocmd VimEnter * call s:RegisterNERDTreeVSplitOverride() autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | execute 'NERDTree' | only | endif autocmd VimEnter * if argc() > 0 || exists("s:std_in") | NERDTree | wincmd p | endif autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif +" Close NERDTree after opening a file from it. +let g:NERDTreeQuitOnOpen = 1 + +" Open a file from NERDTree in a vertical split and close the tree. +function! NERDTreeOpenVSplitAndClose(node) abort + call a:node.activate({'where': 'v', 'keepopen': 0}) +endfunction + +" Same behavior as above, but for bookmarked nodes. +function! NERDTreeOpenVSplitBookmarkAndClose(bookmark) abort + call a:bookmark.activate(b:NERDTree, !a:bookmark.path.isDirectory ? {'where': 'v', 'keepopen': 0} : {}) +endfunction + +" Override NERDTree's default `s` action once the plugin has been loaded. +function! s:RegisterNERDTreeVSplitOverride() abort + if !exists('*NERDTreeAddKeyMap') + return + endif + + call NERDTreeAddKeyMap({ + \ 'key': 's', + \ 'scope': 'FileNode', + \ 'callback': 'NERDTreeOpenVSplitAndClose', + \ 'quickhelpText': 'open vsplit and close tree', + \ 'override': 1 + \ }) + call NERDTreeAddKeyMap({ + \ 'key': 's', + \ 'scope': 'Bookmark', + \ 'callback': 'NERDTreeOpenVSplitBookmarkAndClose', + \ 'quickhelpText': 'open vsplit and close tree', + \ 'override': 1 + \ }) +endfunction + +" Core NERDTree shortcuts nnoremap n :NERDTreeFocus nnoremap :NERDTreeToggle let g:NERDTreeGitStatusWithFlags = 1 +" Example icon and color overrides kept here for later use. "let g:WebDevIconsUnicodeDecorateFolderNodes = 1 "let g:NERDTreeGitStatusNodeColorization = 1 "let g:NERDTreeColorMapCustom = { @@ -67,56 +110,71 @@ let g:NERDTreeGitStatusWithFlags = 1 let g:NERDTreeIgnore = ['^node_modules$'] +" Lightline appearance let g:lightline = { \ 'colorscheme': 'wombat', \ } +" Colorscheme configuration let g:sonokai_style = 'shusia' let g:sonokai_better_performance = 1 colorscheme sonokai lua << EOF -require'nvim-treesitter.configs'.setup { - -- Install parsers for languages you use - ensure_installed = { "c", "cpp", "python", "lua", "vim", "go", "html", "css", "javascript", "ocaml"}, +-- Treesitter config: prefer the older `configs` entrypoint, but fall back to +-- the newer top-level module for the version currently installed locally. +local ok, ts = pcall(require, 'nvim-treesitter.configs') +if not ok then + ok, ts = pcall(require, 'nvim-treesitter') +end - -- Install all maintained parsers - -- ensure_installed = "all", +if ok then + ts.setup { + -- Parsers to install automatically. + ensure_installed = { "c", "cpp", "python", "lua", "vim", "go", "html", "css", "javascript", "ocaml" }, - -- Syntax highlighting - highlight = { - enable = true, - additional_vim_regex_highlighting = false, - }, + -- Install all maintained parsers instead. + -- ensure_installed = "all", - -- Indentation based on treesitter (experimental) - indent = { - enable = true - }, - - -- Incremental selection based on the syntax tree - incremental_selection = { - enable = true, - keymaps = { - init_selection = "gnn", - node_incremental = "grn", - scope_incremental = "grc", - node_decremental = "grm", + -- Treesitter-powered syntax highlighting. + highlight = { + enable = true, + additional_vim_regex_highlighting = false, }, - } -} + -- Treesitter indentation where supported. + indent = { + enable = true + }, + + -- Grow and shrink selections by syntax node instead of by raw text. + incremental_selection = { + enable = true, + keymaps = { + init_selection = "gnn", + node_incremental = "grn", + scope_incremental = "grc", + node_decremental = "grm", + }, + } + } +else + -- Keep startup working even if Treesitter is installed incorrectly. + vim.notify('nvim-treesitter is installed but could not be loaded', vim.log.levels.WARN) +end + +-- Enable default comment mappings like gcc and gc. require('Comment').setup() EOF lua <'] = cmp.mapping.scroll_docs(4), [''] = cmp.mapping.complete(), [''] = cmp.mapping.abort(), - [''] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. + -- Use Tab to confirm the current completion item, otherwise keep normal Tab behavior. + [''] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.confirm({ select = true }) + else + fallback() + end + end, { 'i', 's' }), }), sources = cmp.config.sources({ { name = 'nvim_lsp' }, @@ -153,8 +218,7 @@ lua < with each lsp server you've enabled. vim.lsp.config('', {