2

Upon writing a code, sometimes I open a terminal split with :term to test it by issuing some commands and checking some output. As an example, this is a fragment of what I have executed in a split terminal:

...
I$[12:38]~/Stack>cat file
time;area;temperature;pumpmotor;diameter
1;2;3;4;5
6;7;8;9;10
I$[12:38]~/Stack>awk -f measurements.awk file
I$[12:38]~/Stack>cat *.csv
time,area,diameter
1,2,5
6,7,10
...

Now, I wish to copy that that snippet and paste it on my browser (for an answer in Stack :)), so what I usually do is

  1. C-W N Put the terminal in "text-mode";
  2. Position the cursor in the last line I want to copy, in this case 6,7,10;
  3. V Start visual mode and go up to the first line I want to copy, I$[12:38]~/Stack>cat file;
  4. "+y Yank the selection to clipboard.

That does the job as expected, but the prompt is distracting and useless, thus I want to reduce it to I$ (ideally it would shrink to $ , but that would add still more complexity to the task and I prefer to drop it). But since that is a terminal, I cannot modify it with :'<,'>s/\[.*>/ / between steps 3 and 4 to get rid of the prompt before yanking.

Is there some easy intermediate step to sanitize the prompt without resorting to copy the terminal contents to an additional ordinary buffer? In case I were not clear, the clipboard should contain:

I$ cat file
time;area;temperature;pumpmotor;diameter
1;2;3;4;5
6;7;8;9;10
I$ awk -f measurements.awk file
I$ cat *.csv
time,area,diameter
1,2,5
6,7,10
  • 2
    Welcome to Vi and Vim! – filbranden Jul 9 at 17:16
  • 1
    @filbranden Thank you for your courtesy :) – Quasímodo Jul 9 at 17:22
  • 1
    I noticed your contributions on Vim questions on the other stacks, so I'm really glad to see you finally made your way here :-D – filbranden Jul 9 at 17:23
1

You could use substitute() on the contents of the register to make a replacement, and use a :let command to assign it back to the clipboard register.

For example, start by copying it to the default register in step 4, with y (instead of "+y.)

And then use the :let command to apply a substitute() on the contents of the default register and assign that to the clipboard register:

:let @+ = substitute(@", '\[[^\n]*>', ' ', 'g')

Note that you need to use a non-greedy version of the regular expression ([^\n]* instead of .*) since substitute() will apply it to the text as a whole and not line by line, so the . will match newlines.

You could of course further automate this by creating a mapping, say <Leader>y from visual mode, that would yank the contents into the default register, perform any necessary modifications and finally store that into the clipboard register.

| improve this answer | |
  • 1
    Thanks for the corrections @Quasímodo! – filbranden Jul 10 at 4:08
  • 1
    You are welcome! I only noticed it when a line with [ showed up. Also, thanks for the thorough explanation, it would have been much more difficult to grasp what was happening without it. – Quasímodo Jul 10 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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