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

  • andnekon
    link
    fedilink
    arrow-up
    4
    ·
    3 months ago

    This is really nice

    But I have a habit to :w every 5 seconds, so I can’t really use it

  • sorrybookbroke@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    1
    ·
    3 months ago

    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?

    • sxwpb@lemmy.worldOP
      link
      fedilink
      arrow-up
      3
      ·
      edit-2
      3 months ago

      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.

      • Grey Cat@lemmy.world
        link
        fedilink
        arrow-up
        4
        ·
        3 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.

        • sorrybookbroke@sh.itjust.works
          link
          fedilink
          English
          arrow-up
          2
          ·
          3 months ago

          Ah shit, here’s a use-case, to see exactly what you’ve “undo’d”. I often remember that I had something in one state a while ago, undo until I see it, then have to carefully go back to ensure I return everything I did between the two states.

          With this, I save, using, press the binding, and see the diff clear as day.

          Now that binding is increadibly useful for me. Indobuae undo-tree but this’d be much more intuitive, atleast for me

          • Grey Cat@lemmy.world
            link
            fedilink
            arrow-up
            2
            ·
            3 months ago

            Yeah havn’t checked how to use undo-tree yet, gotta do that at some point, seems really useful.

            But I have put this post’s keymap in my config and first impressions are pretty good. Now I just have to remember to use it haha