Topics in This Post

  1. Autocompletion

  2. Emmet

  3. Yasnippet

Preamble

Emacs is old school. You're either new to it - which means you wouldn't be here unless it was your type of thing - or you know what you're looking for. In either case, read on to get some emacs code, and check the tags or search to get more.

Include Emmet and Yasnippet in your Autocompletion set-up.

Emmet expands characters typed into expressions. Yasnippet inserts pre-selected sections of code.

Emmet



(use-package emmet-mode

:after(web-mode css-mode scss-mode)

:commands (emmet-mode emmet-expand-line yas/insert-snippet yas-insert-snippet company-complete)

:config
(setq emmet-move-cursor-between-quotes t)
(add-hook 'emmet-mode-hook (lambda () (setq emmet-indent-after-insert nil)))
(add-hook 'sgml-mode-hook 'emmet-mode) ;; Auto-start on any markup modes
(add-hook 'css-mode-hook  'emmet-mode) ;; enable Emmet's css abbreviation.
;(setq emmet-indentation 2)
(unbind-key "C-M-<left>" emmet-mode-keymap)
(unbind-key "C-M-<right>" emmet-mode-keymap)

:bind
("C-j" . emmet-expand-line)
((:map emmet-mode-keymap
         ("C-c [" . emmet-prev-edit-point)
         ("C-c ]" . emmet-next-edit-point)))

);end emmet mode

Notes:

The ':after' in the use package declaration gets emacs to run this code only after specified modes are called. The commands does the same only with commands called. The hooks autostart emmet on modes. You can unbinded key configurations that conflicted with your own.

You can use Emmet to complete html tags. To run it, put the cursor after some characters typed and press C-j.

Yasnippet

Snippets are pieces of code you've collected for easy insertion.

You need shortcuts to your snippet files so you can easily add to them, as well as a convention for backing them up.

Here's some configuration code:


(use-package yasnippet
:diminish yas-minor-mode
:commands (yas-reload-all yas/minor-mode yas/global-mode yas/insert-snippet yas-insert-snippet yas-activate-extra-mode hydra-helm/body yas-new-snippet yas-load-directory yas-visit-snippet-file yas-tryout-snippet yas-describe-tables Yasnippet/body)

:init
(add-to-list 'load-path yas-p)
(require 'yasnippet)
(yas-global-mode 1)
:after hydra

:config
(add-to-list 'auto-mode-alist '("yasnippet/snippets" . snippet-mode))
(add-to-list 'auto-mode-alist '("\\.yasnippet\\'" . snippet-mode))
(setq yas-verbosity 1)
(setq yas-use-menu 'full)

;; Wrap around region
(setq yas-wrap-around-region t)
(setq yas-indent-line (quote none))

;packages

(use-package yasnippet-snippets
:defer t
);end use-package snippets

;load those snippets

(yas-reload-all)
;integration yasnippet and company

(setq yas-snippet-dirs '(
yas-snippets-p; main snippet director (when yasnippets snippets updated paste here with overwrite)
auto-yas-dir-p;autoyas snippet directories
;"/path/to/some/collection/"
;"/path/to/yasnippet/yasmate/snippets" ;; the yasmate collection
));yasnippets directories

(global-set-key (kbd "C-c C-s") 'yas/insert-snippet)

;hydra-title

(defvar yasnippet-title (with-faicon "scissors" "SNIPPETS"))
:pretty-hydra
(Yasnippet (:color red :quit-key "q" :title yasnippet-title)
(
"Inserts"
(
          ("i" yas-insert-snippet "Insert a Snippet":color blue)
          ("a" yas-reload-all "Reload Snippets" :color blue)
          ("e" yas-activate-extra-mode "Extra Mode")
          ("d" yas-load-directory "Load a Snippet Directory")
          ("m" yas/minor-mode "Activate Yas Minor":toggle t)
          ("g" yas/global-mode "Always Use Yas":toggle t)
);end inserts

"Manage Snippets"
(
          ("l" yas-describe-tables "List Snippets" :color blue)
          ("n" yas-new-snippet "Create A New Snippet")
          ("t" yas-tryout-snippet "Tryout A Snippet")
          ("s" yas-load-snippet-buffer-and-close "Load and Save New Snippet" :color blue)

          ("f" yas-visit-snippet-file "Edit Snippet File" :color blue)
);manage snippets

"Auto Snippets"
(
          ("w" aya-create "Create an Auto Snippet")
          ("y" aya-expand "Expand an Auto Snippet" :color blue)
          ("o" aya-open-line "Open Line")
          ("h" hydra-helm/body "Return To Helm" :color blue )
          ("<SPC>" nil "Exit Hydra" :color blue )
);end tabbable snippets
);end heads
);end pretty hydra

:bind
("<C-m> y" . Yasnippet/body)
);end use-package yasnippet

;ensure no new lines in snippet mode

(defun do-not-require-new-line () (set (make-local-variable 'require-final-newline) nil))

(add-hook 'snippet-mode 'do-not-require-new-line)

Notes:

You might by wondering why the use-package declarations are in the :config section ... Yes, you can do that, though you probably want to keep them seperate, which will be organizationally more comprehensible. The same applies to the hydra code.

You can google the various pieces of code above or try them out. The main gist is that you need to load the libraries then set some directories for your code. I hardwired my directories earlier in my init so they are stored in variables 'auto-yas-dir-p,' and 'yas-snippets-p.'

It's also nice to get some premade snippets, which you get above with package yasnippet-snippets.

emacs poseidon configuration yasnippet hydra code


Autoyas

Autoyas is a macro system for one-off jobs. For example, if you need to repeat a construction multiple times then tab through it and enter information, you would use autoyas.

Here's some code:


(use-package auto-yasnippet
:commands (aya-create aya-expand aya-yank-snippet aya-expand-and-exit)
:init
(add-to-list 'load-path auto-yas-p)
:config
(setq aya-persist-snippets-dir auto-yas-dir-p)
;; No need to be so verbose
);end auto-yasnippet