diff --git a/README.md b/README.md index 72888f76..b3a4fa99 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,7 @@ highlight NvimTreeFolderIcon guibg=blue - type `]c` to go to next git item - type `[c` to go to prev git item - type `-` to navigate up to the parent directory of the current file/directory +- type `s` to open a file with default system application or a folder with default file manager - if the file is a directory, `` will open the directory otherwise it will open the file in the buffer near the tree - if the file is a symlink, `` will follow the symlink (if the target is a file) - `` will open the file in a vertical split @@ -219,6 +220,7 @@ lua <' to navigate to the next sibling of current file/directory - type 'J' to navigate to the first sibling of current file/directory @@ -410,6 +411,7 @@ Default mappings: { key = "[c", cb = tree_cb("prev_git_item") }, { key = "]c", cb = tree_cb("next_git_item") }, { key = "-", cb = tree_cb("dir_up") }, + { key = "s", cb = tree_cb("system_open") }, { key = "q", cb = tree_cb("close") }, { key = "g?", cb = tree_cb("toggle_help") }, } diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 26f15c2f..c379a3c8 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -89,6 +89,36 @@ local keypress_funcs = { if node.entries ~= nil or node.name == '..' then return end return lib.open_file('preview', node.absolute_path) end, + system_open = function(node) + local system_command = '' + if vim.fn.has('win16') == 1 or vim.fn.has('win32') == 1 or vim.fn.has('win64') == 1 then + system_command = 'start "" ' + elseif vim.fn.has('win32unix') == 1 then + if vim.fn.stridx(vim.fn.system('uname'), 'CYGWIN') ~= -1 then + system_command = 'cygstart ' + else + system_command = 'start "" ' + end + elseif vim.fn.has('mac') == 1 or vim.fn.has('macunix') == 1 then + system_command = 'open ' + elseif vim.fn.has('unix') == 1 then + system_command = 'xdg-open ' + else + vim.cmd('echohl ErrorMsg') + vim.cmd('echomsg "NvimTree system_open: cannot open file with system application. Unsupported platform."') + vim.cmd('echohl None') + return + end + + local command_output = vim.fn.system(system_command .. '"' .. vim.fn.substitute(node.absolute_path, '"', '\\\\"', 'g') .. '"') + + if vim.v.shell_error ~= 0 then + vim.cmd('echohl ErrorMsg') + vim.cmd(string.format('echomsg "NvimTree system_open: return code %d."', vim.v.shell_error)) + vim.cmd('echomsg "' .. vim.fn.substitute(command_output, '\n', '" | echomsg "', 'g') .. '"') + vim.cmd('echohl None') + end + end, } function M.on_keypress(mode) diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 455c6e28..b05d30cc 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -71,6 +71,7 @@ M.View = { { key = "[c", cb = M.nvim_tree_callback("prev_git_item") }, { key = "]c", cb = M.nvim_tree_callback("next_git_item") }, { key = "-", cb = M.nvim_tree_callback("dir_up") }, + { key = "s", cb = M.nvim_tree_callback("system_open") }, { key = "q", cb = M.nvim_tree_callback("close") }, { key = "g?", cb = M.nvim_tree_callback("toggle_help") } }