Skip to content

Instantly share code, notes, and snippets.

@kylepollina
Last active January 21, 2021 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylepollina/92aa01d7fef864cddeafc1e37f78304b to your computer and use it in GitHub Desktop.
Save kylepollina/92aa01d7fef864cddeafc1e37f78304b to your computer and use it in GitHub Desktop.
Vim Tips

Vim Tips

Collection of useful vim tips and vim features I've learned over the years

Auto format JSON files

If you are working inside of a JSON file, you can autoformat it by using Python's built-in json module.

Running this command:

:%!python3 -m json.tool --sort-keys

will automatically format and alphabetize the JSON file in your current buffer. I have this line in my vim config to easy run this:

command! JSON %!python3 -m json.tool --sort-keys

Diffing two files

Vim comes with a built-in program called vimdiff which can be used inside vim with the :diffthis command.

I use it like this: I have two json files that are slightly different, but I want to find out where. I open each file in a split buffer, then in I type :windo diffthis and voila! I have a diff inside my editor showing where the two file diverge. The :windo command runs diffthis on all open buffers in the current window.

Example:

https://i.imgur.com/oRKb8SI.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment