From 1e6bb57c4f8c0c0b89c69cffe624e0d85d2c0a7f Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Sat, 2 Mar 2024 15:19:31 +0000 Subject: [PATCH] Customize netrw to my linking --- init.lua | 1 + lua/netrw.lua | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 lua/netrw.lua diff --git a/init.lua b/init.lua index 189cc68..f183645 100644 --- a/init.lua +++ b/init.lua @@ -1,5 +1,6 @@ require('settings') require('mappings') +require('netrw') local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim" if not vim.loop.fs_stat(lazypath) then diff --git a/lua/netrw.lua b/lua/netrw.lua new file mode 100644 index 0000000..2f4fb14 --- /dev/null +++ b/lua/netrw.lua @@ -0,0 +1,30 @@ +-- Disable banner +vim.g.netrw_banner = 0 + +-- Map - key to open netrw in current files parent directory +vim.keymap.set('n', '-', function() + -- Get the filename before invoking netrw + local filename = vim.fn.expand('%:t') + + -- Invoke netrw on the directory containing the current file + vim.fn.execute(string.format("edit %s", vim.fn.expand('%:hp'))) + + -- Move the cursor to the line matching file + local netrw_liststyle = vim.api.nvim_buf_get_var( + vim.api.nvim_get_current_buf(), 'netrw_liststyle') + local pattern = nil + -- TODO: Replace this with something simpler, perhaps a loop over the netrw + -- buffer lines to get the line number containing the filename string. + if netrw_liststyle and netrw_liststyle == 2 then + -- tpope's magic pattern for list style with multiple files in columns + pattern = '\\%(^\\|\\s\\+\\)\zs' + .. vim.fn.escape(filename .. '.*[]~\\') + .. '[/*|@=]\\=\\%($\\|\\s\\+\\)' + else + -- tpope's magic pattern for all other list styles + pattern = '^\\%(| \\)*' + .. vim.fn.escape(filename, '.*[]~\\') + .. '[/*|@=]\\=\\%($\\|\\t\\)' + end + vim.fn.search(pattern, 'wc') +end)