0

I need to copy a sequence of (normal mode) commands and then paste it to be executed in Vim. When I try that now though it inserts it for me into the document.

How can I paste sequence of Vim (normal mode) commands?

This used to work; now with the auto-insert-mode I don't know how to make it work again, or how to search for this in the documentation.

Using Vim in Terminal on macOS Monterey

Example

JJJj

(three joins and a cursor down one line)

Say I am in huge text file containing repetitive output which I want to transform to make it visually more concise. I don't want to punch in those JJJj every time. Pasting the commands repetitively a few dozen (or hundred) times is faster, maybe even only for tens of times like JJJjJJJjJJJjJJJjJJJjJJJjJJJjJJJjJJJjJJJj.

BEFORE

first line1
first line2
first line3
first line4
second line1
second line2
second line3
second line4
:

AFTER

first line1 first line2 first line3 first line4
second line1 second line2 second line3 second line4
:

This lets me transform thousands of lines within a few seconds.

4
  • 1
    You could try to disable bracketed-paste mode to make this work, but it’s probably easier to turn the commands into a short script you can execute.
    – D. Ben Knoble
    Commented Feb 8 at 1:24
  • I'm not sure to understand the question correctly. In which form do you get the commands (an example would help)? Is it a list of commands each on its own like in a text document? They are separated with |? What do you try? Enter in command mode with : and paste <C-r>+? Commented Feb 8 at 5:42
  • 1
    Are the commands normal mode commands or command-line commands?
    – Rich
    Commented Feb 8 at 16:50
  • 1
    normal mode commands
    – Helmut M
    Commented Feb 8 at 23:42

4 Answers 4

2

How to run normal mode commands from your clipboard

If the normal mode commands are already in your "+ clipboard register then you can run them as a macro by typing:

@+

If your clipboard isn't accessible from Vim (e.g. if Vim is compiled without clipboard support) then you can paste them into your document, yank them (e.g. with y$ or yiw) and then run them from the yank register as a macro by typing:

@0

How I'd actually solve your problem

Instead of writing your commands into your clipboard, I would record them as a macro and then run it a lot of times.

So for your example of running JJJj ten times, I would record a macro with:

qqJJJjq

And then run it 9 more times with:

9@q
2
  • i will try writing macros... its a bit more work. speed is essential. hundreds of files, thousands of lines, not much time... every microsecond adds up. copy&paste was very fast that way. i have commands in separate terminal i can just highlight as needed.
    – Helmut M
    Commented Feb 12 at 7:54
  • Ah okay. If the commands already exist elsewhere and are something you want to re-use then what I'd then personally do is convert them into mappings or Vimscript functions that use :normal. There's no obvious benefit to recording macros if you already have the commands set up. If you do try using macros in the future, then consider using :set lazyredraw to improve their speed.
    – Rich
    Commented Feb 12 at 9:28
1

If the sequence of commands is on separate lines and stored on the system clipboard you can try to run

call execute(split(@*, "\n"), '')

This splits the contents of the clipboard (the star register) into one string per line and passes the resulting list of strings to the execute() function.

1

How can I paste sequence of Vim (normal mode) commands?

This is very unclear to be frank.

simple example e.g. commands = JJJj (three joins and a cursor down one line) say, i am in huge text file containing repetitive output, which i want to transform to make it visually more concise. i don't want to punch in those JJJj every time. but just paste the commands... repetitively a few dozen (or hundred) times. maybe even in 10s, e.g. JJJjJJJjJJJjJJJjJJJjJJJjJJJjJJJjJJJjJJJj

You can use ex-command :join for it:

first line1
first line2
first line3
first line4
second line1
second line2
second line3
second line4

:1,4join and then :2,5join or similar using different :h range variations. Or use visual mode with J to have:

first line1 first line2 first line3 first line4
second line1 second line2 second line3 second line4

asciicast

If it is about "repetitive" thing, you can either use macro recording or a simple :%normal! JJJ over your buffer (or selection):

asciicast

1
  • it was just an example. i'm not confused about the join thing. its just i punch in various commands frequently and need to apply them in fractions of seconds across thousands of lines... visually supervising what's going on. copy&paste is very fast.
    – Helmut M
    Commented Feb 12 at 7:49
1

For key command (i.e. Normal command (like suggested by @Rich) I would do:

@*

Or

@+

Provided you have:

:set clipboard=unnamed

Or

:set clipboard=unnamedplus

For Vim commands I would do:

  • Copy the sequence of commands,
  • Create a new buffer (:enew),
  • Past it into it (p), no need to save the buffer and
  • Run:
:%so
1
  • 1
    @* seems to work well. but one more keystroke (doubles time it takes) ... everything above fractions of seconds is too slow. thanks :)
    – Helmut M
    Commented Feb 12 at 7:51

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.