Computing.Net > Forums > Unix > control J in vi

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

control J in vi

Reply to Message Icon

Name: slee
Date: July 31, 2003 at 14:55:05 Pacific
OS: unix
CPU/Ram: alpha
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: WilliamRobertson
Date: July 31, 2003 at 15:44:36 Pacific
Reply:

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.)


0

Response Number 2
Name: James Boothe
Date: August 8, 2003 at 07:55:09 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: control J in vi

Zsh as login shell, and shell in VI www.computing.net/answers/unix/zsh-as-login-shell-and-shell-in-vi/5785.html

question in vi www.computing.net/answers/unix/question-in-vi/4770.html

How to type ?in vi www.computing.net/answers/unix/how-to-type-in-vi-/4344.html