Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace the first character of each line

Tags:

vim

vi

In vi, how to replace the first character in each line if it is the specified character a with another character !? If the first character is space,do not do anything.

like image 940
witrus Avatar asked Jun 08 '13 02:06

witrus


2 Answers

Global substitute the first character of a line if it is 'a':

:%s/^[a]/!/

Global substitute the first character of a line if it is not space:

:%s/^[^ ]/!/
like image 183
perreal Avatar answered Oct 18 '22 18:10

perreal


Or using global command :g

:g/^a/s//!

this will automatically skip all lines that begin with a space or a character that does not match ^a and replace all matching with !

like image 44
cesar Avatar answered Oct 18 '22 20:10

cesar