Mu Mind
Mu Mind

Reputation: 11194

Get whole string literal under cursor

Vim has expand("<cword>") to get the "word" under the cursor. If the cursor is on a quoted string literal, how would you define a function to get everything between the quotes?

Given this scenario:

foo = "this string has spaces"
          ^ cursor is here

I want to do

:echo GetStringUnderCursor()

and see

this string has spaces


EDIT: I see you can check if the cursor is on a string region with this incantation

synIDattr(synIDtrans(synID(line("."), col("."), 1)), "name") == "String"

I just haven't found a way to look up the start and end positions (line and col) for a given syntax region.

Upvotes: 2

Views: 528

Answers (1)

frevd
frevd

Reputation: 31

For example :

vi"    ->    visual select all inside the quotes
ci"    ->    change all inside the quotes
yi"    ->    yank all inside the quotes

If you yank it, you can do :

echo @"    ->     prints register " (default)

Upvotes: 2

Related Questions