Neovim Floating Journal
Published Sun Jan 28 2024 Updated Thu Feb 15 2024Since I have begun using Neorg one of my favorite features is having a daily Neorg journal within quick reach. My initial setup bound :Neorg journal today
to <leader>j
. This enables me to make quick notes while working in code. While this feature has been great, I don’t like taking up my current window with my journal. To try something new I used the floating window api to open my daily note in a floating window now! Here is the code using vim.api.nvim_create_buf
I mentioned in my post on GitHub search.
(fn []
(let [
win_width (math.floor (* vim.o.columns 0.6))
win_height (math.floor (* vim.o.lines 0.8))
bufnr (vim.api.nvim_create_buf true false)
]
(vim.api.nvim_open_win bufnr true {
:width win_width
:height win_height
:row (/ (- vim.o.lines win_height) 2)
:col (/ (- vim.o.columns win_width) 2)
:relative "editor"
:border "rounded"
:title "Daily Journal"
:title_pos "center"
}))
(vim.api.nvim_command ":Neorg journal today")
)
The bulk of this code is for sizing the window. While not too difficult to write, I wish I could just tell the window api “center my window at 60% of window width” in a straightforward way. My first implementation used vim.fn.winwidth
and vim.fn.winheight
to get the window size. This resulted in my float being sized relative to the window rather than the full editor. By using vim.o.columns
and vim.o.lines
I get the full editor size! I’ll likely write some utility code that I’m sure many other users have written before as well.
The options provided by nvim_open_win
are nice! I appreciate having a couple borders to choose from and a nice way to set a title. Once my window has been opened all I needed was nvim_command
to open my Neorg file.
That is all for now! I’m excited to use my floating journal, and if it doens’t work out I have <leader>nj
bound to open it the old way. To improve this script I would like a way to press q
in normal mode to dismiss the window.