diff --git a/lua/fzf-lua/core.lua b/lua/fzf-lua/core.lua index f82d243..8cb27d8 100644 --- a/lua/fzf-lua/core.lua +++ b/lua/fzf-lua/core.lua @@ -491,7 +491,7 @@ M.set_header = function(opts, hdr_tbl) cwd = path.relative(cwd, vim.loop.cwd()) end -- make our home dir path look pretty - return cwd:gsub("^"..vim.env.HOME, "~") + return path.HOME_to_tilde(cwd) end }, search = { diff --git a/lua/fzf-lua/make_entry.lua b/lua/fzf-lua/make_entry.lua index 102dd71..ea73f7c 100644 --- a/lua/fzf-lua/make_entry.lua +++ b/lua/fzf-lua/make_entry.lua @@ -312,6 +312,10 @@ M.file = function(opts, x) -- TODO: does this work if there are ANSI escape codes in x? x = path.relative(x, opts.cwd) end + -- replace $HOME with ~ + if path.starts_with_separator(x) then + x = path.HOME_to_tilde(x) + end if opts.path_shorten then x = path.shorten(x, tonumber(opts.path_shorten)) end diff --git a/lua/fzf-lua/path.lua b/lua/fzf-lua/path.lua index 25a773c..8b39fc9 100644 --- a/lua/fzf-lua/path.lua +++ b/lua/fzf-lua/path.lua @@ -125,6 +125,14 @@ local function find_next(str, char, start_idx) end end +function M.tilde_to_HOME(path) + return path and path:gsub("^~", vim.env.HOME) or nil +end + +function M.HOME_to_tilde(path) + return path and path:gsub("^"..vim.env.HOME, "~") or nil +end + function M.shorten(path, max_len) local sep = M.separator() local parts = {} @@ -200,6 +208,7 @@ function M.entry_to_file(entry, opts, force_uri) -- Remove ansi coloring and prefixed icons entry = utils.strip_ansi_coloring(entry) local stripped, idx = stripBeforeLastOccurrenceOf(entry, utils.nbsp) + stripped = M.tilde_to_HOME(stripped) local isURI = stripped:match("^%a+://") -- Prepend cwd before constructing the URI (#341) if cwd and #cwd>0 and not isURI and diff --git a/lua/fzf-lua/previewer/builtin.lua b/lua/fzf-lua/previewer/builtin.lua index 9d136c2..d504639 100644 --- a/lua/fzf-lua/previewer/builtin.lua +++ b/lua/fzf-lua/previewer/builtin.lua @@ -614,6 +614,7 @@ function Previewer.buffer_or_file:update_border(entry) if entry.path and self.opts.cwd then entry.path = path.relative(entry.path, self.opts.cwd) end + entry.path = path.HOME_to_tilde(entry.path) local title = (' %s '):format(entry.path or entry.uri) if entry.bufnr then -- local border_width = api.nvim_win_get_width(self.win.preview_winid) diff --git a/lua/fzf-lua/providers/buffers.lua b/lua/fzf-lua/providers/buffers.lua index 40fcceb..8d39ad0 100644 --- a/lua/fzf-lua/providers/buffers.lua +++ b/lua/fzf-lua/providers/buffers.lua @@ -120,7 +120,7 @@ local function gen_buffer_entry(opts, buf, hl_curbuf) bufname = path.basename(bufname) end -- replace $HOME with '~' for paths outside of cwd - bufname = bufname:gsub("^"..vim.env.HOME, "~") + bufname = path.HOME_to_tilde(bufname) -- add line number bufname = ("%s:%s"):format(bufname, buf.info.lnum>0 and buf.info.lnum or "") if buf.flag == '%' then