Vim: get the contents of the syntax element under the cursor

I am in the highlighted complex syntax element and want to get its contents. Can you come up with some way to do this?

Maybe there is a way to search for a regular expression so that it contains a cursor?

EDIT

Ok, an example. The cursor is inside the line, and I want to get the text, the contents of this syntax 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\"" 
+6
source share
4 answers

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

 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 
0
source

if I understood the question. I found this gem a while ago and don’t remember where, but I used to understand how the hilighting syntax works in 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 
+6
source

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

+3
source

This is useful for text objects ( :help text-objects ). To get the content you are looking for ( Foobar, cool \"string\" ), you can simply do:

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

The yank command uses an unnamed case by default ( "" , see :help registers ), so you can programmatically access available content using the getreg() function or the transcript @{register-name} :

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

Or you can load the contents into a different register:

 "qyi" 

stores the internal string with quotes in the register "q , so it does not conflict with the standard use of the register (and may be available as the variable @q ).

+1
source

Source: https://habr.com/ru/post/886892/


All Articles