Change the two characters atomically so that they can be repeated with "."

The idiom for replacing two characters in Vim is xp .

See help :h 04.5 , final paragraph and Vim: how do I exchange two characters? .

But very rarely do I have to perform two or more swap operations per line. I am often surprised that for me one operation - xp - is actually two operations for Vim. This makes it impossible to repeat xp with . . (Or atomic cancel using u .)

Is there a way to exchange two characters in one operation, which I can quickly repeat with . ?

+4
source share
3 answers

Vimcasts.org has published an episode that solves this problem.

As @Jeremy said, the solution is to use the repeat.vim plugin. Then just add the following <Plug> mapping and key mapping to your vimrc.

 nnoremap <silent> <Plug>Transpose xp:call repeat#set("\<Plug>Transpose")<CR> nmap cp <Plug>Transpose 

The mapping shown here uses cp . cp is the new xp repeated with . but otherwise exactly the same.

Now all that remains is getting used to it.

0
source

This is not ideal, but if you store xp as a macro, it will be repeated after the macro is executed. So, instead of xp you use, say @s (for swap). Then @@ will repeat this. Not as good as. But it works.

Edit: you know, I'm sure there is a way to do this with some vimscript and tpope repeat.vim . Unfortunately, my vimscript is not up to tobacco. I got to this - maybe someone can fix it, where am I mistaken?

 fun! DoSwap() :normal xp silent! call repeat#set("\<Plug>Swap",1) endfun nnoremap <silent> <Plug>Swap :call DoSwap()<CR> nmap <Leader>s <Plug>Swap 

The problem is that after entering \s it really replaces the characters you are on, and it repeats when you click . but first it goes to the beginning of the line that this is not what you want. But I think that it can be done, I just do not.

+2
source

Here is an idea. Suppose the cursor is on the "y" of the word "oyxgen".

In normal mode, enter

 2sxy<Esc> 

Now the word reads "oxygen." Find the next typo ( fy ) and repeat the change ( . ).

Too bad this only works for a specific exchange of "yx" to "xy", so this is not a final solution.

0
source

Source: https://habr.com/ru/post/1495109/


All Articles