Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perfoming Vim substitue on a partial line

Tags:

vim

editor

we can perform vim substitute on a set of lines by selecting them in visual mode and entering command mode. it auto populates the selected range
:'<,'>

and we can perform substitute like :'<,'>s/TestSubstitute//gc

I would like to know is it possible in vim to perform substitute command on a partial line selected in visual mode, so something like

:`<,``> [followed by substitute command]

Generally,i will copy a set of field names separated by delimeter "," in vim and would like to calculate the column count which can be determined by the number of comma occurrences once all the field names are selected in visual mode.

select fname,lastmodtime,lastaccesstime from fileInfo;

If i select the text from fname to lastaccesstime in visual mode and would like to know the number of commas in the selected text.

Thanks in advance,
Naga Kiran

like image 616
Naga Kiran Avatar asked Sep 05 '09 18:09

Naga Kiran


1 Answers

Use \%V modifier in pattern expression. It will force vim to match only within visual block you're currently in or were in before. Use the modifier like this:

:s/\%Vpattern/substitution/

In visual mode this will be shown as:.

:'<,'>s/\%Vpattern/substitution/
like image 149
P Shved Avatar answered Oct 31 '22 02:10

P Shved