From indent.el:

(defun indent-relative (&optional unindented-ok)
  "Space out to under next indent point in previous nonblank line.
An indent point is a non-whitespace character following whitespace.
The following line shows the indentation points in this line.
    ^         ^    ^     ^   ^           ^      ^  ^    ^
If the previous nonblank line has no indent points beyond the
column point starts at, `tab-to-tab-stop' is done instead, unless
this command is invoked with a numeric argument, in which case it
does nothing.

See also `indent-relative-maybe'."
  (interactive "P")
  (if (and abbrev-mode
       (eq (char-syntax (preceding-char)) ?w))
  (let ((start-column (current-column))
      (if (re-search-backward "^[^\n]" nil t)
      (let ((end (save-excursion (forward-line 1) (point))))
        (move-to-column start-column)
        ;; Is start-column inside a tab on this line?
        (if (> (current-column) start-column)
        (backward-char 1))
        (or (looking-at "[ \t]")
        (skip-chars-forward "^ \t" end))
        (skip-chars-forward " \t" end)
        (or (= (point) end) (setq indent (current-column))))))
    (if indent
    (let ((opoint (point-marker)))
      (delete-region (point) (progn (skip-chars-backward " \t") (point)))
      (indent-to indent 0)
      (if (> opoint (point))
          (goto-char opoint))
      (move-marker opoint nil))

How would I modify this function so that it aligns the current line with the indentation point in the next, as opposed to the previous, line?


One way is to swap the current line with the next one, use indent-relative and then swap back:

(defun indent-relative-below ()
    (transpose-lines 1)
    (transpose-lines 1)
  • This doesn't seem to work. Though that may have something to do with my use of the "evil" plugin. – LMZ Mar 28 '15 at 22:55
  • You could start Emacs with emacs -Q to see if it's something in your config. – erikstokes Mar 29 '15 at 0:46

Based on erikstokes suggestion, I've come up with a working solution.

edit: I also used code from


(defun move-line-down ()
  (let ((col (current-column)))
      (transpose-lines 1))
    (move-to-column col)))

(defun move-line-up ()
  (let ((col (current-column)))
      (transpose-lines -1))
    (move-to-column col)))

;; Indent relative, but works for the line below rather than above 
(defun indent-relative-below ()
(global-set-key  (kbd "C-;") 'indent-relative-below)

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.