3

Is there an Emacs command that will "fill" a row with a particular character up to a specified column? Basically the equivalent of this question, except with Emacs instead of Vim.

As an example, say I start entering lines that look like this:

/* -- Includes
/* -- Procedure Prototypes
/* -- Procedures 

I'd like a command that automatically fills in the rest of the line (up to a column I can specify) with dashes, regardless of what column the cursor is currently on.

/* -- Includes -----------------------------------------------------
/* -- Procedure Prototypes -----------------------------------------
/* -- Procedures ---------------------------------------------------

Thank you. Sorry if this has been asked already, I couldn't find anything with Google.

Community
  • 1
  • 1
Chris Vig
  • 8,552
  • 2
  • 27
  • 35

2 Answers2

2

Here's something which should work:

(defun fill-to-end ()
  (interactive)
  (save-excursion
    (end-of-line)
    (while (< (current-column) 80)
      (insert-char ?-))))

It appends - characters to the end of the current line until it reaches column 80. If you want to specify the character, it should be changed to

(defun fill-to-end (char)
  (interactive "cFill Character:")
  (save-excursion
    (end-of-line)
    (while (< (current-column) 80)
      (insert-char char))))
resueman
  • 10,572
  • 10
  • 31
  • 45
1
(defun char-fill-to-col (char column &optional start end)
  "Fill region with CHAR, up to COLUMN."
  (interactive "cFill with char: \nnto column: \nr")
  (let ((endm  (copy-marker end)))
    (save-excursion
      (goto-char start)
      (while (and (not (eobp))  (< (point) endm))
        (end-of-line)
        (when (< (current-column) column)
          (insert (make-string (- column (current-column)) char)))
        (forward-line 1)))))

(defun dash-fill-to-col (column &optional start end)
  "Fill region with dashes, up to COLUMN."
  (interactive "nFill with dashes up to column: \nr")
  (char-fill-to-col ?- column start end))
Drew
  • 29,895
  • 7
  • 74
  • 104