简体   繁体   English

高级vim自动补全

[英]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 - XCtrl - 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的答案略有不同:

  1. Navigate to the first brace { 导航至第一个括号{
  2. Enter visual mode with v 使用v进入视觉模式
  3. Press % to navigate to the end of the block, until the last brace } %导航到块的末尾,直到最后一个括号}
  4. y ank it. y ANK它。
  5. 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中的复制寄存器中,并在适当时将其粘贴。

  1. Navigate to the beginning of the action. 导航至操作开始。
  2. Go to visual mode: v 转到可视模式:v
  3. Move over the part of the text you want to copy/yank. 将光标移到要复制/拖动的部分文本上。
  4. To yank into register '1', type: "1y 要拉入寄存器'1',请输入:“ 1y

Now the action is stored in register number one. 现在,该动作存储在第一个寄存器中。

  1. Paste the content by doing: "1p (that p can be any paste command). 通过执行以下操作来粘贴内容:“ 1p(该p可以是任何粘贴命令)。

Hope this proves to be useful :) 希望这被证明是有用的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM