vimrc函数在文件中使用文字'*`搜索文件名,该文件包含所有具有绝对路径的项目文件名列表 [英] vimrc function to search filename using literal `*` in a file which has list of all project filenames with absolute path

查看:148
本文介绍了vimrc函数在文件中使用文字'*`搜索文件名,该文件包含所有具有绝对路径的项目文件名列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为index.txt的大文件,它具有列出的所有文件的所有绝对路径,

  common / mac_get .c 
common / addr / addr_main_set_clear_config.c
common / addr / mac / mac_load_done.c

需要编写vimrc函数来搜索index.txt的每一行中的最后一个单词,分隔符为/(即仅搜索基本名称)并且不匹配文件夹名称,它也应该接受*作为搜索参数的一部分。



说如果我传递参数 mac * .c ,那么它应该搜索以mac&以.c结尾,然后返回结果为,

  common / mac_get.c 
common / addr / mac / mac_load_done。 c

如果我传递参数 mac * done.c 然后它应该搜索文件,以mac开始,结束于done.c,然后返回结果为,

  common / addr / mac / mac_load_done.c 

如果我传递参数 * main * .c 或 * main * set * .c 然后它应该返回结果为,

 common / addr / addr_main_set_clear_config.c 

至今已尝试过:

 函数! searchFname(fname)
执行silent!grep! a:fname〜/ index.txt
endfunction

命令! -nargs = * Fname调用SearchFname('< args>')

任何帮助都将不胜感激。

解决方案

grep code>作为一个与前面的元素匹配零次或多次的量词,但是您需要将它用作匹配任何字符的通配符。



偶数虽然 grep 不使用通配符,但可以构建等效的正则表达式,例如:


  • * 相当于。*

  • 相当于



只匹配文件名(不包括路径),需要用 [^ /] 替换,如下所示: p>

 函数! SearchFname(fname)
执行'silent! grep的! 替换(替换(替换(a:fname,'\ *','[^ /] *','g'),'\' '''''''',''','','[^ /]','g')。'$'''〜/ index.txt'
endfunction

命令! -nargs = * Fname调用SearchFname('< args>')

但是 grep 的输出仍会显示在Vim启动的终端上。为了摆脱它,你可以使用:vimgrep 命令,但是你需要像在Vim中一样使用一个模式(参见 :help:vimgrep

 功能! SearchFname(fname)
执行'silent! vimgrep /^\(.*\/\)\?'。替代(替代(替代(一个:FNAME,\ *', '[^ \ /] *', 'G'),\。', '\。', 'G'),'? ','[^ \ /]','g')。 $ / j〜/ index.txt
endfunction

命令! -nargs = * Fname调用SearchFname('< args>')


I have a large file called index.txt which has all absolute path of all files listed say,

common/mac_get.c
common/addr/addr_main_set_clear_config.c
common/addr/mac/mac_load_done.c

Need to write vimrc function to search only last word in each line of index.txt with delimiter as / (i.e search only basename ) and not match for foldername also it should accept * as a part of argument in search.

say If I pass argument mac*.c then It should search file starting with mac & ending with .c then return results as,

common/mac_get.c
common/addr/mac/mac_load_done.c

say If I pass argument mac*done.c then It should search file starting with mac and ending with done.c then return results as,

common/addr/mac/mac_load_done.c  

say If I pass argument *main*.c or *main*set*.c then It should return results as,

common/addr/addr_main_set_clear_config.c  

This is what I've tried so far :

function! searchFname(fname)
   execute "silent! grep!" a:fname "~/index.txt"
endfunction

command! -nargs=* Fname call SearchFname('<args>')

Any help would be greatly appreciated.

解决方案

grep uses * as a quantifier that matches the preceding element zero or more times, but you're requiring to use it as a wildcard that matches any characters.

Even though grep doesn't use wildcards, you can build equivalent regular expressions, e.g:

  • * is equivalent to .*
  • ? is equivalent to .

To match only filenames (excluding the path), it's necessary to replace . with [^/] as follows:

function! SearchFname(fname)
    execute 'silent! grep! "^\(.*\/\)\?' . substitute(substitute(substitute(a:fname,'\*','[^/]*','g'),'\.','\\.','g'),'?','[^/]','g') . '$"' "~/index.txt"
endfunction

command! -nargs=* Fname call SearchFname('<args>')

But grep's output will still be shown on the terminal Vim was started up. To get rid of it you can use the :vimgrep command instead, but you need to use a pattern as in Vim search (see :help :vimgrep for details):

function! SearchFname(fname)
    execute 'silent! vimgrep /^\(.*\/\)\?' . substitute(substitute(substitute(a:fname,'\*','[^\/]*','g'),'\.','\.','g'),'?','[^\/]','g') . "$/j" "~/index.txt"
endfunction

command! -nargs=* Fname call SearchFname('<args>')

这篇关于vimrc函数在文件中使用文字'*`搜索文件名,该文件包含所有具有绝对路径的项目文件名列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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