Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Introduction

Nano is a simplistic, user-friendly text editor widely used in Unix-based systems. While it might seem limited in functionality compared to more advanced editors, Nano still offers several powerful features, including the ability to comment and uncomment multiple lines efficiently.

In this tutorial, we’ll explore the process of commenting out multiple lines in the Nano editor. Firstly, we’ll discuss how to select multiple lines in the Nano editor. Next, we’ll cover using the built-in shortcut for quickly commenting lines. After that, we’ll see how to use the Ctrl + T feature in Nano to run external commands like sed and awk to prepend # to each line.

2. Selecting Multiple Lines in the Nano Editor

Before commenting out multiple lines, we usually need to select them. To select multiple lines in the Nano editor, we follow a few steps.

First, we open the file using the nano command:

$ nano script.sh
#!/bin/bash
for i in {1..5}
do
  echo "Welcome $i times"
done

Next, we position the cursor at the start of the text we want to select. Then, to enter the selection mode, we press Esc+A or Alt+A to start selecting. Finally, we use the arrow keys to move the cursor and select the desired lines.

Alternatively, we can use Shift in combination with the arrow keys to select the text.

3. Commenting Lines Using Esc+3 (or Alt+3)

Once we’ve selected the lines we want to comment out, the next step is to add the comment symbols. In this section, we discuss several methods to comment out multiple lines in the Nano editor.

3.1. Commenting Multiple Lines

In recent versions of the Nano editor, we can easily comment or uncomment selected lines using a simple keyboard shortcut.

First, we select the lines as already discussed. Then, we press Esc+3 or Alt+3. The default comment character appears at the beginning of each line:

$ cat script.sh
##!/bin/bash
#for i in {1..5}
#do
#  echo "Welcome $i times"
#done

Of course, we should remember to save the changes within nano beforehand.

3.2. Modifying the Comment Character

In addition, we can modify the comment character option in the ~/.nanorc file, where the configuration of Nano is stored.

In particular, to change the comment character, we modify the comment statement in the ~/.nanorc file:

comment "string"

The comment “string” setting in the ~/.nanorc file defines how the Nano editor handles commenting and uncommenting lines of code.

When the string includes a pipe character |, it indicates a bracket-style comment format. For instance, using “/*|*/” applies to languages like CSS. In this format, the characters before the pipe /* are prepended to the start of each line, while the characters after the pipe */ are appended to the end. This approach is useful for languages that utilize block comments or require both opening and closing comment symbols.

If the string doesn’t contain a pipe character, the Nano editor simply prepends the entire string to the beginning of each line. For example, using “#” adds # at the start of each line, which is typical for scripting languages like Bash, Perl, Python, and others.

When empty double quotes “” are specified, Nano disables the comment and uncomment functions. This is particularly useful for file types, such as JSON, where traditional commenting isn’t needed or supported.

By default, Nano uses “#” for the comment string, which works effectively for many scripting and programming languages.

4. Using CtrlT to Execute External Commands

Nano’s Ctrl + T feature is a powerful tool that allows us to execute external commands directly from within the editor. This capability is particularly useful when we need to perform more advanced operations, such as commenting out lines using commands like sed and awk.

4.1. Commenting Lines Using the sed Command

To begin, we can utilize the sed command to comment out multiple lines in a file. First, after selecting the lines we want to comment, we press Ctrl + T to open the Execute Command prompt within the Nano editor.

Once the prompt is open, we enter the following sed command:

sed -i 's/^/#/' script.sh

Lastly, we press Ctrl + X to exit the Nano editor. To confirm the changes in the script.sh file, we use the cat command:

$ cat script.sh
##!/bin/bash
#for i in {1..5}
#do
#  echo "Welcome $i times"
#done

This command works by prepending a # symbol to the beginning of each selected line. Specifically, the -i option instructs sed to edit the file in place, ensuring that the changes are applied directly to the file rather than outputting the result to the screen.

Moreover, the expression ‘s/^/#/‘ signifies a substitution operation where ^ matches the start of each line, and # is added as a comment character.

4.2. Commenting Lines Using the awk Command

Alternatively, we can utilize the awk command to achieve similar results. To comment out lines using awk, we begin by selecting the desired lines in Nano, followed by pressing Ctrl + T to access the Execute Command prompt. In the prompt, we enter the following awk command:

awk '{print "# " $0}' script.sh > temp && mv temp script.sh

As a result, this code comments the entire file:

$ cat script.sh
##!/bin/bash
#for i in {1..5}
#do
#  echo "Welcome $i times"
#done

In this command, awk processes each line of the file, prepending # to the beginning. The expression ‘{print “# ” $0}’ specifies that # should be added before each line’s content, represented by $0. The output is redirected to a temporary file named temp.

Subsequently, the mv command renames this temporary file back to script.sh, effectively replacing the original file with the modified version. After executing the command, we save and reload the file in Nano to observe the changes.

In summary, the Ctrl + T feature in Nano provides a versatile way to execute external commands like sed and awk to edit the content without leaving the Nano editor.

5. Conclusion

In this article, we explored how to comment out multiple lines in the Nano editor.

Initially, we covered the fundamental steps for selecting multiple lines in the Nano editor. Next, we explored how to use Nano’s built-in shortcut for quickly commenting out lines. Additionally, we discussed how to use Ctrl + T feature to execute external commands.