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
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.
The g/generator/
command targets the following two lines:
This is a generator example.
The generator works fine.
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 theA
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.
After the command is run, the line becomes:
This is a generator example. #generator
How the entire command works
g/generator/norm A #generator
This command works as follows:
g/generator/
:
Selects lines that contain the word "generator".norm
:
Runs normal mode commands on each selected line.A #generator
:A
: Moves the cursor to the end of the line and enters insert mode.#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
pattern
: The word or phrase to search for.command
: The command to be run on lines that match the pattern.
Example:
g/error/d
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>
Example:
g/error/norm dd
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.
Command:
g/generator/norm A #generator
Result:
This is a generator example. #generator
Another line without the keyword.
The generator works fine. #generator
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.
Top comments (2)
The global command is a kind swiss knife, The one I remember now is this:
:g/https:/yank A
Yes indeed, a swiss knife and i like to use this both in VIM and NEOVIM.