3

Let's say I'm editing this line of JavaScript in Vim:

var opener = 'As Gregor Samsa awoke one morning from uneasy dreams he found himself transformed in his bed into a gigantic insect';

If my cursor is on the G in Gregor, how do I move it to the t in insect?

I'm not talking about moving to the end of the line. I'm talking about moving to the end of the JavaScript string encapsulated in the single quotation marks. (I know I can do it with /'h, but I'm wondering if there's a more generic command—maybe something like %.)

Joe Mornin
  • 8,766
  • 18
  • 57
  • 82
  • possible duplicate of [How to move to end of line in vim](http://stackoverflow.com/questions/105721/how-to-move-to-end-of-line-in-vim) – James Oravec Jul 02 '15 at 01:11
  • http://stackoverflow.com/questions/105721/how-to-move-to-end-of-line-in-vim – James Oravec Jul 02 '15 at 01:11
  • 1
    You've misread the question. I've edited it to emphasize that I'm talking about moving to the end of the _string_, not the end of the _line_. – Joe Mornin Jul 02 '15 at 01:18
  • Esc, /', enter, i. This will take you to command mode, search for the next ' then take you back to edit mode. – James Oravec Jul 02 '15 at 13:25

4 Answers4

5

There's vi'<Esc>, which goes into visual mode with the selection comprising the interior of the current string, and the cursor at the end of the selection — then leaves visual mode.

I'm not sure if there's a better way.

hobbs
  • 223,387
  • 19
  • 210
  • 288
2

The easiest way would be t', but this would only work if the string ends on the same line and there are no escaped quotes in the string.

The cleanest way to achieve this would be as @hobbs already pointed out to use visual mode to select the whole (inside) text object (vi'<Esc>). With o (before hitting <Esc>) you can also toggle the end of this selection.

You can use a mapping for this, something like:

:noremap [ vi'o<Esc>

:noremap ] vi'<Esc>

And finally there is a plugin (https://github.com/tommcdo/vim-ninja-feet) from Tom McDonald that handle exactly your problem.

Mathias Begert
  • 2,354
  • 1
  • 16
  • 26
0

I thought the easiest way is just f', since hardly can we see another ' before this tag. It's more convenient even though not like % for ( and {.

Lerner Zhang
  • 6,184
  • 2
  • 49
  • 66
-1

Use e to the end of Gregor and $ to the end of the line.

Jaimy
  • 227
  • 1
  • 8