29

Fold your vim config - Marble Zone

 4 years ago
source link: https://sevensidedmarble.com/folds-for-better-vim-configs/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Fold your vim config

Written by: sevensidedmarble - 2. April 2020

You may or may not know that I am pretty into Emacs and Org mode. Org mode, if you haven't seen it, it's an outliner built into Emacs:

Org mode is great for writing structured content like scripts.

Org mode is great for writing structured content like scripts.

Some people even use Org mode as a literate programmingConceived by the famous Donald Knuth, which is interesting. tool for their Emacs config. I've always thought this was pretty cool, and thought literate programming in general was pretty cool, but never found a great way to apply it to my vim config until now.

The inspiration

Before continuing I'd like to lead with where I found this idea online:

I'm not sure which of these I saw first, but the basic concept is using manually placed fold markers in your vim config to break it down into sections. Kind of like this:

Before...

Please forgive my messy config. Please forgive my messy config.

After!

This is what it looks like with one of the folded sections opened. This is what it looks like with one of the folded sections opened.

Some people even take this a step farther and apply some crazy fold styling to make it look even more like Org mode, which you can see in one of the links above.

Here's what you need

fold-marker is a folding method few people I'd imagine spend much time thinking about. The markers themselves look like this:

"" Autocomplete {{{
Plug 'Shougo/neco-vim'
Plug 'neoclide/coc-neco'
Plug 'neoclide/coc-sources'
Plug 'neoclide/coc.nvim', {'branch': 'release'}
"" }}}

"" The almighty tpope {{{
Plug 'tpope/vim-sensible'
Plug 'tpope/vim-endwise'
Plug 'tpope/vim-repeat'
Plug 'tpope/vim-eunuch'
Plug 'tpope/vim-rhubarb'
Plug 'tpope/vim-rsi'
Plug 'tpope/vim-unimpaired'
" }}}

"" Spellcheck {{{
autocmd BufRead,BufNewFile *.md setlocal spell spelllang=en_us
autocmd FileType gitcommit setlocal spell spelllang=en_us
"" }}}

Which when folded look something like this:

+--  7 lines: " Autocomplete

+-- 18 lines: " The almighty tpope

+--  5 lines: " Spellcheck

I've found this is an amazing way of breaking up your vim config into sections without relying on splitting it into different files (which I was doing before, and wasn't ever completely satisfied with).

But wait, there's more

There's only one problem though, you probably don't want to set foldmethod to fold-marker for everything. I personally think syntax or indent is the most useful general-purpose foldmethod, so that is what I set mine to in my vim config. So how you change foldmethod only for .vim files?

Well, there is of course a few ways to do it, but the best is to use ftplugin.

In your vim config directories after/ftplugin directory (I use neovim, so for me this is at ~/.config/nvim/after/ftplugin/vim/vim.vim) add the following file to the following directory:

"" ~/.config/nvim/after/ftplugin/vim/vim.vim
set foldmethod=marker

Breaking this down

  1. The after directory is where ftplugin files can run after vim default configurations have loaded.
  2. The ftplugin directory then contains a bunch of subdirectories for each file type.
  3. Then the vim directory contains files that will run only when editing .vim files, after other config files have fully loaded.
  4. Then the file itself is just setting foldmethod to marker. Settings put here are filetype specific, so they won't bleed into other filetypes.

Well, that's about it. I hope you find this somewhat as cool as I do!


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK