tags:

views:

116

answers:

2

I'm trying NERDtree which is pretty cool, but what I'd like to do is execute special commands, or scripts, on the selected file.

For example, I'd like to highlight an image file in the tree, hit some key and have the appropriate XHTML tag inserted in the original file (I have a small script to do the formatting, I just need to run it on the file). Or insert a 'require modulename' when hitting another key while modulename is selected. Or, well I guess you got the point.

Can it be done in NERDtree, or is there any other plugin that allows this?

Thanks

EDIT
I thought of this solution: I run the command on the file under the cursor in the browser window, capture its output in a register, switch back to the previous window and paste the content of the register.

The problem with this approach is that the file in the browser window has no reference to its full path, so it's just a filename and thus basically useless unless you just work with stuff in the cwd.

A: 
:!<cfile>

This will execute the current file under the cursor (in windows, this means that it will open the default program associated with the file). I haven't used NERDTree, but if the file is highlighted, it might not work quite right (it will try to filter the selection through the command you provide).

You can also use this to execute commands on files:

:!notepad <cfile>

Depending on what you want to do, you can also take the content of a file and send it through your script for filtering. Based on your example, this sounds like it might work out. The selected content will be sent to standard input of your program (or script) and the selection will be replaced by it's output. For example, highlight some text and press : and you should see :'<,'> (which are markers for the current selection). Then type ! followed by your command. The result might look like:

:'<,'>!myscript

When you execute this, the part you highlighted will be sent on standard input to myscript and then replaced by the output of myscript.

jheddings
What I need to do is run some command with <cfile> as argument, and insert the output of that command in the buffer I'm editing (which, when browsing in the NERDtree is "the other window").
kemp
+1  A: 

After research I found a solution that seems to do exactly what I wanted. This piece of code shoud be inserted in a file under ~/.vim/nerdtree_plugin (or equivalent directory under other operating systems):

call NERDTreeAddKeyMap({
    \ 'key': 'b',
    \ 'callback': 'NERDTreeInsertImage',
    \ 'quickhelpText': 'Insert XHTML tag of image' })

function! NERDTreeInsertImage()
    let n = g:NERDTreeFileNode.GetSelected()
    if n != {}
     let @i = system("~/perl/image.pl " . n.path.str())
     normal ^Wp"ip
    endif
endfunction

it adds a mapping to key b which runs the function NERDTreeInsertImage() which takes the full path of the selected file in the browser and passes it as an argument to my perl script. Of course ^W is inserted as <C-V><C-W>.

Hope this can be helpful to some other Vim user :)

kemp