this post was submitted on 18 Aug 2024
4 points (100.0% liked)

Neovim

2161 readers
5 users here now

founded 1 year ago
MODERATORS
 

Hello, I wanted to share a small keymap I made. It lets you inspect unsaved changes in the current file. It uses diff mode, in a vertical split.

To close the diff mode, just press q in the scratch buffer.

vim.keymap.set(
	'n',
	'<M-C-D>',
	function()
		local tmpft = vim.bo.filetype
		vim.cmd.vnew()
		vim.bo.filetype = tmpft
		vim.bo.buftype = 'nofile'
		vim.keymap.set(
			'n',
			'q',
			'<cmd>bw<cr>',
			{ noremap = true, silent = true, buffer = true }
		)
		vim.cmd('silent r#|0d_')
		vim.bo.modifiable = false
		vim.cmd('diffthis|wincmd p|diffthis')
	end,
	{ noremap = true }
)

edit: I discovered that this functionality is actually documented in the help pages (:h :DiffOrig). It’s basically the same action but bound to a command

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 0 points 2 months ago (1 children)

Oh shit that's pretty nice. Can't say I have a particular use for it myself but I like the idea.

Pure curiosity, what've you been using it for? Anyone else have use-cases?

[–] [email protected] 0 points 2 months ago* (last edited 2 months ago) (1 children)

I just use it all the time when I forget what I changed and want to make sure I’m ok with all changes before saving.

It is very similar to a command in emacs called M-x diff-buffer-with-file.

[–] [email protected] 1 points 2 months ago

You know what I usually do in that case ? Press undo until I'm back to no change haha. And each undo jumps you back to each change.

But this looks great and is worth considering, especially with how neat it is. Clever thinking on those buffer local mappings.