7

From the following text:

variable1 = 'example'
foo = 'foo'
barett = 'ver'

I would like to yank (or visually select, if possible) the content of each line until the = sign:

variable1
foo
barett

That is: "yank/visually select the contents of the following lines, stopping the yanking/selection in each line as soon as an = character is found"

If I were dealing with a single line it would be simple> from the beginning of the line: yt=

But I have no clue how to do this with multiple lines (and visual block won't help since I don't want the same number of characters from each line).

3 Answers 3

4

I'm not sure this is what you want, but you could try this mapping:

xno my y'>pV']:s/=.*//<cr>gvd

To use it, you would visually select your lines, then hit my. It should write the text before the = character on each line of your selection, inside the unnamed register.

Here's what it does:

  • y yanks the selection
  • '> moves the cursor to the last line of the selection
  • p puts the unnamed register, where currently the last yanked text is
  • V goes into visual line mode
  • '] moves the end of the selection down to the last line of the last text on which you performed an operation (here the yanking)
  • :s/=.*//<cr> removes everything after the = character, on each line of the selection
  • gv reselects the last selection
  • d deletes it
2
  • 1
    I'm giving it a go. Maybe instead of the last yu I could use d? Otherwise the uundoes the :s/=.*//<cr>, not the pasting. Jun 27, 2017 at 12:44
  • @user2891462 Indeed, you're right, d should work too, and it's shorter.
    – user547381
    Jun 27, 2017 at 12:50
1
  • ytx – yank from the current cursor position up to and before the character (til x)
  • yfx – yank from the current cursor position up to and including the character (find x)
1
  • 2
    Thanks Natan. I now how to do it in a single line (I mention it in the question), what I wanted to know is how to do it on multiple lines at the same time. Check out @user547381's answer for something which worked for me. Jun 13, 2022 at 12:34
0

The following should work without pasting/deleting:

:let @a = ''
[visually select, or don't to use the whole file]
:global/=/normal! 0"Ayt=
  1. Empty an alphabetic register (qaq works for this too)
  2. On each line (possibly in the range) matching =, do 0"Ayt=, which appends to the a register as it yanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.