From 01f7ed6eff3c920c8a79e59d99d49d7a33fcd56b Mon Sep 17 00:00:00 2001 From: psychhim Date: Sun, 14 Sep 2025 17:15:08 +0530 Subject: [PATCH] Updated Neo-tree opening files in a new tab by pressing Enter but replacing an empty buffer by opening in the same tab, Added e to open an empty buffer in a new tab, fixed Neo-tree starting on left side if Neovim is opened inside a directory --- init.lua | 33 +++++++++------ lua/custom/plugins/filetree.lua | 72 +++++++++++++++++++++++++++++++-- 2 files changed, 89 insertions(+), 16 deletions(-) diff --git a/init.lua b/init.lua index 9b8b031..0649fce 100644 --- a/init.lua +++ b/init.lua @@ -1,14 +1,6 @@ vim.g.mapleader = ' ' vim.g.maplocalleader = ' ' ---[[Open Neotree on new tab]] ---vim.gpi.nvim_create_autocmd("TabNewEntered", { --- group = vim.api.nvim_create_augroup("NeotreeOnNewTab", { clear = true }), --- callback = vim.schedule_wrap(function() --- vim.cmd("Neotree show") --- end), ---}) - -- [[ Install `lazy.nvim` plugin manager ]] -- https://github.com/folke/lazy.nvim -- `:help lazy.nvim.txt` for more info @@ -30,6 +22,18 @@ vim.opt.rtp:prepend(lazypath) -- You can configure plugins using the `config` key. -- You can also configure plugins after the setup call, -- as they will be available in your neovim runtime. + +-- Disables which-key healthcheck notifications +do + local orig_notify = vim.notify + vim.notify = function(msg, ...) + if type(msg) == "string" and msg:match("which%-key") then + return -- ignore WhichKey health messages + end + return orig_notify(msg, ...) + end +end + require('lazy').setup({ -- NOTE: First, some plugins that don't require any configuration -- Git related plugins @@ -287,17 +291,23 @@ vim.keymap.set('n', 'j', "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = tr -- Diagnostic keymaps vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, { desc = 'Go to previous diagnostic message' }) vim.keymap.set('n', ']d', vim.diagnostic.goto_next, { desc = 'Go to next diagnostic message' }) -vim.keymap.set('n', 'e', vim.diagnostic.open_float, { desc = 'Open floating diagnostic message' }) -vim.keymap.set('n', 'q', vim.diagnostic.setloclist, { desc = 'Open diagnostics list' }) +--vim.keymap.set('n', 'e', vim.diagnostic.open_float, { desc = 'Open floating diagnostic message' }) +--vim.keymap.set('n', 'q', vim.diagnostic.setloclist, { desc = 'Open diagnostics list' }) --[[ Custom keymaps ]] ---NeoTree sync to current directory & toggle +--Neo-tree sync to current directory & toggle vim.keymap.set('n', 'n', 'cd %:p:h | Neotree toggle float') --Terminal open in new tab vim.keymap.set('n', 't', 'tabnew +termi') +-- Create an empty buffer in a new tab +vim.keymap.set("n", "e", function() + vim.cmd("tabnew") -- create a new tab + vim.cmd("enew") -- create a new empty buffer in it +end, { noremap = true, silent = true }) + -- Save current buffer (asks for filename if new/unsaved) vim.keymap.set('n', 'w', function() if vim.api.nvim_buf_get_name(0) == "" then @@ -646,7 +656,6 @@ mason_lspconfig.setup { }, } - -- [[ Configure nvim-cmp ]] -- See `:help cmp` local cmp = require 'cmp' diff --git a/lua/custom/plugins/filetree.lua b/lua/custom/plugins/filetree.lua index cdd0a4c..9c72fa1 100644 --- a/lua/custom/plugins/filetree.lua +++ b/lua/custom/plugins/filetree.lua @@ -10,13 +10,77 @@ return { "MunifTanjim/nui.nvim", }, config = function() + + -- smart_open function: If there's an empty "No Name" buffer, replace it and open files in the same tab +local function smart_open(state) + local node = state.tree:get_node() + if not node then return end + local path = node:get_id() + + -- Get the window in the current tab that is NOT Neo-tree + local wins = vim.api.nvim_tabpage_list_wins(0) + local target_buf = nil + for _, win in ipairs(wins) do + local buf = vim.api.nvim_win_get_buf(win) + local bufname = vim.api.nvim_buf_get_name(buf) + local buftype = vim.api.nvim_buf_get_option(buf, "buftype") + local modified = vim.api.nvim_buf_get_option(buf, "modified") + -- find a window with empty, unmodified buffer + if bufname == "" and buftype == "" and not modified then + target_buf = buf + vim.api.nvim_set_current_win(win) + break + end + end + + if target_buf then + -- replace the empty buffer + vim.cmd("edit " .. path) + else + -- otherwise open in new tab + require("neo-tree.sources.filesystem.commands").open_tabnew(state) + end +end + require('neo-tree').setup { - filesystem = { - filtered_items = { + -- Open Neo-tree in a floating window in middle at first launch when Neovim is opened inside a directory with "nvim ." + close_if_last_window = true, -- close Neo-tree if it's the last window + popup_border_style = "rounded", + enable_git_status = true, + enable_diagnostics = true, + default_component_configs = { + indent = { + padding = 1, + indent_size = 2, + }, + icon = { + folder_closed = "", + folder_open = "", + folder_empty = "ﰊ", + }, + }, + window = { + position = "float", -- this makes Neo-tree open in the middle + width = 40, + mapping_options = { + noremap = true, + nowait = true, + }, + mappings = { + [""] = smart_open, -- Enter → always open file in new tab if there's no empty buffer + ["t"] = "noop", -- disable t + }, + }, + filesystem = { + follow_current_file = true, + use_libuv_file_watcher = true, + hijack_netrw_behavior = "open_default", + filtered_items = { visible = true, -- This is what you want: If you set this to `true`, all "hide" just mean "dimmed out" hide_dotfiles = false, hide_gitignored = true, - }, - }, } + }, + }, + } end, }