1

There are many project description files using a quite neat structure like this:

Introduction
============

......bla..bla.....


Details
=======

......bla..bla.....

My question is: Is there any convenient ways to generate automatically the title using such structure as:

Title
=====

, where the number of Equal Sign(=) is well aligned to the that of characters of the title just preceding it.

1
  • Write your file in Org mode and export as text?
    – NickD
    May 22 at 11:03

2 Answers 2

0

The documentation for yasnippet gives almost exactly this workflow as an example of its "field mirroring" capabilities. That's for entering titles; it might be possible to adapt it to edit existing well-structured text too.

For example, with the following snippet, press C-c u and start typing. When you're done, press TAB.

# -*- mode: snippet -*-
# name: underline
# binding : C-c u
# --
${1:}
${1:$(make-string (string-width yas-text) ?\=)}

$0
1
  • Thank you @jagrg. It seems unfair that I can't upvote you for such a substantive assist. May 22 at 16:07
0

You can use the following command to underline the line at point.

(defun underline ()
  (interactive)
  (let* ((line (string-trim (thing-at-point 'line t)))
         (underline (make-string (string-width line) ?\=))
         (padding (save-excursion
                    (forward-line 0)
                    (when (re-search-forward "[^ ]" (point-at-eol) t)
                      (make-string (1- (current-column)) ?\s)))))
    (unless (string-empty-p underline)    
      (insert (format "\n%s%s\n\n" padding underline)))))
1
  • Thanks! The solution just works perfectly as well! Would definitely upvote for it if I have enough SE Reputation.
    – absuu
    May 27 at 8:06

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.