Search code examples
vimemacsfile-type

Vim, setting .el folding has killed syntax highlighting


When I open my init.el in Vim, I want folding on comments. However, the code I made for this has broken syntax highlighting. This is not the desired result.

Here is what I used, in my .vimrc:

augroup filetype_el
    autocmd!
    autocmd FileType el setlocal foldmethod=marker foldmarker=;;;,;;+
augroup END
au BufRead,BufNewFile *.el      set filetype=el

This breaks syntax highlighting for me.


Solution:

With Ben's explanation, I realize I should have used lisp as filetype, and not el. Here is the working code:

augroup filetype_lisp
    autocmd!
    autocmd FileType lisp setlocal foldmethod=marker foldmarker=;;;,;;+
augroup END

Now I have folding and syntax highlighting.


Sample file:

In a .el file, I want the following to have folding and syntax highlighting when opened from Vim.

;;; config folding
;;;; outline & outshine

;; when a .el file is opened, use outline-minor-mode
(add-hook 'emacs-lisp-mode-hook 'outline-minor-mode)

;; gives my init.el nice folding and keybinding defaults
(when (locate-library "outshine")
  (autoload 'outshine-hook-function "outshine")
  (add-hook 'emacs-lisp-mode-hook 'outshine-hook-function))
(setq outshine-startup-folded-p t)

;;+
;;;; vimrc mode, hideshow

;; because we sometimes view our .vim and .vimrc files from emacs
(when (locate-library "vimrc-mode")
  (add-to-list 'auto-mode-alist '(".vim\\(rc\\)?$" . vimrc-mode))
  (add-to-list 'hs-special-modes-alist '(vimrc-mode "{{{" "}}}" nil nil))
  (add-hook 'vimrc-mode-hook '(lambda ()
                                (hs-minor-mode)
                                (hs-hide-all)))
  (autoload 'hs-minor-mode "hideshow" nil t)
  (eval-after-load 'hideshow
    '(define-key hs-minor-mode-map (kbd "TAB") 'hs-toggle-hiding)))

;;+
;;+

(I will consider ways to get rid of the ;;+s, someday.)

Edit: Someday has come, Vim, foldexpr, like folding in outshine.el effect on init.el - Stack Overflow. I can now omit the ;;+ from my init.el.


Solution

  • When I edit a test.el file, Vim sets the filetype to "lisp" automatically.

    You have overwritten this setting, setting filetype to "el" instead.

    Vim does not define any syntax highlighting or indeed any plugin rules for the "el" filetype.

    You will either need to create an "el" filetype, trigger off the "lisp" filetype instead, or eschew filetypes entirely and just trigger off file names.

    Alternatively, you could TRY using a dotted filetype (e.g. "lisp.el") but I'm not sure how well that will work. Supposedly Vim will try loading both filetypes together but it's not a very common use.