Vim:获取光标下语法元素的内容

bli*_*nry 7 regex vim syntax-highlighting

我正在使用突出显示的复杂语法元素,并希望获得它的内容.你能想到办法吗?

也许有一些方法来搜索正则表达式,以便它包含光标

编辑

好的,例子.光标在一个字符串里面,我想得到文本,这个句法元素的内容.请考虑以下行:

String myString = "Foobar, [CURSOR]cool \"string\""; // And some other "rubbish"
Run Code Online (Sandbox Code Playgroud)

我想写一个返回的函数

"Foobar, cool \"string\""
Run Code Online (Sandbox Code Playgroud)

小智 7

如果我明白这个问题.我不久前发现了这个宝石,不记得在哪里,但我曾经理解语法高亮在vim中是如何工作的:

" Show syntax highlighting groups for word under cursor
nmap <leader>z :call <SID>SynStack()<CR>
function! <SID>SynStack()
  if !exists("*synstack")
    return
  endif
  echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
endfunc
Run Code Online (Sandbox Code Playgroud)


bli*_*nry 0

编辑:看到 nelstrom 提到的插件的工作原理与我原来的方法类似,我选择了这个稍微更优雅的解决方案:

fu s:OnLink()
    let stack = synstack(line("."), col("."))
    return !empty(stack)
endf

normal mc 

normal $
let lineLength = col(".")
normal `c

while col(".") > 1
    normal h
    if !s:OnLink()
        normal l
        break
    endif
endwhile
normal ma`c

while col(".") < lineLength
    normal l
    if !s:OnLink()
        normal h
        break
    endif
endwhile
normal mb`av`by
Run Code Online (Sandbox Code Playgroud)