From 6533a96ba05b9c5b98c8711f94bf0423f766c0a6 Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Thu, 14 Mar 2024 23:57:41 +0000 Subject: [PATCH] Fix netrw - mapping when in a buffer with no name/file --- lua/netrw.lua | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/lua/netrw.lua b/lua/netrw.lua index 067227b..8b8b243 100644 --- a/lua/netrw.lua +++ b/lua/netrw.lua @@ -10,18 +10,25 @@ end vim.keymap.set('n', '-', function() -- Get the filename before invoking netrw local filename = vim.fn.expand('%:t') + local directory = vim.fn.expand('%:hp') + + if directory == '' then + directory = vim.fn.getcwd() + end -- Invoke netrw on the directory containing the current file - vim.fn.execute(string.format("edit %s", vim.fn.expand('%:hp'))) + vim.fn.execute(string.format("edit %s", directory)) - -- In the netrw buffer, move the cursor to the first character of filename - local buffer = vim.api.nvim_get_current_buf() - local lines = vim.api.nvim_buf_get_lines(buffer, 0, -1, true) - for row, content in ipairs(lines) do - local column = string.find(content, filename) - if column then - vim.api.nvim_win_set_cursor(0, { row, column - 1 }) - break + if filename ~= '' then + -- In the netrw buffer, move the cursor to the first character of filename + local buffer = vim.api.nvim_get_current_buf() + local lines = vim.api.nvim_buf_get_lines(buffer, 0, -1, true) + for row, content in ipairs(lines) do + local column = string.find(content, filename) + if column then + vim.api.nvim_win_set_cursor(0, { row, column - 1 }) + break + end end end end)