用于对文件执行命令的 VIM 浏览器插件 [英] VIM browser plugin to execute commands on files

查看:27
本文介绍了用于对文件执行命令的 VIM 浏览器插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试非常酷的 NERDtree,但我想做的是对所选文件执行特殊命令或脚本.

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.

例如,我想突出显示树中的一个图像文件,点击某个键并在原始文件中插入适当的 XHTML 标记(我有一个小脚本来进行格式化,我只需要运行它在文件上).或者在选择 modulename 的情况下按另一个键时插入require modulename".或者,我想你明白了.

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.

是否可以在 NERDtree 中完成,或者是否有其他插件允许这样做?

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

谢谢

编辑
我想到了这个解决方案:我在浏览器窗口中光标下的文件上运行命令,将其输出捕获到寄存器中,切换回上一个窗口并粘贴寄存器的内容.

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.

这种方法的问题在于浏览器窗口中的文件没有对其完整路径的引用,因此它只是一个文件名,因此基本上没有用,除非您只处理 cwd 中的内容.

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.

推荐答案

经过研究,我找到了一个似乎完全符合我的要求的解决方案.这段代码应该插入到~/.vim/nerdtree_plugin下的文件中(或其他操作系统下的等效目录):

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

它添加了一个到键 b 的映射,它运行函数 NERDTreeInsertImage(),它在浏览器中获取所选文件的完整路径并将其作为参数传递给我的 perl 脚本.当然 ^W 插入为 .

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>.

希望这对其他 Vim 用户有所帮助:)

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

@romainl 这是一个非常简单的 Perl 脚本(需要 ImageMagick 模块):

@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="">#;

这篇关于用于对文件执行命令的 VIM 浏览器插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