Alex’s Blog

Random musings on life, tech, and anything else

Buy me a hot chocolate! | About Me

How To Sort Lines In Place Very Quickly With Vim In VsCode, Vim, or Neovim

date: Sat May 6 09:34:50 PM CEST 2023

Background

In a recent pull-request review a colleague of mine suggested something very smart that showed a lot of empathy for our end users:

“What do you think about ordering the values of the struct so that stakeholders can scan the fields in alphabetical order?”

I hadn’t thought of that!

I immediately thought: “Shoot, okay, so I can probably extract the lines and sort them in python and then paste them back in…”

Setting About To Find A Solution

And then I realized that I am using Neovim which is an extensible, vim-based text editor. (Learn more about vim here) And this means I have super powers.

Amazed At How Easy This Was

I still don’t yet know how to list all the available commands (if you know, email me how to do this!) but I wondered if sort was one of the commands. I assumed it would follow how sort works on the commandline given that vim and the unix shell kind of grew up together.


UPDATE: Mon 15. May 09:50:22 CEST 2023

Shoutout to bheadmaster who commented on this article when I posted it to HackerNews as they replied with the solution to the above – isn’t it great when the internet is nice and helpful?

:help ex-cmd-index

Typing that in command mode from normal mode, SHFIT+: and then hitting enter will get you a list of all the built in commands.

From there you can navigate to the sort command and see how it can be used.

And even more helpful, if you’re familiar with the idea of man[ual] pages from the world of Unix/Linux you can do this:

:help :sort

And get a really helpful understanding of how the built in sort works and its flags and even some examples!

Check out the rest of the conversation from the folks at HackerNews (HN) here.


So I highlighted the rows I wanted in Visual Mode. Entered command mode (shift + :). Then I typed: sort i to sort the lines in place and irrespective of case.

And voila! It was done.

Check out a screen capture turned gif of the whole thing. Note: you’ll also get a feel for how vim/neovim works.

sort-in-place-with-neovim-or-vim
Note: VSCode Users

For this to work in the vim way as I have describe above you need the vim plugin. Find info about it here:

VS Marketplace Link

Just find the plugin, hit install, and you’re ready to go. Getting started with Vim is out of the scope of this small entry. But I’ll try anyway.

Small Vim Primer

To navigate you can use the up and down and left and right arrows or hjkl where:

The default mode is what’s called Normal Mode and it’s in this mode that you navigate stuff. You’ll probably find that the cursor is a thicker rectangle.

To Insert text you’ll want to be in Insert Mode and this can be done from Normal Mode by hitting the i key. From here you write out your text and then move back to Normal Mode to move around and such.

To save your work from command mode shift + :, type w

To go to Command Mode from Normal Mode use the key-combination: shift+: and you should see in the bottom left hand corner of the editor a “:” from which you can type commands like the above sort command.

To go back to Normal Mode you can hit ctrl+c or escape.

And of course, to exit, go to command mode, shift + : then x to write your changes, q to exit if there are no changes, and q! to exit even if there are changes and you don’t care about saving them.

I love everything freecodecamp.org stands for and what they do and they’ve a really solid introduction to vim if the above article I’ve just shared has inspired you to try it. Check it out on YouTube here


Feedback

Go Back Home

Copyright Alex Narayan - 2023