简体   繁体   中英

vim shift selection in column mode (not text)

Suppose I have selected a column of text in visual mode and edited the same. Now I want to move the selection (not text) to the next column so that I can edit those entries in the second column. How can I do the same.

Detailed

+------+-------+-------+ 
| line | A     | B     |
+------+-------+-------+
| 1    | 2     | 4     |
| 2    | 4     | 4     |
| 3    | 0     | 3     |
| 4    | 3     | 1     |
| 5    | 6     | 1     |
+------+-------+-------+

Lets assume that I have selected lines 1,2,3 of column A (ie I selected the elements 2,4,0). I incremented each of these numbers. Now I want to move the selection to column B (ie select elements 4,4,3). Now I want to decrement the selected elements in column B

You're looking for 1v . This re-creates a visual selection of the same mode ( blockwise in your example) and of the same dimensions (width and height, as much as possible), but starting at the current cursor position. You need to have applied a change to the first selection, though!

So, once you've worked on your first column (eg increased it via <CA> ), just move to the next one ( 2w , f4 , ...), and type 1v .

If you incremented the 2 4 0 with selection and Ctrl-A , you still want to increment the element 4 4 3 , you don't have to shift the selection. You can just move the cursor on the first 4 in column B , and press .(dot) to repeat the previous Ctrl-a . Then 4 4 3 will be 5 5 4

update

regarding your comment, it seems that you want to do different operation on the "selection". You can do following:

  • Ctrl-V select the column A ( 2,4,0 ), do something
  • move cursor to column B , first line element 4 , then press 1Ctrl-V

You should see that 4,4,3 is selected. So the trick is the [count]ctrl-v , we pressed 1 as count, vim will select same number of lines as your last selection from your cursor starting point. read :h ctrl-v for details.

Most modern editors have a feature called "multiple cursors," which is what it sounds like, multiple cursors for editing multiple places at once with the same keystrokes. It's an efficient and visually easy feature to do tasks exactly like this. By default in Vim there's no way to do this, and instead you have to inefficiently glue together movements and counts and commands to try to achieve what you want.

Fortunately there's a plugin that gets us about 50% of the way to efficient multiple cursors, vim-multiple-cursors . Due to the limited nature of Vim's interface (like it's impossible to have an offscreen cursor) it can be fairly buggy, but in this example it works quite well.

在此处输入图片说明

Keystrokes for this GIF:

  • (Starting on row 1, column A)
  • CTRL v Visual block mode
  • j j Move selection down 2 lines
  • CTRL n Enter multiple cursors mode. Turns the start of the visual block on each line into a mostly-functional Vim cursor.
  • CTRL a increment at every cursor position
  • w w Move every cursor over 2 words (to column b)
  • CTRL x Decrement at every cursor position
  • ESC Exit multiple cursor mode.

Multiple cursors are a fast and intuitive way to edit repetitive data, in row/column or any other format. Unfortunately Vim will never be able to match the speed and effifiency of other editor's multiple cursors, but this plugin is still very useful for tasks like this.

My solution: :4,6norm 3w Ctrl-v Ctrl-a 2w Ctrl-v Ctrl-x

:4,6 .......... interval where we will perform the change
norm .......... normal mode
3w ............ jump to the seccond column
Ctrl-v ........ used to insert literal key combinations
Ctrl-a ........ increase number
2w ............ jump to the next column
Ctrl-v ........ again literal combination stuff
Ctrl-x ........ decrease number

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM