0

I can do a search to highlight multiple unrelated words that exist with:

/word1\|word2\|word3

But this is only search, it is not "select" (for copy and paste operation)

How to highlight multiple unrelated sentences ?

  1. As in web browser, I can highlight any lines of sentences or any words and line mix that is not on the same line (by holding down "shift key"), to copy them , so that i can paste them into an empty space.

Like so: do a multi select multi select I have a huge note of all important event taken place daily. When i browse thru the note,i want to extract out all the related events that i need, and paste it into an empty space (to group them together, instead of scattered all around). I was thinking maybe vim have the same capability as in browser, to multi select for copy and paste.

If vim don't have such function, how do you do such a task ? For example, the txt file is 100 pages long, i am somewhere in the middle of the page (where the search landed me). I can see multiple lines, scattered around on my screen that are of my interest. I want to copy them and paste them to an empty space of the file (which is at the bottom of the 100 pages file) or paste into new file in splited window. I can't just copy one line then go to bottom of the page to paste and then come back to the same screen to repeat for the rest of the lines, because on the current screen itself has already got 10 lines of interest, and if i scroll down 1 page, there will be another several lines... and the list might goes on for many more pages.. if i just copy 1 line and paste and return to the same place to look for where did i just copied just to copy another line, there will be huge confusion and not productive. If (like in web browser) i can use "shift key" to multi select as i go, it will be faster, i can copy all the lines of interest from current screen and paste once only.. the i can page down to repeat ..

Thxenter image description here

Sorry, my previous post was lacking in clarification. It was closed, hence i repost here.

Is there such a feature in vim : I has got tags, such @result @plan @action @analyze etc in the text file. Is it possible that Vim will shows me all the lines that containt the Tag that i want only; any line that is not have the @tag , will not be display or will not be selected. In this can, i can easily list out only all the line of a certain @tag.

statox
  • 49,782
  • 19
  • 148
  • 225
  • If there is no multi selection function in vim, how you copy multiple scattered sentences or words on screen ? Thx – user15344508 Mar 12 '21 at 19:00
  • Related, possible dupe: https://vi.stackexchange.com/q/4307/10604. (For copy, something like qqq to clear the q register and then :global/pat/yank Q or :…/normal! "Qy…, but also just :global/pat/to … and similar) – D. Ben Knoble Mar 12 '21 at 19:14
  • D.Ben Knoble, The link you gave me is very confusing. Don't understand, somehow it is multiple cursor, but no multiple selection ? (or the "cursor" in the link actually equal to selection? ) What's is your way to do such a task ? copy and paste a line each time ? for the whole text ? is there any better or more efficient way of doing it ? Thx – user15344508 Mar 12 '21 at 20:37
  • I’m not entirely clear that I’ve understand your workflow, but if you find jumping around disorientating, you may want to take a look at either using the jump list or using a vertical split with the bottom of the same file in the other split. – Andrew Ho-Lee Mar 12 '21 at 21:11
  • Ok, let me explain again my work flow : – user15344508 Mar 12 '21 at 21:16
  • @AndrewHo-Lee also marks – D. Ben Knoble Mar 12 '21 at 21:19
  • Ok, let me explain again my work flow : I want to extract lines related to my work from a big file. The lines are scattered everywhere throught the whole file. Sometime i can see several "line of interest" on my screen (still scattered, but there are several on screen), which i would like to highlight them all to be copied and paste to group them together for analyse. If i would have to switch screen here and there (to copy and then paste), that would be confusing. – user15344508 Mar 12 '21 at 21:22
  • A possible workflow would be to go through the file yanking / deleting the text and appending it to a named register. Using the capital letter version of a register appends rather than replaces the content (e.g. ”Ayy). You may want to combine this with > in cpoptions. Apologies, I am on my phone so can’t flesh this out more at the moment but hopefully that will give you a pointer. – Andrew Ho-Lee Mar 12 '21 at 21:32
  • What you said, Everything seems to be new to me. Hence hard to understand what you meant. I don't know where to begin ... – user15344508 Mar 12 '21 at 21:38
  • 1
    I'll post a longer answer which might help, rather than continue this discussion in comments. Your question at the end about displaying only certain lines is a separate question, please make a new post as vi.se intends for one question per post. – Andrew Ho-Lee Mar 12 '21 at 21:53

1 Answers1

3

If you want to copy multiple separate words and paste them at the end of a long document, here are some options.

Option 1: splits

You can open the same buffer side-by-side using :vsplit. You can then jump between the two using window motions such as CTRL-WCTRL-W. So a workflow might look like this:

  1. :vsplit to open the same buffer in a vertical split
  2. G to go to the bottom of the file
  3. CTRL-WCTRL-W to go back to the left pane

Then, you can go through yanking the words you want, so for each word:

  1. /word1 to go to it
  2. yiw to yank that word into the unnamed register
  3. CTRL-WCTRL-W to go the right pane
  4. p to paste from the unnamed register
  5. CTRL-WCTRL-W to go back to the left pane

As @Rich notes in the comments, you can record a macro to do steps 2-5. For example, to record to the b register you would use qbyiwCTRL-WCTRL-WG:putCTRL-WCTRL-Wq. You can then go to the next word with /word2 and execute the macro with @b. Note that I've used :put in that macro rather than p as this puts the yanked text on a new line, which I suspect is what you want.

