3

I'm using the latest version of vim 8.2.148 on the latest version of arch linux 5.5.6-arch1-1.

If I want to copy from the cursor until a letter further on in the sentence, I can do that, for example in this sentence, if my cursor was at the start of the sentence and I wanted to yank until the c of copy I would give the command yuc, which would copy "If I want to".

Another equally valid example would be to yank from the start of the string to the start of the second instance of "screenshots" in this string:

/home/user/linux_config/script_yad/screenshots/change_screenshots_directory.sh

However if my cursor was at the start of this sentence and I wanted to yank up until and including the second instance of sentence, how would I do that?

I think such a command might look structurally something like yt2"sentence" though this command does not work.

Obviously using the letter s to yank will not be satisfactory.

I wish to achieve this without using visual mode.

Does this need a plugin, and if so which one?

Can this be done without a plugin and how?

5
  • 1
    Why do you want to achieve this without using visual mode? This is pretty much the exact kind of scenario visual mode was made for...
    – Chris Down
    Mar 6, 2020 at 12:20
  • Could you provide an example of the string, and what you expect? Mar 6, 2020 at 12:20
  • 1
    @chris-down the word "sentence", in the example I give, marks a more specific location than the first letter of sentence, which is "s". If I can choose the 2 instance of "sentence" I can mark the exact spot I want to yank until. I feel - my opinion - that if I have to use visual mode then I might as well use a mouse.
    – Kes
    Mar 6, 2020 at 13:20
  • @guillermo-chamorro the example I give above in the description is accurate. Another equally valid example would be to yank from the start of the string to the start of the second instance of "screenshots" in this string /home/user/linux_config/script_yad/screenshots/change_screenshots_directory.sh
    – Kes
    Mar 6, 2020 at 13:35
  • Since no one else has I'll plug it: we have a stackexchange site dedicated to vim and vi Mar 6, 2020 at 20:57

1 Answer 1

6

You can use:

y2/screenshot<CR>

Or, more briefly:

y2/screen<CR>

This will yank from the cursor position until the second instance of the word "screenshot" on that same line.

(Note that <CR> here means pressing the "Return" key, to complete the search.)

If you have incremental search highlighting enabled (set incsearch), you'll see Vim highlighting that second match.

You seem to be mixing up the motion to a character with the motion to a sentence. The t motion (for "until", it's t and not u) only takes a single character. Another way to yank to that same location would be y6ts, but that would require you to count the s's in the sentence to see that the one at the start of the second instance of "screenshot" is the 6th s on that line. (There are plug-ins that can help with that.)

There are caveats when using / to search (versus t, f, T or F motions), in that / search can find matches on a different line and, if that happens, your yank operation will now fetch contents that includes newlines, which might be surprising or unexpected when pasting it later on.

Finally, using visual mode is a great way to have visual feedback and be able to adjust your selection for an operation, when the motion to use isn't obvious or you need more flexibility.

For example, you could start with vts and then press ; to repeat that motion until you get to the second "screenshot" (5 times, it turns out.) Easier than counting it's the 6th without the highlighting! Once you're there, you can just press y to yank it.

Or v/screen<CR>, then you realize it's the next one, press n, then you notice the initial s is selected, press h to move right once, then y to yank it.

(Addressing your comment: the visual mode solutions above are superior to using a mouse IMO. vts;;;;;y is 9 keystrokes but 5 of them on the same key. Plus, your hands haven't left the keyboard. And you haven't had to count letters before the selection, you only count with visual help. Dragging the mouse is slower, in comparison.)

If you want to look into a plug-in that might help with this kind of motion, vim-easymotion is a good one to consider.

4
  • 1
    Thanks. That worked. I tried that solution earliear but some how got the syntax mixed up so didn't work. You've nailed it. Cheers.
    – Kes
    Mar 6, 2020 at 14:31
  • 2
    your yank operation will now happen in linewise mode, so you'll yank "n" full lines rather than from the cursor to the start of the match. No, it will still be a characterwise yank. Unless you do y2V/screenshot<cr> to force it to be linewise.
    – DJMcMayhem
    Mar 6, 2020 at 21:41
  • Great catch @James! For some reason I expected that to be a linewise motion since it was finding a result on a different line... Updated my answer with the correction, thanks!
    – filbranden
    Mar 6, 2020 at 22:06
  • 1
    No problem! I was really surprised that it's character-wise as well.
    – DJMcMayhem
    Mar 6, 2020 at 22:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .