0

Im looking for a way (function) to add/split a new line each time a dot (.) is present?

for example turn this

When a user wants to communicate with the computer, the user interacts with an application. The application interacts with the operating system, and the operating system makes hardware components to work according to the user given instructions. The hardware components sends the result back to the operating system, then the operating system forwards the same to the application and the application shows the result to the user.

to this

When a user wants to communicate with the computer, the user interacts with an application. 

The application interacts with the operating system, and the operating system makes hardware components to work according to the user given instructions. 

The hardware components sends the result back to the operating system, then the operating system forwards the same to the application and the application shows the result to the user.

Any ideas would be greatly appreciated!

  • 1
    What should happen if there are linebreaks already? Do you want them gone, or should they stay? – wvxvw Oct 19 '17 at 7:41
2

With the point at the beginning of the text:

M-%.RET.C-q C-j RET

or

M-xquery-replaceRET.RET.C-q C-j RET

The first . makes query-replace look for a period. The second . followed by C-q C-j inserts a period followed by a newline character. You can use multiple newline characters to get the result from your example.


If you want to ignore periods that are already followed by a newline, you can use query-replace-regex and use \.[^C-q C-j] as a search regex, which looks for a . that is not follwed by a newline.

Update: If you want to do this often, you can wrap it in a function:

(defun my/replace-dot-with-newline ()
  (interactive)
  (replace-regexp "\\.[^\n]" ".\n"))

This searches for a . that is not followed by a newline, and inserts a newline after it. You can bind this to a key or call it using M-x my/replace-dot-with-newline.

  • 1
    thx for the answer. i get this error in the C-q C-j part: C-q C-j is undefined. any clue? – zeltak Oct 21 '17 at 10:10
  • That's strange, that is the standard way to insert a newline into a replace-command (see gnu.org/software/emacs/manual/html_node/efaq/…). Note that that should be read as key combinations; control+q followed by control+j. – Arnot Oct 21 '17 at 15:19
  • thx. is there no regex way (non ketbind) to specify the C-q-C-j? – zeltak Oct 26 '17 at 14:55
  • I updated the answer with a function definition which does not require C-q C-j - does that help? – Arnot Oct 26 '17 at 15:12
  • thx! that seems to work! – zeltak Oct 27 '17 at 14:48

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.