Skip to main content
Name, document and simplify advice; minor rewording and clarification
Source Link

The command end-of-buffer uses a variation on (goto-char (point-max)) to go to the last position in the buffer. If the buffer ends with a newline that, end-of-buffer will leave you on the empty line at the end --, whereas if you delete thatthis last newline then, end-of-buffer leaveswill leave you at the end of the text in the now non-empty last line.

(advice-add 'end-of-buffer :after
            (lambda (&rest args) (when (looking-at-p "^$") (previous-line))))
(defun my-end-of-buffer-dwim (&rest _)
  "If current line is empty, call `previous-line'."
  (when (looking-at-p "^$")
    (previous-line)))

(advice-add #'end-of-buffer :after #'my-end-of-buffer-dwim)

If you want end-of-buffer to always leave you at the beginningbeginning of the lastpenultimate line of text (rather than at the end if the file has no trailing newline), you might want this instead:

(defun end-of-buffer-dwim (&rest args)
  (when (looking-at-p "^$")
    (previous-line))
  (beginning-of-line))

(advice-add 'end-of-buffer :after 'end-of-buffer-dwim)
(defun my-end-of-buffer-dwim (&rest _)
  "Go to beginning of line.
If current line is empty, go to beginning of previous one
instead."
  (beginning-of-line (and (looking-at-p "^$") 0)))

(advice-add #'end-of-buffer :after #'my-end-of-buffer-dwim)
(defun my-end-of-buffer ()
  (interactive)
  (goto-char (point-max))
  (when (looking-at-p "^$")
    (previous-line))
  (beginning-of-line))

(global-set-key [remap end-of-buffer] 'my-end-of-buffer)
(defun my-end-of-buffer ()
  "Go to beginning of last line in buffer.
If last line is empty, go to beginning of penultimate one
instead."
  (interactive)
  (goto-char (point-max))
  (beginning-of-line (and (looking-at-p "^$") 0)))

(global-set-key [remap end-of-buffer] #'my-end-of-buffer)

Note that all of the above solutions leave you at most one line away from the end of the buffer - they do not leave you on the last non-empty line.

The command end-of-buffer uses (point-max) to go to the last position in the buffer. If the buffer ends with a newline that will leave you on the empty line at the end -- if you delete that last newline then end-of-buffer leaves you at the end of the text in the last line.

(advice-add 'end-of-buffer :after
            (lambda (&rest args) (when (looking-at-p "^$") (previous-line))))

If you want end-of-buffer to always leave you at the beginning of the last line of text (rather than at the end if the file has no trailing newline), you might want this instead:

(defun end-of-buffer-dwim (&rest args)
  (when (looking-at-p "^$")
    (previous-line))
  (beginning-of-line))

(advice-add 'end-of-buffer :after 'end-of-buffer-dwim)
(defun my-end-of-buffer ()
  (interactive)
  (goto-char (point-max))
  (when (looking-at-p "^$")
    (previous-line))
  (beginning-of-line))

(global-set-key [remap end-of-buffer] 'my-end-of-buffer)

The command end-of-buffer uses a variation on (goto-char (point-max)) to go to the last position in the buffer. If the buffer ends with a newline, end-of-buffer will leave you on the empty line at the end, whereas if you delete this last newline, end-of-buffer will leave you at the end of the text in the now non-empty last line.

(defun my-end-of-buffer-dwim (&rest _)
  "If current line is empty, call `previous-line'."
  (when (looking-at-p "^$")
    (previous-line)))

(advice-add #'end-of-buffer :after #'my-end-of-buffer-dwim)

If you want end-of-buffer to always leave you at the beginning of the penultimate line (rather than at the end if the file has no trailing newline), you might want this instead:

(defun my-end-of-buffer-dwim (&rest _)
  "Go to beginning of line.
If current line is empty, go to beginning of previous one
instead."
  (beginning-of-line (and (looking-at-p "^$") 0)))

(advice-add #'end-of-buffer :after #'my-end-of-buffer-dwim)
(defun my-end-of-buffer ()
  "Go to beginning of last line in buffer.
If last line is empty, go to beginning of penultimate one
instead."
  (interactive)
  (goto-char (point-max))
  (beginning-of-line (and (looking-at-p "^$") 0)))

(global-set-key [remap end-of-buffer] #'my-end-of-buffer)

Note that all of the above solutions leave you at most one line away from the end of the buffer - they do not leave you on the last non-empty line.

added 411 characters in body
Source Link
glucas
  • 20.9k
  • 1
  • 56
  • 84

The command end-of-buffer uses (point-max) to go to the last position in the buffer. If the buffer ends with a newline that will leave you on the empty line at the end -- if you delete that last newline then end-of-buffer leaves you at the end of the text in the last line.

One possible solution is to advise end-of-buffer to just go up a line if it leaves you on an empty line:

(advice-add 'end-of-buffer :after
            (lambda (&rest args) (when (looking-at-p "^$") (previous-line))))

If you want end-of-buffer to always leave you at the beginning of the last line of text (rather than at the end if the file has no trailing newline), you might want this instead:

(defun end-of-buffer-dwim (&rest args)
  (when (looking-at-p "^$")
    (previous-line))
  (beginning-of-line))

(advice-add 'end-of-buffer :after 'end-of-buffer-dwim)
 

And the standard advice disclaimer: You can avoid unanticipated side-effects by just creating your own end-of-buffer command and rebinding the keys, rather than using advice:

(defun my-end-of-buffer-dwim (&rest)
 args (interactive)
  (goto-char (point-max))
  (when (looking-at-p "^$")
      (previous-line))
  (beginning-of-line))

(global-set-key [remap end-of-buffer] 'my-end-of-buffer)

The command end-of-buffer uses (point-max) to go to the last position in the buffer. If the buffer ends with a newline that will leave you on the empty line at the end -- if you delete that last newline then end-of-buffer leaves you at the end of the text in the last line.

One possible solution is to advise end-of-buffer to just go up a line if it leaves you on an empty line:

(advice-add 'end-of-buffer :after
            (lambda (&rest args) (when (looking-at-p "^$") (previous-line))))

If you want end-of-buffer to always leave you at the beginning of the last line of text (rather than at the end if the file has no trailing newline), you might want this instead:

(advice-add 'end-of-buffer :after 'end-of-buffer-dwim)
 
(defun end-of-buffer-dwim (&rest args)
  (when (looking-at-p "^$")
      (previous-line))
  (beginning-of-line))

The command end-of-buffer uses (point-max) to go to the last position in the buffer. If the buffer ends with a newline that will leave you on the empty line at the end -- if you delete that last newline then end-of-buffer leaves you at the end of the text in the last line.

One possible solution is to advise end-of-buffer to just go up a line if it leaves you on an empty line:

(advice-add 'end-of-buffer :after
            (lambda (&rest args) (when (looking-at-p "^$") (previous-line))))

If you want end-of-buffer to always leave you at the beginning of the last line of text (rather than at the end if the file has no trailing newline), you might want this instead:

(defun end-of-buffer-dwim (&rest args)
  (when (looking-at-p "^$")
    (previous-line))
  (beginning-of-line))

(advice-add 'end-of-buffer :after 'end-of-buffer-dwim)

And the standard advice disclaimer: You can avoid unanticipated side-effects by just creating your own end-of-buffer command and rebinding the keys, rather than using advice:

(defun my-end-of-buffer ()
  (interactive)
  (goto-char (point-max))
  (when (looking-at-p "^$")
    (previous-line))
  (beginning-of-line))

(global-set-key [remap end-of-buffer] 'my-end-of-buffer)
deleted 60 characters in body
Source Link
glucas
  • 20.9k
  • 1
  • 56
  • 84

The command end-of-buffer uses (point-max) to go to the last position in the buffer. If the buffer ends with a newline that will leave you on the empty line at the end -- if you delete that last newline then end-of-buffer leaves you at the end of the text in the last line.

One possible solution is to advise end-of-buffer to just go up a line if it leaves you on an empty line:

(advice-add 'end-of-buffer :after
            (lambda (&rest args) (when (looking-at-p "^$") (previous-line))))

If you want end-of-buffer to always leave you at the beginning of the last line of text (rather than at the end if the file has no trailing newline), you can add an additional check likemight want this instead:

(advice-add 'end-of-buffer :after 'end-of-buffer-dwim)

(defun end-of-buffer-dwim (&rest args)
  (ifwhen (looking-at-p "^$")
      (previous-line)
    (when (looking-at-p ".$")
      (beginning-of-line))))

The command end-of-buffer uses (point-max) to go to the last position in the buffer. If the buffer ends with a newline that will leave you on the empty line at the end -- if you delete that last newline then end-of-buffer leaves you at the end of the text in the last line.

One possible solution is to advise end-of-buffer to just go up a line if it leaves you on an empty line:

(advice-add 'end-of-buffer :after
            (lambda (&rest args) (when (looking-at-p "^$") (previous-line))))

If you want end-of-buffer to always leave you at the beginning of the last line of text (rather than at the end if the file has no trailing newline), you can add an additional check like this instead:

(advice-add 'end-of-buffer :after 'end-of-buffer-dwim)

(defun end-of-buffer-dwim (&rest args)
  (if (looking-at-p "^$")
      (previous-line)
    (when (looking-at-p ".$")
      (beginning-of-line))))

The command end-of-buffer uses (point-max) to go to the last position in the buffer. If the buffer ends with a newline that will leave you on the empty line at the end -- if you delete that last newline then end-of-buffer leaves you at the end of the text in the last line.

One possible solution is to advise end-of-buffer to just go up a line if it leaves you on an empty line:

(advice-add 'end-of-buffer :after
            (lambda (&rest args) (when (looking-at-p "^$") (previous-line))))

If you want end-of-buffer to always leave you at the beginning of the last line of text (rather than at the end if the file has no trailing newline), you might want this instead:

(advice-add 'end-of-buffer :after 'end-of-buffer-dwim)

(defun end-of-buffer-dwim (&rest args)
  (when (looking-at-p "^$")
      (previous-line))
  (beginning-of-line))
Source Link
glucas
  • 20.9k
  • 1
  • 56
  • 84
Loading