vim

My Favorite Vim Oneliners For Text Manipulation

Vim one-liners for fast text edits: count words, format JSON, delete blank lines, sum the numbers in a file, extract IP addresses, and open files in splits.

To support my work, I’ve enabled ads on my blog. I hope you don’t mind them, as they help me continue creating valuable content. Your understanding and support mean the world to me! Thank you!


These are the vim one-liners I reach for most when editing files. Each one replaces a task that would otherwise take several manual steps.

  1. Count the Number of Words in a File:

A simple vim one-liner that counts the words in the file. Handy when you need a quick count without leaving the editor.

terminal
:%s/\w\+/\=submatch(0)+1/g | echo line('$')
  1. Format JSON with Python:

This one uses Python’s json library from inside vim to format a JSON document. Useful when you need to format a large JSON file locally.

terminal
:%!python -m json.tool
  1. Remove all the blank lines

Removes all blank lines from the file you are working on.

terminal
:g/^\s*$/d
  1. Opening Multiple Files at once in split mode
terminal
:argedit file1.txt file2.txt | all
  1. Calculating the sum of all numbers present in the file
terminal
:put =eval(join(getline(1, '$'), '+'))
  1. Extracting IP Addresses
terminal
:g/\(\d\{1,3}\.\)\{3}\d\{1,3\}/y A

If you have a vim tip or one-liner of your own, share it in the comments below. I would love to check it out.



I write occasionally; feel free to follow me on Twitter. To get new posts by email, subscribe to the newsletter.