Fix - mapping sometimes landing on wrong filename

Instead of using Lua to search through all buffer lines and match the
file line containing the filename, instead use `search()` and wrap the
filename in word boundry delimiters `\<\>` to always match the whole
filename not a subscript. This also moves the cursor to that position
in the file.
This commit is contained in:
Kenneth Benzie 2024-03-17 14:16:34 +00:00
parent c235fb88e1
commit b9be6fba5a

View File

@ -18,17 +18,8 @@ vim.keymap.set('n', '-', function()
-- Invoke netrw on the directory containing the current file
vim.fn.execute(string.format("edit %s", directory))
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
vim.fn.search('\\<' .. filename .. '\\>')
end
end)