From b1f1ff8545363655e8d69c64c82642f546a91a1c Mon Sep 17 00:00:00 2001 From: Ben Vincent Date: Mon, 11 Mar 2024 18:08:32 +1100 Subject: [PATCH] VIM updates - updated keybindings to match work - added functions for finding files --- .config/nvim/lua/func.lua | 25 +++++++---- .config/nvim/lua/func/findfile.lua | 72 ++++++++++++++++++++++++++++++ .config/nvim/lua/func/sessions.lua | 5 +++ .config/nvim/lua/keys.lua | 4 +- .config/password-store | 2 +- 5 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 .config/nvim/lua/func/findfile.lua diff --git a/.config/nvim/lua/func.lua b/.config/nvim/lua/func.lua index a868be9..0d94e86 100644 --- a/.config/nvim/lua/func.lua +++ b/.config/nvim/lua/func.lua @@ -1,15 +1,22 @@ -- FUNCTIONS -- -- all language functions -vim.cmd [[ - augroup lang_all - autocmd! - autocmd FileType * luafile ~/.config/nvim/lua/func/utils.lua - autocmd FileType * luafile ~/.config/nvim/lua/func/tabline.lua - autocmd FileType * luafile ~/.config/nvim/lua/func/popterm.lua - autocmd FileType * luafile ~/.config/nvim/lua/func/sessions.lua - augroup END -]] +require('func.utils') +require('func.tabline') +require('func.popterm') +require('func.sessions') +require('func.findfile') + +-- vim.cmd [[ +-- augroup lang_all +-- autocmd! +-- autocmd FileType * luafile ~/.config/nvim/lua/func/utils.lua +-- autocmd FileType * luafile ~/.config/nvim/lua/func/tabline.lua +-- autocmd FileType * luafile ~/.config/nvim/lua/func/popterm.lua +-- autocmd FileType * luafile ~/.config/nvim/lua/func/sessions.lua +-- autocmd FileType * luafile ~/.config/nvim/lua/func/findfile.lua +-- augroup END +-- ]] -- puppet language keybindings vim.cmd [[ diff --git a/.config/nvim/lua/func/findfile.lua b/.config/nvim/lua/func/findfile.lua new file mode 100644 index 0000000..506598e --- /dev/null +++ b/.config/nvim/lua/func/findfile.lua @@ -0,0 +1,72 @@ +vim.cmd([[ +let g:previous_buffer = 0 + +function! FindFile() + " Store the buffer number of the current window + let g:previous_buffer = bufnr('%') + + " Prompt user for search pattern + let pattern = input('Enter search pattern: ') + let escaped_pattern = escape(pattern, '\/$.*[~') + "let cmd = "grep -Rl " . escaped_pattern . " *" + let cmd = "rg --column --no-heading --color=never -l " . shellescape(escaped_pattern) + + + " Get the search results as a list + let results = systemlist(cmd) + if empty(results) + echo "No files found for pattern: " . pattern + return + endif + + " Open a new split window at the bottom + new + " Set the height of the new split window + resize 10 + + " Set the buffer to be unlisted, no swapfile + setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile + + " Clear the 'readonly' option in case it's set by default + setlocal noreadonly + + " Populate the buffer with the search results + call setline(1, results) + + " Make the buffer read-only after populating it + setlocal readonly + + " Map keys to open files in different modes + nnoremap :call OpenFile('edit', g:previous_buffer) + nnoremap v :call OpenFile('vsplit', g:previous_buffer) + nnoremap h :call OpenFile('split', g:previous_buffer) + nnoremap t :call OpenFile('tabedit', g:previous_buffer) + + " Focus on the results window + normal! G +endfunction + +function! OpenFile(openType, bufnr) + " Extract the file path from the current line + let file_path = getline('.') + if filereadable(file_path) + " Save the current buffer number + let current_bufnr = bufnr('%') + " Close the search results window + bd! + " Switch to the buffer that was active before the search + execute 'buffer' g:previous_buffer + " Open the file based on the specified openType + execute a:openType . ' ' . file_path + " If opening in a new tab, switch back to the original buffer in the new tab + if a:openType == 'tabnew' + execute 'tabnext' + execute 'buffer' . current_bufnr + endif + else + echo "File does not exist or cannot be read" + endif +endfunction + +nnoremap ff :call FindFile() +]]) diff --git a/.config/nvim/lua/func/sessions.lua b/.config/nvim/lua/func/sessions.lua index a51dc08..35d3237 100644 --- a/.config/nvim/lua/func/sessions.lua +++ b/.config/nvim/lua/func/sessions.lua @@ -1,4 +1,9 @@ vim.cmd([[ +let g:session_dir = expand('~/.cache/nvim/sessions/') +if !isdirectory(g:session_dir) + call mkdir(g:session_dir, "p") +endif + command! SaveSession call SaveSession() function! SaveSession() let session_file = g:session_dir . GetRepositoryName() . '_' . GetCurrentGitBranch() . '.vim' diff --git a/.config/nvim/lua/keys.lua b/.config/nvim/lua/keys.lua index 77b5c33..b120065 100644 --- a/.config/nvim/lua/keys.lua +++ b/.config/nvim/lua/keys.lua @@ -48,6 +48,6 @@ map('n', 'ff', [[:Telescope find_files]], {}) -- map('n', 'T', ':lua require("FTerm").toggle()', {}) -- Open a file in a vertical split, horizontal split and move a split to a new tab -map('n', 'sv', ':vsplit ', {noremap = true, silent = false}) -map('n', 'sh', ':split ', {noremap = true, silent = false}) +map('n', 'ov', ':vsplit ', {noremap = true, silent = false}) +map('n', 'oh', ':split ', {noremap = true, silent = false}) map('n', 'mt', ':tabedit %', {noremap = true, silent = true}) diff --git a/.config/password-store b/.config/password-store index 8bafa73..ba169ed 160000 --- a/.config/password-store +++ b/.config/password-store @@ -1 +1 @@ -Subproject commit 8bafa7394bfcb8cfbafc43190be64cee6e70aa7c +Subproject commit ba169ed5b0c16f6bd9cedf88d4b54ba369d4a19c