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 Answers
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
).
-
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?– SassanCommented Oct 1, 2016 at 14:32
-
-
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.– VanLaserCommented Oct 1, 2016 at 14:40
-
lazyredraw
is disabled by default because it is generally not needed.– romainlCommented Oct 1, 2016 at 14:43 -
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.
-
^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.– SassanCommented 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.– romainlCommented 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?– SassanCommented 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.– romainlCommented 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 (?)– VanLaserCommented Oct 1, 2016 at 14:43
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
-
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.– SassanCommented 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.– SassanCommented Oct 1, 2016 at 11:20
:set lazyredraw
help?