Sunday, September 23, 2012

vi: Removing Endline Character ^M

A text file commutes between Linux and Windows may cause the endline control character explicitly printed as ^M.
To remove all control characters, under command mode
:%s/^M//g
Be aware that ^M is created by pressing ctrl + v (^) and then ctrl + M (M).
The command is utilizing the Find and Replace under vi:
:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
Ranges are
% - the whole file
m,n - from line m to n
Pattern see vim reference.
Flags are
c - asks for confirmation at each replacement 
g - all the occurrences within a line
i - case insensitive
Count means to repeat for n times starting from the current line

No comments:

Post a Comment