12

My Favorite Vim Oneliners For Text Manipulation

 1 year ago
source link: https://muhammadraza.me/2023/vim-onliners/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Aug 2, 2023

My Favorite Vim Oneliners For Text Manipulation

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!


In this post, I will be sharing my favorite vim one-liners that have significantly enhanced my vim workflow, making it more productive and efficient. As an avid vim user, I have extensively utilized these one-liners to edit files, and they never cease to surprise me with their ability to accomplish tasks swiftly, saving precious time. This is precisely what drew me to Vim - the unparalleled efficiency it offers when it comes to editing text.

  1. Count the Number of Words in a File:

A simple vim one liner which allows you to count the words in the file as this can be very handy.

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

Now this command is very simple but it allows you use python json library within vim to format your json document and its very useful when you need to format large json documents locally.

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

This allows you to remove all the blank lines from the file you are working on.

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

Lastly I hope you enjoyed reading this and got a chance to learn something new from this post and if you have any vim tips or one liners feel free to comment below and I would love to check it out.


I write occasionally feel free to follow me on twitter, also I run a newsletter and if you like reading technology news you can subscribe here

If you liked this article and think others should read it, please share it on Twitter or Facebook.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK