How to use the vim marking function, but keep the cursor where it is

Vim marking function allows you to apply functions to each line between the current line and the marked line. For example, if I mark the next line 3 with k

 1 var a = 0; 2 while (a < 10){ 3 a++; 4 console.log('Hello'); 5 console.log('world'); 6 β–ˆ 7 } 

and from the cursor position ( β–ˆ ) execute the command >'k , I will get the following

 1 var a = 0; 2 while (a < 10){ 3 β–ˆ a++; 4 console.log('Hello'); 5 console.log('world'); 6 7 } 

(Note: cursors may be above a , but that doesn't matter)

This is the desired effect, but now the cursor has moved completely. In most cases this is desirable since I usually want to edit from above. But in this case, I may want to retreat again, so I need to go to the bottom again. In cases where I backslide 20 lines, this becomes a real job.

How to temporarily disable this search feature?

+4
source share
4 answers

After you do > ' k , just hit ' ' (single quote, single quote) - I think I won’t go back to the tick and you will go back to where you were.

If you do this often, you can map the key to do this in one:

 :map >> >'k'' 

Then when you press >> , it will execute this sequence.

+3
source

The simplest solution is to click ` ` (i.e. go back) after your team returns to the previous place.

+5
source

It depends on how many times you want to repeat this action.

  • If it were 2 or 3 times, I would use:

    '' to return to line 6.
    . to repeat your last command (indent from these lines).

  • If there was more time, I would use the qa macro to start recording, q to complete recording, and <number>@a to repeat it.

+1
source

The most accurate answer I can think of is simple:

 :'k,.> 

Ie, use a command mode command with a range ( :he :range and other sections )

In fact, you can do "remote actions" that will resemble the illusions of levitation for non-vim programmers. Just try

 :'k> 

Indent from the marked line, in the distance! 1

You will find that most interesting editing commands have a command mode version. For instance.

 :'ky|put 

While holding the selected line, place it after the current cursor line.

If the command of the command mode is not there, there is always :normal . For instance. You can

 :'k,.norm ,cc 

using NerdCommenter to comment out a block instead of indentation


Now, for fun:

 :'k,.>|'k,.retab|'k,.y+|u 

To take the same block, reject it, reposition it on it, put it on the Windows / X clipboard and cancel editing (this is perfect for pasting into StackOverflow). Note that in practice, I would rather use a visual selection for this:

 V'k:>|*retab|*y+|u 

1 Fair warning: some "destructive" commands (such as: delete or some script mappings, for example :norm ,cc , to comment on a choice) actually move the cursor

+1
source

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


All Articles