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.
26 lines
721 B
Lua
26 lines
721 B
Lua
-- Disable banner
|
|
vim.g.netrw_banner = 0
|
|
|
|
-- Fix gx when running inside WSL
|
|
if vim.env.WSLENV then
|
|
vim.g.netrw_browsex_viewer = 'cmd.exe /C start'
|
|
end
|
|
|
|
-- 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')
|
|
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", directory))
|
|
if filename ~= '' then
|
|
-- In the netrw buffer, move the cursor to the first character of filename
|
|
vim.fn.search('\\<' .. filename .. '\\>')
|
|
end
|
|
end)
|