Last time I too often feel the need to write both ‘prose’ and ‘code’ stuff. My favorite editor is vim and I’m using it for any file editing purpose.

Unfortunately, it can be very uncomfortable to use the same vimrc for writing prose and code. When you are writing prose some plugins (or settings) which intended to use while writing code can make your ass burn (and vice-versa). You can use autogroup to deal with it, but it’s very inflexible and makes your vimrc look ugly. You can also use filetype plugins, but as long as I can see, it’s inflexible too. However, I can be wrong with all this stuff and there is some more neat way to solve the problem instead of the next one.

The main idea is pretty simple - to split vimrc into three parts. The first part is the core, where I store the common settings. The second and third parts depend on first part and they contain appropriate settings for writing prose or code. Such splitting is very scalable and makes possible to manage the whole vimrc for such purpose instead of brainfucking with autogroup or filetype plugins.

If you already interested in, you can find the sources at the end of note.

Here is the very-generalized description of steps and tips to get a working solution:

  • Create three files: vimrc.core, vimrc.code, vimrc.prose. I just described these files above.

  • Fill them with appropriate configuration stuff, move the common settings to vimrc.core and make others source it. These things are very simple to implement and you can get in trouble just with per-mode plugins. It can be solved easily if you are using dein.

  • Setting up shell aliases sometimes can be handy. I have done it like that:

    alias vp="vim -u $XDG_CONFIG_HOME/vim/vimrc.prose"
    alias vc="vim -u $XDG_CONFIG_HOME/vim/vimrc.code"
    
  • To not care about current ‘mode’ in the most of situations, my vimrc now contains some logic to determine it and load appropriate config (generally it’s just sourcing vimrc.prose or vimrc.code). For now the logic just depends on file extension, but maybe it will be nice to make it use filetypes. However seems like it’s not so easy as it looks. My variant looks so (yea, that’s whole vimrc):

    let s:prose_types = ['md', 'txt']
    if index(s:prose_types, expand('%:e')) != -1
      source $XDG_CONFIG_HOME/vim/vimrc.prose
    else
      source $XDG_CONFIG_HOME/vim/vimrc.code
    endif
    

That’s it. Check the sources here (edit: as of 16 dec 2023 I migrated from vim to astronvim, so I have completely new .vimrc now and the link is no valid anymore. But you still can check early commits in my repository if you are interested in).

Have a nice day!