Vim Selection Groups

Dec 4, 2023
In Neovim, the scope that regular expression select group is out of this world. Here is a screencast of me making a json object out of just text data.

Table of Content

Click to expand

In Neovim, the scope that regular expression select group is out of this world. Here is a screencast of me making a json object out of just text data. ๐Ÿง‘โ€๐Ÿ’ป

Lets assume this is vim

Neovim has been one of those tool, that I had been procastinating on for majority of my carrer. Now, I have come to embrace it and I am a month old neovim users. You can read more about this here.

My journey to VIM, from ๐Ÿ˜• to ๐Ÿ˜„ _Vim has been throne till my software development. Something that I always ignored and looked away from. Here I wouldโ€ฆ_blog.stackademic.com

Here our text input follows a pattern. Pattern like of a csv file. Fields are separated by commaย ,

We will have to select that and use the find and replace command. The keybinding for that is

:'<,'>s/

You will only need to pressย :s asย :โ€™<,โ€™> comes due to selection done in the visual line mode

The syntax for the find and replace command is

s/<input\_regex>/replacement string/gc

Here, g,c are flags like global means all and c means confirmation. It will ask for prompt

Select groups are those characters that are based upon the values provided in the input selection regular expression.

Here using \v means that we willnโ€™t have to escape brackets. In the above example. the target groups are

(*.), (*.), (*.) -> Everything from beginning till 1st comma, Between 1st and 2nd comma and everything else. We can reference those selection group on the replace part using \1 \2 and \3 in the right side. \0 means everything. It is like the syntax in the awk command. I also used some json like fields to add extra text in the replace side.

If you guessed the replace part, you can see that the output of this must look like some JSON structure. You are right.๐Ÿฅณ So final output looks like this.

Tricks like this has made something more effective in me and I love tricks. You might have some tricks that I may not know. Share me your NeoVim tricks.

I want to find a way to reference registers in the replace part of find and replace. Imagine if we could use register content in replace part. This is what I will be looking into. #neovim #vim #programming

For the time being, comments are managed by Disqus, a third-party library. I will eventually replace it with another solution, but the timeline is unclear. Considering the amount of data being loaded, if you would like to view comments or post a comment, click on the button below. For more information about why you see this button, take a look at the following article.

Related Posts


You may also like

Reading List Contact
server

© Nirjal Paudel