VIM browser plugin to execute commands on files

前端 未结 2 903
粉色の甜心
粉色の甜心 2021-01-07 04:52

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 highligh

相关标签:
2条回答
  • 2021-01-07 05:20
    :!<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.

    0 讨论(0)
  • 2021-01-07 05:22

    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 :)

    @romainl this is the very simple Perl script (requires ImageMagick module):

    #!/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="">#;
    
    0 讨论(0)
提交回复
热议问题