|
|
Removing text file EOL?
|
|
|
|
Dedicated MacNNer
Join Date: Feb 2003
Status:
Offline
|
|
I've been using Vim for text processing and would like to remove the EOL code in all my files (a grep function?) so that when used in another application they no longer stay at 72 spaces. ie Lines now wrap to the page. Thanks.
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Assuming the files look something like this:
Code:
This is some text
that gets cut off
rather poorly.
It has two line
breaks between
paragraphs.
The following script will clean up the line breaks in the middle of the sentences:
Code:
#!/usr/bin/ruby -ni.bak
out = if $_ == "\n"
$last == "\n" ? "\n" : "\n\n"
else
$_.chomp
end
print out
$last = $_
It will save backups as <filename>.bak in case something goes wrong. Remove the ".bak" at the end of the shebang line if you don't want it to do that.
(
Last edited by Chuckit; Jul 12, 2005 at 07:26 PM.
)
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Feb 2003
Status:
Offline
|
|
The resposnible item in my .vimrc is:
" Set default textwidth
set tw=72
Do I use your source as a shell script and give it the text file name?
ie test.sh mytestfile.txt
Thanks!
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Yep. It's actually a Ruby script, but both Panther and Tiger include Ruby by default, so it shouldn't make a difference unless you're using a very old OS version.
By the way, I don't use Vim much, but a Google suggests that you can tell it not to wrap with "set nowrap".
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status:
Offline
|
|
If you just want to disable textwidth you can just use set tw=0
Depending on what you're editing, "undoing" the wrapping can be a harder issue. That ruby script will work above if it's something like paragraphs and text in LaTeX, but not well if it's code.
|
"Against stupidity, the gods themselves contend in vain" (Schiller)
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
To be specific, it will work for anything where single newlines in the middle of text signal wrapping and multiple newlines signal intentional paragraph breaks. This isn't the case for a lot of code (in fact, it would ruin the script if you ran it on itself), but there's really no way around that without writing a parser for the particular language involved to determine whether or not a line break needs to be there.
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|