Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am trying to substitute newline (^J) by character X. I tried control V control J under vi, but it didn't work. How can I escape the newline character in vi? Thanks in advance.

vi won't replace newlines - they are treated differently from other characters.
You could try variations of the "Join" command, e.g:
:%j
or constructions like:
:g/x$/-2j
(which would find all lines ending with 'x' and perform a Join on the line two lines above - that wouldn't make much sense, but it shows the kind of thing you can do.)

A neat capability of vi is to send a specified set of lines to some host command and replace those lines with the output of that host command (stdout or stderr).
Create the following one-liner script called joinx, and make it executable:
awk '{printf "%sX",$0}'
Now in vi, set your cursor on the first line of the script (or first line you want to start joining) and type:
!Gjoinx
That will send current line thru last line to the joinx script and replace those same lines with output of joinx, which will be all those lines joined together, with newlines changed to an X (including the last line, which also ends with a newline).
If you wanted to joinx only 10 lines, then you would go to the first of those 10 lines and type:
!9jjoinx
Or maybe up to the next empty line (end of paragraph):
!}joinx
You can do all sorts of specialty things with this approach. A very common usage is to re-wrap messy text by sending to the adjust command. To adjust one paragraph, place cursor on first line of paragraph and:
!}adjust
or to override the default margin of 72:
!}adjust -m64
If you get undesired results (such as an error message returned), just undo.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |