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

[英]Vim: Get content of syntax element under cursor


I'm on a highlighted complex syntax element and would like to get it's content. Can you think of any way to do this?

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

Maybe there's some way to search for a regular expression so that it contains the cursor?

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

EDIT

编辑

Okay, example. The cursor is inside a string, and I want to get the text, the content of this syntactic element. Consider the following line:

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

String myString = "Foobar, [CURSOR]cool \"string\""; // And some other "rubbish"

I want to write a function that returns

我想写一个返回的函数

"Foobar, cool \"string\""

4 个解决方案

#1


6  

if I understood the question. I found this gem some time ago and don't remember where but i used to understand how syntax hilighting works in vim:

如果我明白这个问题。我不久前发现了这个宝石,不记得在哪里,但我曾经理解语法高亮在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

#2


3  

The textobj-syntax plugin might help. It creates a custom text object, so that you can run viy to visually select the current syntax highlighted element. The plugin depends on the textobj-user plugin, which is a framework for creating custom text objects.

textobj-syntax插件可能有所帮助。它创建一个自定义文本对象,以便您可以运行viy以直观地选择当前语法高亮显示的元素。该插件依赖于textobj-user插件,后者是一个用于创建自定义文本对象的框架。

#3


1  

This is a good use for text objects (:help text-objects). To get the content you're looking for (Foobar, cool \"string\"), you can just do:

这对文本对象很有用(:help text-objects)。要获取您正在寻找的内容(Foobar,cool \“string \”),您可以这样做:

yi"

y = yank
i" = the text object "inner quoted string"

The yank command by default uses the unnamed register ("", see :help registers), so you can access the yanked contents programmatically using the getreg() function or the shorthand @{register-name}:

默认情况下,yank命令使用未命名的寄存器(“”,请参阅:help寄存器),因此您可以使用getreg()函数或简写@ {register-name}以编程方式访问抽出的内容:

:echo 'String last yanked was:' getreg('"')
:echo 'String last yanked was:' @"

Or you can yank the contents into a different register:

或者您可以将内容抽入不同的寄存器:

"qyi"

yanks the inner quoted string into the "q register, so it doesn't conflict with standard register usage (and can be accessed as the @q variable).

将内部引用的字符串拉入“q寄存器,因此它不会与标准寄存器使用冲突(并且可以作为@q变量访问)。

#4


0  

EDIT: Seeing that the plugin mentioned by nelstrom works similar to my original approach, I settled on this slightly more elegant solution:

编辑:看到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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2011/04/28/fd054460fe61a6a42f82590b613d1dc0.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号