📜 ⬆️ ⬇️

Calling up the system handler application for the selected file in NERDTree in Vim

Recently, I have been actively using Vim as a text editor, and the NERDTree plugin for navigating through the file system. And I really didn’t have enough opportunity in it to launch a standard handler application for the file selected in the tree. Searching a little and not finding it, I decided to write my own plugin.

The idea of ​​implementation spied in netrw. I didn’t support win, so it only works under * nix (gnome / kde).

To install, you need to write the whole thing into a file (for example, execute_menuitem.vim), and put it in the daddy ~ / .vim / nerdtree_plugin. By the way, it should already contain the fs_menu.vim plugin, which is required for my plugin to work, and is included in NERDTree by default.

How it works: in the tree, select the file or folder, press 'm' to bring up the menu, and 'x' to launch the processing application. Everything.
')
" ============================================================================
" File: execute_menuitem.vim
" Description: plugin for NERD Tree that provides an execute menu item, that
" executes system default application for file or directory
" ============================================================================
if exists("g:loaded_nerdtree_shell_exec_menuitem")
finish
endif

let g:loaded_nerdtree_shell_exec_menuitem = 1
let g:haskdeinit = system("ps -e") =~ 'kdeinit'

call NERDTreeAddMenuItem({
\ 'text': 'e(x)ecute',
\ 'shortcut': 'x',
\ 'callback': 'NERDTreeExecute' })

function! NERDTreeExecute()
let treenode = g:NERDTreeFileNode.GetSelected()
let path = treenode.path.str()

if has("unix") && executable("gnome-open") && !g:haskdeinit
exe "silent !gnome-open ".shellescape(path,1)." > /dev/null"
let ret= v:shell_error
elseif has("unix") && executable("kfmclient") && g:haskdeinit
exec "silent !kfmclient exec ".shellescape(path,1)." > /dev/null"
let ret= v:shell_error
end
redraw!
endfunction

Source: https://habr.com/ru/post/94758/


All Articles