1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

neovimで開いたファイル先のディレクトリツリーを表示する(nvim-tree)

Last updated at Posted at 2023-08-24

概要

neovimでnvim-treeを使用しています。nvim hoge.txtをした際に、開いたファイル先のディレクトリツリーを表示してほしかったのでその設定方法をまとめます。

例えばカレントディレクトリが~/path/to/currentだったとして、nvim ~/config/nvim/init.luaを開いた場合、currentをディレクトリツリーに表示するのではなく、nvimの方を表示することができます。

やったこと

file-explorer.luaの設定

nvim-treeの設定ファイルを作成し、以下の項目を書きます。

file-explorer.lua
vim.g.nvim_tree_respect_buf_cwd = 1

require("nvim-tree").setup
{
	update_focused_file = {
		enable = true,
		update_cwd = true,
	},
}

なお、一度nvim-treeを開いただけだとカレントディレクトリが開くので、再度開きなおすかコードに移動してもう一度nvim-treeに移動すると、nvim-treeがRefreshされて正しく表示されます。

(補足)私は以下のようにfile-explorer.luaを設定しています。

file-explorer.lua
-- disable netrw at the very start of your init.lua (strongly advised)
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

-- set termguicolors to enable highlight groups
vim.opt.termguicolors = true

-- showing the tree of my current buffer from where i open up nvim-tree
vim.g.nvim_tree_respect_buf_cwd = 1

require("nvim-tree").setup
{
	sort_by = 'extension',
	update_focused_file = {
		enable = true,
		update_cwd = true,
	},
	view = {
		width = '20%',
		side = 'right',
		signcolumn = 'no',
	},

	renderer = {
		highlight_git = true,
		highlight_opened_files = 'name',
		icons = {
			glyphs = {
				git = {
					unstaged = '!', renamed = '»',
					untracked = '?', deleted = '✘',
					staged = '✓', unmerged = '', ignored = '◌',
				},
			},
		},
	},

	git = {
		enable = true,
		ignore = false,
	},

	actions = {
		expand_all = {
			max_folder_discovery = 100,
			exclude = { '.git', 'target', 'build' },
		},
	},

	on_attach = 'default'
}

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?