3

In emacs, the command "escape-q" word wraps a paragraph. However a lot of text I copy from the internet has paragraphs that each consist of several sentences, but are not separated by blank lines. Also, where code is embedded in the text, unless it is separated by a blank line from the previous text, it gets gathered up into the paragraph destroying the separate lines and indentation of the code.

To word wrap these and preserve the paragraph breaks etc I have to go through the file and insert blank lines, which is long-winded and tedious in a long file..

Is there a way of word wrapping such text, by "select all" on a long file, without inserting blank lines?


Here's an example:

This is some text as copied from the internet, and pasted into emacs

Hello World
 The famous "hello world" program, is the first program every programmer writes. It is one of the simplest possible programs to write, but there is still a lot to learn about what happens below the surface.
 The hello world code in C:

 #include <stdio.h>
 
 int main(int argc, char **argv)
 {
  char str[]="Hello world!!";
  printf("%s\n",str);

  return 0;
 }

There it is, very simple to look at, only 5 real lines of meaningful code. However, all 5 of those lines are worth looking at in detail.

If I select it all and use "escape-q" I get this:

Hello World  The famous "hello world" program, is the first program
every programmer writes. It is one of the simplest possible programs
to write, but there is still a lot to learn about what happens below
the surface.   The hello world code in C:

 #include <stdio.h>    int main(int argc, char **argv)  {   char
str[]="Hello world!!";   printf("%s\n",str);

  return 0;  }

There it is, very simple to look at, only 5 real lines of meaningful
code. However, all 5 of those lines are worth looking at in detail.

And, of course what I really want is this:

Hello World
 The famous "hello world" program, is the first program every programmer writes.
It is one of the simplest possible programs to write, but there is still a lot
to learn about what happens below the surface.
 The hello world code in C:

 #include <stdio.h>
         
 int main(int argc, char **argv)
   {
     char str[]="Hello world!!";
     printf("%s\n",str);

     return 0;
   }

 There it is, very simple to look at, only 5 real lines of meaningful code.
 However, all 5 of those lines are worth looking at in detail.

Acknowledgement: I copied the snippet from this BetterOs.org Tutorial. Thanks to the author for that excellent tutorial, and hope he doesn't mind.

  • Do you want to wrap (setq word-wrap t) (setq truncate-lines nil), or actually create new lines by inserting \n at a certain column -- e.g., 70 or something like that? – lawlist Mar 15 '15 at 16:55
1

You say "To word wrap these and preserve the paragraph breaks etc I have to go through the file and insert blank lines, which is long-winded and tedious in a long file.

If you can do that - manually go through and insert blank lines where they belong - then one of two things is true:

  1. It is relatively straightforward to decide where the blank lines belong. By this I mean that the determination is relatively unsophisticated, and perhaps even trivial/mechanical.

    For example, maybe you do it whenever you see a sentence that begins at the start of a line (bolp).

  2. Such determination is not straightforward. It requires relatively deep consideration, thought, and judgment.

If (2) is the case, then I'd say that you are out of luck. If (1) is the case, however, the Emacs answer seems obvious: record what you do, and replay it. IOW, start with a keyboard macro. You might end up defining a slightly more sophisticated command, but a keyboard macro is a good place to start, for something like this.

Some things that might help:

  • Know how to move backward (M-a) and forward (M-e) across a sentence, to end up at its beginning or its end.

  • Know how to test for point being at a line beginning (bolp) or end (eolp), and for beginning (bobp) and end (eobp) of the buffer (possibly narrowed).

Maybe give it a try and ask again, saying what you've tried, if you need more help.

[You also say "Is there a way of word wrapping such text, by "select all" on a long file, without inserting blank lines?". I am assuming that by the last phrase you really mean without having to insert blank lines manually (individually). If you meant that you do not want blank lines inserted at all then you need to look at something like visual-line-mode, which pseudo-wraps.]


Anyway, in case it helps, here is one quick possibility. But it's really worth trying to experiment on your own, either to improve this or to see what you can do in a different way.

(defun split-to-paras (beg end)
  "Split the buffer (or active region) into paragraphs.
Split whenever a sentence starts at the beginning of a line."
  (interactive "r")
  (unless (use-region-p) (setq beg (point-min) end (point-max)))
  (save-excursion
    (goto-char (point-max))
    (while (> (point) beg)
      (backward-sentence)
      (when (and (bolp)  (not (looking-back "\n\n"))  (not (bobp)))
        (insert-char ?\n)
        (backward-char)))))

Then M-x split-to-paras.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.