DEV Community

gokayburuc.dev
gokayburuc.dev

Posted on

Adding Hashtags to Lines Containing Certain Words with Neovim

In this article, we will look at how to add a hashtag to the end of lines containing a specific word using Neovim. The following command is used to add the #generator tag to the end of every line containing the word "generator" in a file:

g/generator/norm A #generator
Enter fullscreen mode Exit fullscreen mode

We will analyze the operation of the command line by line, explaining in detail what the g (global) and norm commands used mean.


Detailed Analysis of Code

1. g/generator/

This section starts with the global command and targets a specific pattern throughout the file.

Description:

  • g (Global):
    Neovim's global command performs an action on all lines matching the specified pattern.
    That is, it checks every line in a file and selects all lines that contain the word "generator".

  • /generator/:

  • /: Specifies the beginning and end of the pattern.

  • generator: The word or pattern to search for. Here the word "generator" is targeted.

At this stage, Neovim scans the file and selects all lines that contain the word "generator".

Example:

If your file is like this:

This is a generator example.
Another line without the keyword.
The generator works fine.
Enter fullscreen mode Exit fullscreen mode

The g/generator/ command targets the following two lines:

This is a generator example.
The generator works fine.
Enter fullscreen mode Exit fullscreen mode

2. norm

The norm command is used to run normal mode commands on each target line in Neovim.

Description:

  • norm:
    Abbreviation for "Normal mode". It allows you to run a command in normal mode on selected lines.
    This automates operations that you can do manually.

  • This command performs the action or operation that you specify on each line that contains the word "generator".


3. A #generator

This section defines the operation to be performed on each target line.

Description:

  • A: In normal mode, moves the cursor to the end of the line and enters insert mode.
  • This allows you to add new text to the end of the line.

  • #generator:
    After the A command, this text is added to the end of the line. Here, a hashtag is created for the word "generator".

Example:

If the target line is:

This is a generator example.
Enter fullscreen mode Exit fullscreen mode

After the command is run, the line becomes:

This is a generator example. #generator
Enter fullscreen mode Exit fullscreen mode

How ​​the entire command works

g/generator/norm A #generator
Enter fullscreen mode Exit fullscreen mode

This command works as follows:

  1. g/generator/:
    Selects lines that contain the word "generator".

  2. norm:
    Runs normal mode commands on each selected line.

  3. A #generator:

  4. A: Moves the cursor to the end of the line and enters insert mode.

  5. #generator: Adds this text to the end of the line.

This process is repeated for all lines in the file that contain the word "generator".


Neovim Global and Norm Commands

1. Global Command (g)

The global command targets lines in a file that match a certain pattern and performs operations on these lines.

Usage:

g/pattern/command
Enter fullscreen mode Exit fullscreen mode
  • pattern: The word or phrase to search for.

  • command: The command to be run on lines that match the pattern.

Example:

g/error/d
Enter fullscreen mode Exit fullscreen mode

This command deletes all lines containing the word "error".


2. Norm Command (norm)

The norm command performs operations that can be performed in normal mode on the targeted lines.

Usage:

norm <normal-mode-command>
Enter fullscreen mode Exit fullscreen mode

Example:

g/error/norm dd
Enter fullscreen mode Exit fullscreen mode

This command uses the dd command to delete every line that contains the word "error".


Sample Usage

File Content:

This is a generator example.
Another line without the keyword.
The generator works fine.
Enter fullscreen mode Exit fullscreen mode

Command:

g/generator/norm A #generator
Enter fullscreen mode Exit fullscreen mode

Result:

This is a generator example. #generator
Another line without the keyword.
The generator works fine. #generator
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this article, we learned how to add hashtags to lines containing a specific word throughout a file using Neovim's powerful g (global) and norm commands. This command saves time and makes file editing easier on large files. If you frequently perform similar operations, this method can greatly speed up your workflow.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (2)

Collapse
 
voyeg3r profile image
Sérgio Araújo

The global command is a kind swiss knife, The one I remember now is this:
:g/https:/yank A

Collapse
 
gokayburuc profile image
gokayburuc.dev • Edited

Yes indeed, a swiss knife and i like to use this both in VIM and NEOVIM.



`g/def/norm A  " this is a function`  -> adds comment end of the line  wherever founds `def` in buffer 
`g/func/norm O // Function`  ->  adds comment to before line wherever has `func`  
`g/function/norm I // ->  comments all the lines has `function in JS 

Enter fullscreen mode Exit fullscreen mode

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay