[英]Advanced vim autocompletion
I am trying to build a lexical analyzer using flex. 我正在尝试使用flex构建一个词法分析器。 Following is a line which shows a regular expression and its corresponding action.
下一行显示了正则表达式及其相应的动作。
[0-9] {printf("yada yada \n");} //regex1
Is there a way to reuse {printf("yada yada \\n");}
from regex1 by auto-completion features of vim, so that I don't need to write the whole thing again while writing regex2? 有没有一种方法可以通过vim的自动完成功能从regex1重用
{printf("yada yada \\n");}
,因此在编写regex2时不需要再次写整个东西?
eg 例如
.* {printf("yada yada \n");} //regex2
This goes beyond word completion, so I was wondering is this doable in vim? 这超出了单词补全功能,所以我想知道这在vim中是否可行?
TIA TIA
Have a look into :h complete-functions
and :h 'completefunc'
for details. 有关详细信息,请参阅
:h complete-functions
和:h 'completefunc'
。
Or dive into the code below: 或深入了解以下代码:
" Scan current buffer for everithing in { }.
fun! GetRegexes()
let rx = '{.\+}'
let res = []
for line in getline(1, '$')
if line =~ rx
call add(res, matchstr(line, rx))
endif
endfor
return res
endfun
fun! MyComplete(findstart, base)
if a:findstart
" locate the start of the completion
let line = getline('.')
let start = col('.') - 1
while start > 0 && line[start] !~ '{'
let start -= 1
endwhile
return start
else
let res = []
for m in GetRegexes()
if m =~ '^' . a:base
call add(res, m)
endif
endfor
return res
endif
endfun
set completefunc=MyComplete
finish
[0-9] {printf("yada yada \n");}
[0-9] {printf("yada \n");}
If you save this file as compl.vim
and source it with :so %
command then you'll be able to start typing {pri
and press Ctrl - X , Ctrl - U to invoke completion menu with 2 elements: 如果将此文件另存为
compl.vim
并使用:so %
命令作为源文件,则可以开始输入{pri
并按Ctrl - X , Ctrl - U调用包含2个元素的完成菜单:
{printf("yada yada \n");}
{printf("yada \n");}
I believe you can adjust it to your needs. 我相信您可以根据需要进行调整。
You can save 2 functions into your .vimrc and set completefunc to MyComlete
for buffer where you need that kind of completion. 您可以将2个函数保存到.vimrc中,并将completefunc设置为
MyComlete
以便在需要这种完成的情况下使用缓冲区。
It is not quite auto-completion, but you could define an abbreviation - eg 它不是自动完成的,但是您可以定义一个缩写-例如
:iab yada {printf("yada yada \n");}
Then in insert mode if you type 然后在插入模式下,如果您键入
[0-9] yada<SPACE>
it will instantly replace yada
with {printf("yada yada \\n");}
. 它会立即替换
yada
与{printf("yada yada \\n");}
See :h abbreviations
for the full scoop. 有关完整的信息,请参见
:h abbreviations
。
Only slightly different than myme's answer: 仅与myme的答案略有不同:
{
{
v
v
进入视觉模式 %
to navigate to the end of the block, until the last brace }
%
导航到块的末尾,直到最后一个括号}
y
ank it. y
ANK它。 p
aste it, wherever. p
ASTE它,走到哪里。 :-) Well, it might not be auto-completion, but it might do the trick for you. 好吧,它可能不是自动完成,但可以为您解决问题。 You could just save that action/print statement to a copy register in vim, and just paste it when it's appropriate.
您可以将动作/打印语句保存到vim中的复制寄存器中,并在适当时将其粘贴。
Now the action is stored in register number one. 现在,该动作存储在第一个寄存器中。
Hope this proves to be useful :) 希望这被证明是有用的:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.