Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Last active May 22, 2021 07:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirrajan/6371290a721220c6b553f18b8c77b715 to your computer and use it in GitHub Desktop.
Save amirrajan/6371290a721220c6b553f18b8c77b715 to your computer and use it in GitHub Desktop.
Emacs Evil Hello World

Minimal steps to get Emacs with VIM emulation working.

  1. Install Emacs.
  2. Create the file ~/.emacs.d/init.el.
  3. Paste the following in ~/.emacs.d/init.el.
;; This buffer is for text that is not saved, and for Lisp evaluation.
;; To create a file, visit it with C-x C-f and enter text in its buffer.
;; This is a import that ships with emacs.
;; package is Emacs's package manager.
(require 'package)
(setq package-check-signature nil)

;; Emacs has an official repository of packages and a more current
;; unofficial one. Melpa is the 'unoffical one' (which in this
;; case translates to more up to date, newer, and by extension,
;; a bit more volatile). The official repo is called org. This
;; repo is slow changing and extremely stable (but doesn't have
;; all the cool/cutting edge packages that are being used).
(push '("melpa" . "http://melpa.org/packages/") package-archives)
(push '("org" . "http://orgmode.org/elpa/") package-archives)
(push '("melpa-stable" . "https://stable.melpa.org/packages/") package-archives)

;; After the repositories have been set, initialize the package
;; manager.
(package-initialize)

(defun amir-reinstall-packages-core ()
  ;; Emacs keeps a cache of the package list locally. We'll
  ;; initialize this cache once. If you ever go to install a package
  ;; and it cant be found, just rerun (package-reresh-contents).
  (unless package-archive-contents (package-refresh-contents))

  ;; Here is our big bad package list. Whenever you want to
  ;; install a new package, just add it to this list.
  (setq bootstrap-list
	'(use-package))

  (dolist (package bootstrap-list)
    (unless (package-installed-p package)
      (package-install package))))

(defun amir-reinstall-packages ()
  (interactive) (amir-reinstall-packages-core))

(amir-reinstall-packages-core)

(use-package evil
  :ensure t
  :init
  (setq evil-want-C-i-jump nil)
  :config

  (evil-mode 1)

  ;; wire up emacs so that I can spam the escape key and
  ;; exit whatever ridiculous window/buffer it has me in.
  (defun minibuffer-keyboard-quit ()
    (interactive)
    (if (and delete-selection-mode transient-mark-mode mark-active)
	(setq deactivate-mark  t)
      (when (get-buffer "*Completions*") (delete-windows-on "*Completions*"))
      (abort-recursive-edit)))

  (define-key evil-normal-state-map [escape] 'keyboard-quit)
  (define-key evil-visual-state-map [escape] 'keyboard-quit)
  (define-key minibuffer-local-map [escape] 'minibuffer-keyboard-quit)
  (define-key minibuffer-local-ns-map [escape] 'minibuffer-keyboard-quit)
  (define-key minibuffer-local-completion-map [escape] 'minibuffer-keyboard-quit)
  (define-key minibuffer-local-must-match-map [escape] 'minibuffer-keyboard-quit)
  (define-key minibuffer-local-isearch-map [escape] 'minibuffer-keyboard-quit)

  (global-set-key [escape] 'evil-exit-emacs-state)

  ;; In Tmux and iTerm, change the normal/edit mode cursors.
  (defun evil-send-string-to-terminal (string)
    (unless (display-graphic-p) (send-string-to-terminal string)))

  (defun evil-terminal-cursor-change ()
    (when (string= (getenv "TERM_PROGRAM") "iTerm.app")
      (add-hook 'evil-insert-state-entry-hook (lambda () (evil-send-string-to-terminal "\e]50;CursorShape=1\x7")))
      (add-hook 'evil-insert-state-exit-hook  (lambda () (evil-send-string-to-terminal "\e]50;CursorShape=0\x7"))))
    (when (and (getenv "TMUX")  (string= (getenv "TERM_PROGRAM") "iTerm.app"))
      (add-hook 'evil-insert-state-entry-hook (lambda () (evil-send-string-to-terminal "\ePtmux;\e\e]50;CursorShape=1\x7\e\\")))
      (add-hook 'evil-insert-state-exit-hook  (lambda () (evil-send-string-to-terminal "\ePtmux;\e\e]50;CursorShape=0\x7\e\\")))))

  (evil-terminal-cursor-change))
  1. Start up Emacs, it will probably give you an error.
  2. Press Alt+x, type package-referesh-contents and press enter.
  3. Exit Emacs by pressing Ctrl+x, Ctrl+c.
  4. Re-open Emacs and you should have Vim mode enabled.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment