如何右对齐区域和/或线?


10

我们可以使用M-x center-region和将文本居中M-o M-s。右对齐是否有类似的东西?

之前的示例:

Item 0: value                                                       |
Item 100: value                                                     |
Item 30: value                                                      |

后:

                                                       Item 0: value|
                                                     Item 100: value|
                                                      Item 30: value|
                                                       fill-column  ^

文本右对齐的最简单方法是什么?

Answers:


13

根据Filling上的手动节点,一些fill函数采用一个可选的JUSTIFY参数,您可以使用该参数。因此,例如,要使用正确的理由填充段落,可以使用(fill-paragraph 'right)

您也可以使用(justify-current-line 'right)单行。

如果计划大量使用这些选项,则可以将它们包装在如下函数中,然后将这些函数绑定到您选择的键上:

(defun right-justify-current-line ()
  "Right-justify this line."
  (interactive)
  (justify-current-line 'right))

(defun right-fill-paragraph ()
  "Fill paragraph with right justification."
  (interactive)
  (fill-paragraph 'right))

您可以使用以下函数代替fill-paragraph。使用各种前缀,它可以让您决定在要填充的段落上使用哪种对齐方式:

(defun fill-paragraph-dwim (&optional arg)
  "Fills the paragraph as normal with no prefix. With C-u,
right-justify.  With C-u C-u, center-justify.  With C-u C-u C-u,
full-justify."
  (interactive "p")
  (fill-paragraph (cond ((= arg 4)  'right)
                        ((= arg 16) 'center)
                        ((= arg 64) 'full))))

如果您不希望在右对齐时进行填充,则可以使用以下函数,该函数直接通过center-region单行更改从函数中复制,以使其右对齐:

(defun right-region (from to)
  "Right-justify each nonblank line starting in the region."
  (interactive "r")
  (if (> from to)
      (let ((tem to))
    (setq to from from tem)))
  (save-excursion
    (save-restriction
      (narrow-to-region from to)
      (goto-char from)
      (while (not (eobp))
    (or (save-excursion (skip-chars-forward " \t") (eolp))
        ;; (center-line))              ; this was the original code
        (justify-current-line 'right)) ; this is the new code
    (forward-line 1)))))
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.