4

Consider something like this nnoremap <silent> <leader>( F(a<cr><esc>vi(:^Uexecute"'<,'>s/,/,\r/gg"<cr>=i(k$%i,<cr><esc>%:nohlsearch<cr> It expands arguments of a function in lines. (You can run it on (a, b, c, d) for example.) The problem is things happen in front of my eyes while I expect it to happen in background and only show me the final result. I just thought maybe there's a way to make vim do some commands in background and just show the final result on screen.

3
  • The question is not how to change the above mapping so that it behaves nice. Suppose that we can't change it. The question is about an ability in vim described above.
    – Sassan
    Commented Oct 1, 2016 at 9:06
  • 1
    Does :set lazyredraw help?
    – VanLaser
    Commented Oct 1, 2016 at 11:47
  • Yeah thanks. If you mind adding an answer I'll surely accept it. Would be great if you add some explanation (I read the docs, but for others who come here.)
    – Sassan
    Commented Oct 1, 2016 at 14:21

3 Answers 3

5

To avoid "seeing" the effect of each command run from a map or macro, use:

set lazyredraw

.. so that the screen is only redrawn once at the end - which makes the whole process (much) faster (see :h lz).

6
  • I'm wondering if plugins set this temporary on when they process text? Or they process text in a string and then replace it in editor? What's the best practice here?
    – Sassan
    Commented Oct 1, 2016 at 14:32
  • And I also wonder why it's not on by default.
    – Sassan
    Commented Oct 1, 2016 at 14:33
  • With Vim, there are many things that can be changed by default. Since you use Neovim, you can suggest it to the devs to enable by default. The best practice for a plugin is to read the settings value, enable it if needed for the duration of plugin processing, then restore it to its previous value.
    – VanLaser
    Commented Oct 1, 2016 at 14:40
  • lazyredraw is disabled by default because it is generally not needed.
    – romainl
    Commented Oct 1, 2016 at 14:43
  • github.com/tpope/vim-sensible/issues/78
    – VanLaser
    Commented Oct 1, 2016 at 14:44
3

First, the right hand side of your mapping has errors so it can't do anything useful in its current state. Here is a fixed version:

nnoremap <silent> <leader>( F(a<cr><esc>vi(:'<,'>s/,/,\r/g<cr>=i(k$%i,<cr><esc>%:nohlsearch<cr>

Second, that mapping is instantaneous so there's no need to run anything in the background at all.

seeing is believing

6
  • ^U got lost in copy and paste, I updated the question.It's not instantaneous, I can clearly first first line goes to new line, then I clearly see that inside the parenthesis gets selected, then I clearly see that all commas get highlighted and suddenly it's done.
    – Sassan
    Commented Oct 1, 2016 at 11:16
  • It should be instantaneous. It is, here, both in 7.3 and in 8.0.3, with or without my custom setup. The question is not how to make commands run in the background but why your Vim doesn't behave as it should.
    – romainl
    Commented Oct 1, 2016 at 12:33
  • Yeah, you're right, if it's instantaneous for you there's something wrong with my setup. I'm using neovim but I doubt if that's related. It got solved by setting :set lazyredraw as @VanLacer suggested in a comment. Its default value is off so I wonder why it works instantaneously on your setup. Can you check its value?
    – Sassan
    Commented Oct 1, 2016 at 14:23
  • @Sassan lazyredraw is disabled (I never felt the need to enable it) and I use Vim. Please tag your questions with "neovim" in the future.
    – romainl
    Commented Oct 1, 2016 at 14:39
  • It's also possible that the problem is elsewhere, perhaps something to do with speed of redrawing of the terminal (?)
    – VanLaser
    Commented Oct 1, 2016 at 14:43
0

AFAIK, we can't run vim commands in background. At best we can run external processes.

Or may be start another vim process and ask it to do thing through client-server mode. But if you want to transform the current text, it won't work very well IMO

2
  • Maybe the word background is misleading, it's totally alright if vim is not responsive at the time it's processing the rhs of the map (which is ~.2 sec in my computer.) So I don't expect it to run a process async in background or things like that. I just don't want to see the intermediate steps. Like lots of plugins out there that process text in vim and you don't see intermediate steps of their process.
    – Sassan
    Commented Oct 1, 2016 at 11:18
  • For example when you indent 1000 lines of code, vim informs you every 50 lines it indents. But you don't see these lines indented until the whole process is finished. Meanwhile vim is not responsive.
    – Sassan
    Commented Oct 1, 2016 at 11:20

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.