From ab56d6f449949ac6612abbf0e76403e99f44c9ee Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Tue, 15 Nov 2016 08:58:45 +0100 Subject: [PATCH] Add two new functions to move to beginning/end of a line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit They consider visual lines and indentation. However, they don’t work in Org-mode yet. Inspired by @bbatsov: http://emacsredux.com/blog/2013/05/22/smarter-navigation-to-the-beginning-of-a-line/ --- init.el | 2 ++ lisp/buf-manipulation.el | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/init.el b/init.el index b5558a5..d5ae372 100644 --- a/init.el +++ b/init.el @@ -945,6 +945,8 @@ ("M-t s" . transpose-sentences) ("M-t p" . transpose-paragraphs) ("M-t W" . transpose-windows) + ("C-a" . gpolonkai/move-to-beginning-of-line) + ("C-e" . gpolonkai/move-to-end-of-line) :map ctl-x-map ("C-y" . duplicate-line) ("_" . maximize-window) diff --git a/lisp/buf-manipulation.el b/lisp/buf-manipulation.el index 1333e87..bc66458 100644 --- a/lisp/buf-manipulation.el +++ b/lisp/buf-manipulation.el @@ -127,3 +127,34 @@ copied to the kill-ring." (let ((beginning (progn (beginning-of-line) (point))) (end (progn (end-of-line) (point)))) (copy-region-as-kill beginning end)))) + +(defun gpolonkai/move-to-beginning-of-line () + "Move to different beginnings of the line; in order: beginning +of the visual line if `visual-line-mode' is active, the first +non-whitespace (indentation), the actual beginning of the line. +This function will jump between the first character and the +indentation if used multiple times. + +Inspired by Bozhidar Batsov's solution: +http://emacsredux.com/blog/2013/05/22/smarter-navigation-to-the-beginning-of-a-line/" + (interactive) + (let ((last-pos (point))) + (when visual-line-mode + (beginning-of-visual-line)) + (when (= (point) last-pos) + (back-to-indentation)) + (when (= (point) last-pos) + (beginning-of-line)) + (when (= (point) last-pos) + (back-to-indentation)))) + +(defun gpolonkai/move-to-end-of-line () + "Move to the end of the line. If `visual-line-mode' is active, + jump to the end of the visual line first. The jump to the + actual end of the line." + (interactive) + (let ((last-pos (point))) + (when visual-line-mode + (end-of-visual-line)) + (when (= (point) last-pos) + (end-of-line))))