Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do column editing in vim?

Tags:

vim

vi

editor

Vim is pretty powerful when editing by line - the search/replace tools are modeled to work linewise.

But what if I want to change a particular column across all lines? For example, how can I change the 80th column in my file to a # easily?

like image 817
Lazer Avatar asked Aug 07 '11 09:08

Lazer


People also ask

How do I select a column in vim?

Use ctrl+v to select a column of text consisting of the first character of the place you want your new column to go in front of. Use I to go into insert mode, and type one space. Press Esc, and you'll see you have inserted a column of single spaces. Now use ctrl+v again to highlight the column of spaces.

How do I insert a column in VI?

Ctrl + V to go into column mode. Select the columns and rows where you want to enter your text. Shift + i to go into insert mode in column mode.


2 Answers

To edit a column, follow these steps:

  1. Stand on the beginning of the column
  2. Press Ctrl+v, then mark across the column you want to edit.
  3. Press Shift+i to insert text at the beginning of the column, Shift+a to append text, r to replace highlighted text, d to delete, c to change... etc.
  4. Hit ESC when done.

I think people (me) sometimes map the column editing keys to Ctrl+Q so it won't collide with visual select line (V) or paste-text if you mapped it as such.

like image 71
sa125 Avatar answered Sep 22 '22 05:09

sa125


...I couldn't follow the steps of sa125 (facepalm) so I looked someplace else and found a simpler explanation at: https://blog.pivotal.io/labs/labs/column-edit-mode-in-vi

  1. Ctrl+v [ and select what ever you want]
  2. Shift+i [and write whatever...(check out ** below)]
  3. Esc

*c without Shift can be used instead of step 2, to delete selection before insert. And also r to replace.

**!! Attention Don't be discouraged by the fact that only the first row is changed when you 'write whatever...'!!

Hope it helps!

like image 31
Francisco López-Sancho Avatar answered Sep 21 '22 05:09

Francisco López-Sancho