VIM browser plugin for executing file commands

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

For example, I would like to select an image file in a tree, press some key and add the corresponding XHTML tag to the source file (I have a small script for formatting, I just need to run it in the file). Or insert 'require modulename' when you press another key while the module name is selected. Or, I think you understand.

Can this be done in NERDtree, or is there another plugin that allows this?

thanks

EDIT
I thought about this solution: I run the command in the file under the cursor in the browser window, write its output to the register, switch back to the previous window and insert the contents of the register.

The problem with this approach is that the file in the browser window does not have a link to its full path, so this is just the name of the file and therefore basically useless if you just don't work with things in cwd.

+2
source share
2 answers

After researching, I found a solution that seems to do exactly what I wanted. This piece of code will be inserted into the file under ~/.vim/nerdtree_plugin (or the equivalent directory on 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 , in which the NERDTreeInsertImage() function is NERDTreeInsertImage() , which takes the full path to the selected file in the browser and passes it as an argument to my perl script. Of course, ^W inserted as <CV><CW> .

Hope this can be useful for some other Vim users :)

@romainl is a very simple Perl script (ImageMagick module required):

 #!/usr/bin/perl use strict; use warnings; use Image::Magick; my $source = $ARGV[0]; my $img = Image::Magick->new; $img->Read($source); my ( $width, $height ) = $img->Get('width', 'height'); print qq#<img src="$source" width="$width" height="$height" alt="">#; 
+2
source
 :!<cfile> 

This will lead to the current file under the cursor (in windows, this means that it will open the default program associated with the file). I did not use NERDTree, but if the file is selected, it may not work correctly (it will try to filter the selection using the command that you provide).

You can also use this to execute commands in files:

 :!notepad <cfile> 

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

 :'<,'>!myscript 

When you do this, the part you select will be sent to standard myscript , and then replaced with myscript output.

0
source

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


All Articles