简体   繁体   中英

How to customize Emacs key bindings for going to specific line / end of buffer

I want to customize Emacs so that pressing

ESC : n RET

takes me to line number n

and

ESC : $ RET

takes me to the last line. (That's how the vi editor works.)

How can I achieve this inside my Emacs configuration file? Currently I have this in my .emacs :

(global-set-key (kbd "M-9") 'prev-window)
(global-set-key (kbd "M-0") 'other-window)

I don't want to use any of the off-the-shelf solutions (eg. evil) because they are bloated and mess with my existing shortcuts.

Put this in a file and load it (load means execute):

(defun vi-goto-line (arg)
 (interactive "sLine:")
 (message arg)
 (if (string= "$" arg)
  (end-of-buffer)
  (goto-line (string-to-int arg))
 )
)

(global-set-key (kbd "M-:") 'vi-goto-line)

To load it you can use Mx load-file and then enter interactively the path to the file.

Keep in mind that the key combo M-: (which is the same as ESC : ) already has a meaning in Emacs, so this now gets cloaked.

Of course you also can load the file from your .emacs by putting (load-file "/path/to/my/file") into the .emacs or put these lines directly into your .emacs file (or any other configuration file which gets loaded)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM