I am in the process of formatting a couple of hundred markdown files and am trying to automate that process. I am having two problems, but I guess I can ask two questions ;)

Goal

I am trying to achieve the following:

  • Every main title starting with # should have two newlines above and one newline below.
  • Every subtitle starting with two or more # should have one newline above and one newline below.

Example:

More text here
# The main title 
## Another title 


## The third title 
   More text 

# The second main title 

more text here 


## Second subsection 


# Third title 

more text 

Should be formatted to

More text here


# The main title 

## Another title 

## The third title 

   More text 


# The second main title 

more text here 

## Second subsection 


# Third title 

more text 

Attempt

I am not sure if this is best suited for regex or a well defined macro?

My attempt at regex has so far been

[#]+.*\n[^\n]

This is at least able to identify every title that is missing a newline underneath.

enter image description here

However, I have no idea how I would use the expression above to insert a newline after the title. Any help would be appreciated.

  • I'd use Perl :-) – choroba Jul 12 at 21:35
  • A little bit of context for this could be appreciated: can it be elisp code that does this operation, regardless of regex use? can the regex have perl extensions (man perlre)? – Felipe Lema Jul 12 at 21:49
  • I am quite new to emacs. Would such a elisp script be something I would add in my user-config and then trigger it with some hotkey? The end goal is to write tests to detect these cases, and trigger at least a warning when running travis in GitHub. So regex for detection would be preferred. – Øistein Søvik Jul 12 at 22:05
  • If you can do the editing in a systematic way manually then you can simply record your actions as a keyboard macro. Then you can replay that as many times as you want. If that's straightforward enough (and I'm guessing it is) then you don't need to know Elisp or regexps. See the Emacs manual, node [Keyboard Macros]()gnu.org/software/emacs/manual/html_node/emacs/…. – Drew Jul 13 at 2:26

This is a working answer, however it is very rudementary. Hopefully it can be of help to someone else

Removes all the newlines except ONE BEFORE ALL titles

:%s/[\n\r]*\(#.*\)/\n\n\1

Removes all the newlines ONLY on the main title, inserts two above

:%s/[\r\n]\(#[^#].*\)/\n\n\1

Removes all EXCESS newlines after titles

:%s/\(^#.*\)\n\n[\n]+/\1\n\n

Finds Titles # ## ### without newlines and inserts ONE newline beneath them

:%s/\(^#.*\)\n\([^\n].*\)/\1\n\n\2
  • Please, note that this is meant to be used with evil and not out-of-the-box Emacs – Felipe Lema Jul 13 at 12:56

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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