✒️ Vim for writing code and prose
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.
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 otherssource
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 sourcingvimrc.prose
orvimrc.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 wholevimrc
):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
Conclusion
That’s it. Check the sources here.
Thank you for reading this little article, I hope it will be useful to someone. Feel free to reach me if you have something to say.
And also take a look on the posts on similar topics: