For a developer, i think the following things are very important and will aid immensely in development.
- Autocompletion
- Syntax Check & Linting
- Code Folding
- Matching Parentheses
- Yasnippet
In this part we will cover how to configure spacemacs to get all the above features. This will be a pre requisite for all the other part where we will look at how to configure spacemacs for devops, web development, etc.
FYI. I am using spacemacs develop branch.
Autocompletion
Enable Autocomplete
Spacemacs comes with autocomplete for most languages. For autocompletion for each language to work, we need the auto-completion
layer to be enabled.
For this add auto-completion
layer to the dotspacemacs-configuration-layers
as shown below.
dotspacemacs-configuration-layers
'(
... (auto-completion :variables
auto-completion-enable-sort-by-usage t
auto-completion-enable-snippets-in-popup t))
This should enable autocomplete. We have also
- sort the auto complete list based on usage
- Show yasnippets in autocomplete
I find these two being useful. The shortcuts to use are
Syntax Check & Linting
Spacemacs support automatic syntax checking for most languages using flycheck
. To enable syntax checking add the syntax-checking
layer as shown below.
dotspacemacs-configuration-layers
'(
... syntax-checking)
You can use SPC e l
to show the list of all the errors or SPC e L
to show list of all errors and focus the errors buffer.
Code Folding
By default, spacemacs supports code folding. Nothing to configure. hurray.
Shortcuts for code folding are z a
to toggle folding at current position, z m
to hide all blocks, z r
to show all blocks. Mostly these all shortcuts are what i need to browse code effectively.
Matching Parentheses
By default, spacemacs has enabled matching parentheses. This means you do not need to type closing braces, quotes, etc. But emacs can do much more than what you regular editor can provide.
Add the below changes to your .spacemacs file
dotspacemacs-additional-packages '(evil-smartparens)
...(defun dotspacemacs/user-config ()
"Configuration for user code:
This function is called at the very end of Spacemacs startup, after layer
configuration.
Put your configuration code here, except for variables that should be set
before packages are loaded." ...
(module/misc/smartparens)
)
Add this after dotspacemacs/user-config
;;;; Smart Parentheses
(defun module/misc/smartparens ()
(use-package smartparens
:defer t
:diminish ""
:bind (("C-)" . sp-forward-slurp-sexp)
("C-}" . sp-forward-barf-sexp)
("C-(" . sp-splice-sexp))
:config
(progn
(add-hook 'smartparens-enabled-hook #'evil-smartparens-mode)
(push 'yas-installed-snippets-dir yas-snippet-dirs))
))
With the above configurations you get
- use
C-)
andC-}
for forward slurp and forward barf.
2. If you delete line dd
or delete word dw
, still the braces, quotes are maintained. Neat isn’t it
Yasnippet
Yasnippet is amazing, it will help you write code blazingly fast. To load pre written snippets add the following after dotspacemacs/user-conf
;;;; Yasnippet
(defun module/misc/yasnippet ()
"Yassnippet bindings and config."
(use-package yasnippet-snippet
:defer t
:config
(push 'yas-installed-snippets-dir yas-snippet-dirs)
)
)
and add
(defun dotspacemacs/user-config ()
"Configuration for user code:
This function is called at the very end of Spacemacs startup, after layer
configuration.
Put your configuration code here, except for variables that should be set
before packages are loaded."...
(module/misc/yasnippet)
)
This will load the all the snippets in https://github.com/AndreaCrotti/yasnippet-snippets
I hope this will give you wings for fast development in emacs.