More information: :h windows.txt and :h complex-repeat

Option 2: jumps

If you press G to go to the bottom of the document, you can then press CTRL-O to go back to where you were previously. You can then adopt a strategy similar to above.

More information: :h jump-motions

Option 3: marks

More generally, if you want to remember where you are in a document, go elsewhere to do something (e.g. the bottom of the document to paste something) and then return to a certain place, you can also use marks. In this setting, you might use ma to set mark a, navigate away to do something, then return to that place with `a.

More information: :h mark-motions

Option 4: appending to a register

This can be used in combination with any of the above movement strategies. When you yank a word with yiw, you are actually yanking to the unnamed register. This is the equivalent of ""yiw. You can yank to a named register such as a with "ayiw.

If you want to append rather than replace the content of a register, you can use the uppercase version of that register. So, for example, a workflow might look like this:

  1. /word1 to go to a word you want to yank
  2. "ayiw to yank that word into the a register
  3. /word2 to go to another word to yank
  4. "Ayiw to append that word to the a register
  5. repeat for more words...
  6. Then, when you are ready, go to the bottom of the document with G and paste the a register with "ap.

If there is already something in that named register, you can effectively clear it with qaq.

If you use this strategy, you may want to also add > to cpoptions (using :set cpoptions+=> which puts a linebreak before appending text to that register.

More information: :h registers and :h cpoptions.

Option 5: :global

If you know a unique regex that encompasses the line(s) that you want to yank then the :global command can be used.

For example, if you want to yank all lines containing the regex foo to register c, use:

:global/foo/yank C

This can be abbreviated to :g/foo/y C. Note that this is the ex command :yank, not the normal mode mapping y. :yank yanks the whole line to register c.

To yank less than a line, use a normal mode as the command to :global like so:

:global/foo/normal "Cyiw

If doing so, it's likely that you'll want to add > to cpoptions as above.

Andrew Ho-Lee
  • 1,210
  • 5
  • 11
  • If I find myself using your option one for anything more than about 5 occurrences, I'll usually record a macro to automate steps 2-5 so they can be done with a couple of keypresses. – Rich Mar 12 '21 at 22:45
  • Also, +1 for cpo->; I learned something new today! – Rich Mar 12 '21 at 22:47
  • @Rich Good idea for the macro, I've added some information on that. – Andrew Ho-Lee Mar 13 '21 at 00:07
  • My personal favorites include :global/…/ where the command is either :to in the case of whole lines or :normal with some yanking sequence into a capital register (to append, as you said) – D. Ben Knoble Mar 13 '21 at 03:57
  • Thanks @D.BenKnoble I've added a bit about :global. What is :to? :h :to just takes me to :topleft. – Andrew Ho-Lee Mar 13 '21 at 10:15
  • Argh, I forget the command is actually just :t, a synonym for :copy – D. Ben Knoble Mar 13 '21 at 13:51
  • Wow @_@ ! Thanks @Andrew Ho-Lee. – user15344508 Mar 13 '21 at 16:14
  • @AndrewHo-Lee I was using the whole of Option 5, beginning from "Option 5:" ... to end of " cpoptions as above." I executed firstly qcq , then :g/the/yank c , then "cp . the output i got was "To yank less than a line, use a normal mode as the command to :global like so:" .. why ? – user15344508 Mar 13 '21 at 16:38
  • What is :h and :t ? i cant get any useful. :h is vim help and :t does nothing. – user15344508 Mar 13 '21 at 16:41
  • I'm not clear on your description of what is happening when you try and use Option 5, perhaps you could provide some more detail? @D.BenKnoble was pointing out :t which is a synonym for :copy as a possible ex command to use with :global. You can see this by browsing the vim documentation for :t with :h :t. It sounds as if you are fairly new to vim, so you may find How do I navigate to topics in Vim's documentation? useful. – Andrew Ho-Lee Mar 13 '21 at 17:38
  • Also, if you haven't already, I would encourage you to complete vimtutor and read the user manual (:h user-manual) which covers some of these topics. Apologies if this is off the mark! – Andrew Ho-Lee Mar 13 '21 at 17:40
  • @user15344508 should be :g/the/yank C; the capital matters here – D. Ben Knoble Mar 13 '21 at 18:45
  • @D.BenKnoble Wonderful! It works.. I just tried on any piece of vim that happen to open currently and it seems to work!. Why C ? is this C a C register ? – user15344508 Mar 13 '21 at 20:35
  • @AndrewHo-Lee Where is vimtutor ? there is no link to it ? i did pick and read a few section of "user-manual " , it is a huge list of reading.. made me giddy.. lol Thx. Thank you for your time in construct this detailed article. Appreciate it. – user15344508 Mar 13 '21 at 20:38
  • C is the c register but when you use the uppercase version it appends to it rather than replaces the existing content of the register (see :h registers for a more complete description of vim’s registers). This is needed with :global because otherwise it will go through each match and replace the content of the c register each time it matches. – Andrew Ho-Lee Mar 13 '21 at 20:40
  • vimtutor is usually just run from the command line (instead of vim). See How to use vimtutor. – Andrew Ho-Lee Mar 13 '21 at 20:41
  • Understood. Many Thanks @AndrewHo-Lee – user15344508 Mar 13 '21 at 23:45