0

How do I get emacs to automatically format a large text file like section a
to section b. I have tried using notepad++ and a macro, but it just froze my computer

section a

words    wordss
wordsss    wordssss
wordssss    wordsssss
wordsssss    wordssssss

to

section b

words
wordss
wordsss
wordssss
wordsssss
wordssssss
  • How about placing your cursor at the beginning of the word "section"?: M-x replace-string RET C-q TAB RET C-q C-j RET assuming those are tabs. If they are spaces then count how many spaces and use that instead of C-q TAB. – lawlist Feb 4 '18 at 0:58
  • is there a way to distinguish tabs from spaces if I didn't create the file? – digitaldefector Feb 4 '18 at 1:07
  • 1
    M-x whitespace-mode – npostavs Feb 4 '18 at 1:13
  • it's four spaces so I would – digitaldefector Feb 4 '18 at 1:35
  • 1
    It would be perhaps better to write a more descriptive title for your question. – Name Feb 4 '18 at 7:43
1

A regular expression will work with both tabs and spaces and also with a variable number of them. Type M-x replace-regexp RET \s-+ RET Q-q C-j RET, this will replace any sequence of one or more whitespace characters with a single newline, see the emacs manual for details.

| improve this answer | |
0

As @npostavs points out in a comment above, M-x whitespace-mode is very helpful to visually distinguish between tabs and spaces.

If it were four (4) spaces, instead of tabs between the words, then:  Place the cursor at the beginning of the word "section" [or simply before the first occurrence to be replaced] and type M-x replace-string RET RET C-q C-j RET . The gap between the two RET are four (4) spaces.

If it were tabs, instead of spaces between the words, then:  Place the cursor at the beginning of the word "section" [or simply before the first occurrence to be replaced] and type M-x replace-string RET C-q TAB RET C-q C-j RET


To limit the search to a specific region, select the region and type M-x narrow-to-region. When done, type M-x widen.

| improve this answer | |
  • Using a regular expression you don't need to know if they are spaces or tabs and can also work with a variable number of spaces – matteol Feb 4 '18 at 7:51
  • @matteol -- Please write up an alternative answer. – lawlist Feb 4 '18 at 7:57
  • I posted an answer. – matteol Feb 4 '18 at 8:39

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.