Creating a Minimal Writing Environment in Neovim using a Simple Function

Creating a Minimal Writing Environment in Neovim using a Simple Function

This code is a proof, that your software developer is safe and you shouldn’t worry too much. I asked Claude, o1, 4o, deepseek to create a writer mode for neovim. They all failed. So I wrote my own… feel free to use it.

The Why?

As a writer, having a distraction-free environment is crucial for focusing on your craft. Neovim, a fork of the popular neovim text editor, offers a highly customizable platform for creating a minimal writing environment. In this blog post, we'll explore how to create a simple writing environment in Neovim using a custom function, without relying on any plugins.

The Goal

Our goal is to create a writing environment that is free from distractions, with a clean and minimal interface. We want to achieve the following:

The Function

To create our minimal writing environment, we'll define a custom function in our Neovim configuration file (init.lua). Here's the code:

local function create_margins()
  -- Store current buffer number
  local current_buf = vim.api.nvim_get_current_buf()
  -- Create splits
  vim.cmd("vsplit")
  vim.cmd("vsplit")
  vim.cmd("vertical resize 20")
  -- Set up left margin window
  vim.cmd("wincmd h")
  vim.cmd("wincmd h")
  vim.cmd("vertical resize 20")
  vim.cmd("enew")
  -- Set up middle window with content
  vim.cmd("wincmd l")
  vim.cmd("wincmd l")
  vim.cmd("wincmd l")
  vim.cmd("vertical resize 70")
  vim.cmd("enew")
  vim.cmd("wincmd h")
  vim.cmd("wincmd h")
  -- Hide split lines
  vim.opt.fillchars = { vert = " " }
  -- Remove numbers and make all windows 'invisible'
  for _, win in pairs(vim.api.nvim_list_wins()) do
    vim.api.nvim_win_set_option(win, "number", false)
    vim.api.nvim_win_set_option(win, "relativenumber", false)
    vim.api.nvim_win_set_option(win, "wrap", true)
  end
  -- Go to left window and remove all highlights including cursor
  vim.cmd("wincmd h")
  vim.cmd("wincmd h")
  vim.api.nvim_win_set_option(0, "winhl", "Normal:NonText,CursorLine:NonText,CursorColumn:NonText")
  vim.wo.cursorline = false
  vim.wo.cursorcolumn = false
  vim.opt.guicursor = "n:block-NonText"
  -- Go to right window and remove all highlights including cursor
  vim.cmd("wincmd l")
  vim.cmd("wincmd l")
  vim.api.nvim_win_set_option(0, "winhl", "Normal:NonText,CursorLine:NonText,CursorColumn:NonText")
  vim.wo.cursorline = false
  vim.wo.cursorcolumn = false
  vim.opt.guicursor = "n:block-NonText"
  -- Return to middle window and set its options
  vim.cmd("wincmd h")
  vim.opt.spell = true
  vim.opt.spelllang = "en_us"
  vim.opt.laststatus = 0 -- Always show the status line
end
vim.api.nvim_create_user_command("WritingMode", create_margins, {})

This function creates a new writing environment with the following features:

Using the Function

To use the create_margins function, simply run the :WritingMode command in Neovim. This will create the minimal writing environment, and you can start writing immediately.

Conclusion

Creating a minimal writing environment in Neovim using a simple function is a great way to customize your writing experience without relying on plugins. With this function, you can focus on your writing, free from distractions, and take advantage of Neovim's powerful features, such as spell-checking and line wrapping. Give it a try and see how it can improve your writing workflow!

My full neovim config, heavily based on josean:

https://github.com/sq5rix/ai_nvim

beatiful serene scene