Lazer
Lazer

Reputation: 94810

How to do column editing in vim?

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?

Upvotes: 87

Views: 120837

Answers (6)

...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!

Upvotes: 61

suno3
suno3

Reputation: 181

To remove all # in the example below.

  1. ctrl + v at # : (Visual Block) mode
  2. drag all # s using arrow down or j
  3. Then press x to remove all # s
# a
# b
# c
# d
# e
# f
  1. Then press Esc to escape from editing
 a
 b
 c
 d
 e
 f

Upvotes: 1

sa125
sa125

Reputation: 28971

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.

Upvotes: 175

Micah Elliott
Micah Elliott

Reputation: 10264

For column-wise editing, vis.vim is really useful. You can block-select your column of interest, and manipulate it with normal commands, and even arbitrary Ex commands. From the example on that page, I have often used the pattern:

:'<,'>B s/abc/ABC/g

You can Vundle/Pathogen install vis.vim from github:

Plugin 'taku-o/vim-vis'

Upvotes: 1

johnsyweb
johnsyweb

Reputation: 141780

You can use a substitution where the pattern matches a specific column (\%c):

:%s/\%80c/#/<CR>

Or you can use block-wise visual mode:

gg80|CTRL+vGr#

The 'virtualedit' option can be used to allow positioning the cursor to positions where there is no actual character:

:set virtualedit

Upvotes: 45

Plouff
Plouff

Reputation: 3470

I may be totally off topic here, but if your idea is to avoid long lines, you could have a look at the colorcolumn option of vim 7.3.

Upvotes: 1

Related Questions