Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim: how to replace something only in a column

Tags:

replace

vim

I want to replace to symbols ":" and "-" both to tab in the first column of a file:

The input is like:

chr1:100-200 1 2 3e-4
chr2:300-400 4 5 6e-4

And I want the output to be:

chr1 100 200 1 2 3e-4
chr2 300 400 4 5 6e-4

I know how to do replacement globally by "%s/:/^I/g" to replace ":" to a tab.

But because some of the entries have numbers in scientific notation such as 3e-4, I can not just use "%s/-/^I/g" to replace "-" to a tab.

Does anyone know how to specify replacement only to the first column?

Thanks.

like image 495
olala Avatar asked Dec 17 '25 19:12

olala


1 Answers

You can use "Ctrl+V", active the "VISUAL BLOCK" mode, select the columns to be changed, press "c", make the change, and then "Esc", it will be applied to all selected columns.

like image 162
Saullo G. P. Castro Avatar answered Dec 20 '25 11:12

Saullo G. P. Castro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!