Daniel Wu
Daniel Wu

Reputation: 6003

how to move the cursor to the middle of the line in evil of emacs?

The following link gives an answer in vim,

https://superuser.com/questions/216411/go-to-middle-of-line-in-vim

using the following script

map gm :call cursor(0, virtcol('$')/2)<CR>

but how to do that in emacs? put it inside .emacs? and how ?

Upvotes: 2

Views: 608

Answers (3)

Aborn Jiang
Aborn Jiang

Reputation: 1059

I use this version:

(defun aborn/move-middle-of-line (arg)
  "Smart move point to the middle of current displayed line." 
  (interactive "P")
  (if (or (bolp) (eolp))
      (goto-char (/ (+ (point-at-bol) (point-at-eol)) 2))
    (progn
      (goto-char (/ (+ (point)
                       (if arg (point-at-eol) (point-at-bol)))
                    2)))))

Upvotes: 0

Dan
Dan

Reputation: 5369

This function will put the cursor on the middle point of the line. The following line binds it to gm (which clobbers the default evil-middle-of-visual-line, by the way):

(defun middle-of-line ()
  "Put cursor at the middle point of the line."
  (interactive)
  (goto-char (/ (+ (point-at-bol) (point-at-eol)) 2)))

(define-key evil-motion-state-map "gm" 'middle-of-line)

Put those lines in your .emacs file; the define-key line should come after the part that initializes evil. (FYI: the normal state map inherits from the motion state map.)

Upvotes: 4

Orpheus
Orpheus

Reputation: 298

How about something like this?

(defun middle-of-line ()
  (interactive)
  (let ((eol-pos (progn
                    (end-of-line)
                    (point)))
       (bol-pos (progn
                    (beginning-of-line)
                    (point))))
     (forward-char (/ (- eol-pos bol-pos) 2))))  

Upvotes: 2

Related Questions