Nano create a file. Working with the nano editor: basic keyboard commands

Although most actions in the Linux operating system can be done through a graphical interface, if you want more control over the process and greater flexibility, you will have to resort to manually editing configuration files.

This way you can fine-tune each parameter and know what each line is responsible for. You can use different editors to work with configuration files. Beginners most often use Gedit, more experienced users who are already accustomed to the terminal prefer the nano editor, and many professionals want to use only Vim.

We already looked at how to use Vim in one of the previous articles, it is very flexible, but this flexibility came at the cost of complexity and a vertical learning curve. In this article I want to look at how to use the Nano text editor on the Linux operating system. This is a very simple editor and any beginner can handle it. But, in turn, this will be an excellent step towards the development of the terminal.

The Nano text editor is included in the GNU suite of programs and is the standard console editor for many Linux distributions. The development of the program, like many other programs from the GNU suite, began in the nineties.

In those days, there was a program for sending and receiving email - Pine. Part of it was a text editor called Pico, which you could use to write your messages. Many beginners liked Pico for its simplicity and intuitive interface. But Linux distributions, including Debian, could not include private binaries for Pine and Pico in their repositories.

Taking all this into account, it was decided to create a TIP or Tip Isn't Pico editor. But then, due to a name conflict with another utility, the editor was renamed nano.

The main advantage of nano is its ease of use. It's as easy to use as OpenOffice or Gedit. Therefore, it is very easy to learn how to use the program. Now let's move on to the question of how to use nano.

How to use the Nano editor?

First of all, it must be said that nano is a console text editor, which means that it is customary to execute it from the terminal. The following syntax is used for this:

$nano options /address/file/file

Using command line options doesn't necessarily require just specifying a file name, but in some cases options can be very useful. Let's look at them in more detail:

  • +number- specify the line in the file from which editing should begin;
  • -B- create a backup copy of the file when saving;
  • -WITH- backup folder;
  • -D- display text in bold;
  • -E- convert tabs to spaces;
  • -F- enable support for editing multiple files;
  • -H- save search and replacement history;
  • -I- do not read configuration from nanorc files;
  • -P- remember and restore the cursor position during the last edit;
  • -T- tab size in spaces;
  • -m- enable mouse support;
  • -v- read-only mode, you cannot save the file.

We have considered only the most important options of the program; naturally, there are actually many more of them, but these will be quite enough to get started. Next we will look at how to work with the program itself, its appearance and keyboard shortcuts.

To open a file, just pass it in the parameters to the nano command. If such a file does not exist, a new one will be created. For example:

The main window of the program looks very simple, in the top line there is the editor version and file name, as well as a notification whether the file has been changed. Next comes the text itself, and at the very bottom there is again the file name and the basic keyboard shortcuts that you can use to work with the program.

To move through the text, you can use the mouse, keyboard arrows, or special keyboard shortcuts. There is no menu in the program, and all actions are performed using hot keys. Perhaps some of them will not be easy to remember the first time, but after several times of practice everything will work out. Moreover, everything you need is displayed at the bottom of the screen. To activate a particular action, just press Ctrl and the specified letter. The Shift key is not used anywhere.

Basic keyboard shortcuts

Now let’s take a closer look at what keyboard shortcuts the nano linux text editor offers. For more information about keyboard shortcuts, click Ctrl+G:

When you finish working with the editor and save your changes, click Ctrl+X To exit if the file was not saved, the program will prompt you to do this:

Before closing the program, you need to save the changes to a file on disk. To do this, use the combination Ctrl+O. If you ran nano without parameters, you will need to enter a file name.

You can paste the contents of another file at the cursor position by clicking Ctrl+R and enter the target file name:

To undo the last action, click Alt+U. And you can repeat the last action again using Alt+E. To search by string or regular expression, click Ctrl+W, you can use standard perl regular expression syntax:

Using a keyboard shortcut Alt+R you can use search and replace:

We looked at the main key combinations that are used to control the editor functions. They may seem complicated to you, but to get started, it’s enough to just know Ctrl+O And Ctrl+X, everything else will come later.

Keyboard shortcuts for navigation

Let's now look at the hotkeys for quickly moving through text in the nano editor. Of course, you can always use the keys you are already familiar with PageUP, PageDown, Home, End and arrow keys, but this is not entirely convenient considering that nano has its own shortcuts. Although they are not as practical as Vim's, you can still use them.

To move the cursor forward or backward, press Ctrl+F And Ctrl+B and to move up/down one line use Ctrl+P And Ctrl+N.

To move to the beginning of the line use Ctrl+A, and at the end - Ctrl+E. There are also shortcuts for moving through pages, Ctrl+V moves one page down and Ctrl+Y- up the page.

But that is not all. You can navigate through words using Ctrl+Space And Alt+Space, and also press Ctrl+_ and enter the number of the desired line, and even a symbol separated by a comma.

To see where the cursor is now, click Ctrl+C.

Copy cut and paste

You can copy text with your mouse, but that's only if you're working in a GUI. But if you are using the program in a clean terminal, this option will not work. For such cases, nano has its own selection and copy tools.

To select a section of text, click Ctrl+^ move the cursor to the end of the desired section and press the same key combination again. All text between the start and end points, not including the cursor position, will be highlighted and you can copy or cut it.

To copy selected text, click Alt+^ and to cut it out - Ctrl+K. To insert cut text, move the cursor to the desired position and click Ctrl+U.

To cut an entire line of text, just click Ctrl+K, sometimes this is very convenient when editing configuration files.

Multiple files in Nano

Not everyone knows, but the nano linux text editor supports opening multiple files. You can't open them on the same screen, but you can freely switch between editing each of them.

To do this, pass several files to the utility when starting:

nano file1 file2

And then use keyboard shortcuts to move between them Alt+> And Alt+< . To close a file that is no longer needed, click Ctrl+X. Not the entire program will close, but only the active buffer with the file.

conclusions

In this article we looked at how to use the nano linux editor. Although it is very simple and aimed primarily at those new to Linux, there are a huge number of features that make the program a very powerful editor. If you have any questions, ask in the comments!

Finally, a short comparison between nano and vim:

For newbies who are just starting to learn Linux, the terminal is a lot of stress, but when it comes to the nano command...

Almost all servers running on Linux, including Ubuntu Server, do not use a graphical shell; all settings are made using the terminal. In desktop Linux distributions, the situation is slightly simpler, but still, if you want to fine-tune the system, then you can’t avoid getting familiar with the terminal. One of the most difficult “commands” to master is Nano Linux. This command, " nano ", nothing more than a console text editor.

How to Use Nano Text Editor in Linux Terminal

In this article we will try to clearly tell you how to use this text editor so that Linux does not seem such a complex operating command.

Installing Nano on Linux

If the terminal does not respond in any way to the nano command, then this text editor is not installed. To install it, enter the command:

sudo apt install nano

Everything is ready to launch!

Launch of the Nano

Text editor nano only works in the terminal. One more clarification, before we begin, the Linux operating system is case sensitive, if a word or part of a command is written with a capital letter, then you need to type it that way, otherwise you will get an error.

If you are in the directory (folder) where the required text file is located, then, in order to start editing it using nano you need to write in the terminal like this:

Nano index.html

By the way, the team will help you find out which directory you are in now pwd , and what files are located nearby? ls .

If we are editing a file that is located in another directory (folder), then you need to specify the full path in the terminal:

Nano /var/www/html/index.html

If you suddenly edit a file sensitive to word wrapping, then enter the command in the terminal nano with additional key -w :

Nano -w/var/www/html/index.html

In order to open a document on a specific row or column nano + row, column file_name

For example nano +3,2 /etc/hosts

Nano hotkeys for control in a text editor

With text information entered into nano no one should have any problems. But how convenient it is to manage text data, delete it in bulk, move it, and most importantly, how to save it in nano, many people have problems with this.

Action name nano hotkeys
Open a file on a specific line

+ line_number

For example:

nano +25 log.txt

“Slow” movement to the sides - up, down, left, right Keys

or

Ctrl F And Ctrl B

←→ or

Ctrl P And Ctrl N

Move to the beginning of the file Alt | or Alt\
Move to the end of the file Alt / or Alt?
Move forward one word Ctrl Space
Move to the beginning of the line Ctrl A
Move to the end of the line Ctrl E
Move back a word Alt Space
Move to line by number Alt G or Ctrl -
Next page (if long text file) Ctrl Y
Previous page (if long text file) Ctrl V
Cut "delete" entire line Ctrl K
Erase everything from the cursor to the end of the file ALT T
Paste a previously cut or copied line Ctrl U
Select part of the text, copy or cut it (or delete) Move to the required section of text

Clamp ALT A

and move the keys ↓←→

Copy Alt-6

Cut or delete Ctrl K

Insert Tab Ctrl I
Paste text information from another file Ctrl R

indicate the path to the file

In nano show row and column number CTRL C
Find the required word in a text file Ctrl W

write the search word

Find next Ctrl W
Saving changes to a file Ctrl O

press Enter

Saving changes to a file and exiting the nano editor Ctrl O

press Enter

Do not save changes made Ctrl Z
Save changes and rename file Ctrl X

press Y

enter a new file name

press Enter

I hope using the nano terminal text editor will no longer be such a big headache as before!

Do you still have additional questions? Write them in the comments about what you did or vice versa!

That's all! Read more articles and instructions in the section. Stay with the site, it will be even more interesting!

And Unix-like operating systems.

GNU nano is a clone of the well-known Pico editor. It was developed in 1991 and was originally called TIP. True, in 2000 it was renamed. Official resource –nano-editor.org.

The new editor is based on the curses library and distributed under the GNU GPL license. Today it is included in Ubuntu distributions by default and does not need to be installed.

Where to start? To launch the console editor, you need to open a terminal (you need to find it in the Applications-Standard menu) and run the command:nano . To create and open a file, the command is –# nanofilename .

To work with it you only need a keyboard. Move the mouse away.

At its core, Nano is designed to emulate the functionality and usability of the original UW Pico. The editor is divided into 4 parts. The first displays the program version, file name and current changes in it. The second is the file that is currently being edited. The third bottom part shows important messages. The fourth shows those combinations that are most often used.

The editor is controlled using a keyboard shortcut. Let's look at some examples: save the current documentctrl+o , accessing the search menuctrl+w , get available keyboard shortcutsctrl+g . Combinations with Meta keys are also used, for example,meta+s – enable or disable smooth scrolling.

Easy to learn, Nano is suitable for novice users, but also handles configuration tasks flawlessly.The improved version nano 2.0 distinguished itself with its current capabilities : support for UTF-8 encoding, improved syntax highlighting, the ability to copy text without cutting it, the ability to print every character that was pressed on the keyboard, repeat the last search w/o query (Meta key + W/ Ctrl+W), spell checking / replacing only the selected section of text, shifting the selected text to the right, moving to the beginning and end of the paragraph, searching in the file manager shell, converting files of different formats automatically one to one.

Main commands in nano:

  1. to call the full prompt directory -CTRL+G or F1 ;
  2. to leave the program -CTRL+X or F2 ;
  3. to write the next file –CTRL+O or F3 ;
  4. to insert a file into the next one -CTRL+R or F5 ;
  5. to search for text in the next file –CTRL+W or F6 ;
  6. to replace text in the next file -CTRL+\(F14 or Meta+R) ; First, the replacement text is entered, then, after pressing Enter, the replaced text is entered;
  7. to migrate to preview screen –CTRL+Y (F7 or PgUp) ;
  8. to migrate to upcoming screen –CTRL+V (F8 or PgDwn) ;
  9. CTRL+K (F9) = deleting (Cut, cut) a line at the cursor position and storing it in a buffer (cutbuffer);
  10. to insert the contents of the cutbuffer into a line at the marker positionCTRL+U – (F10) ; if the latter has not changed, it performs the role of Undo (cancellation), which is not normally provided for; can also be used for unlimited cloning of lines in an arbitrary part of the text - you just need to move the cursor to the desired place after deleting;
  11. to display information about the cursor position in the form -CTRL+C (F11) ;
  12. to check spelling (via an external spelling program, if it is installed and the corresponding option is enabled in the config, see below) –CTRL+T (F12) ;
  13. to migrate the marker (cursor) one line up –CTRL+P ;
  14. to migrate the marker one line down -CTRL+N ;
  15. to migrate the marker one character forward –CTRL+F ;
  16. to migrate the marker one character back -CTRL+B ;
  17. to migrate a marker to the beginning of the next line -CTRL+A ;
  18. to migrate a marker to the end of the next line -CTRL+E ;
  19. to redraw the next screen –CTRL+L ;
  20. to select (and place in the buffer) text, starting from the next cursor position -CTRL+^ (Meta+A) ;
  21. to eliminate a character at a marker position –CTRL+D ;
  22. to eliminate the character to the left of the marker –CTRL+H ;
  23. to insert a tab character –CTRL+I ;
  24. to autocomplete the next paragraph –CTRL+J (F4) ;
  25. to insert a newline character (CR) at a marker position –CTRL+M ;
  26. to go to the intended line number –CTRL+_ (F13 or Meta+G) .

As we wrote above,using the Meta key you can also execute commands. List of the following commands with Meta:

  1. for connection i/disable permanent marker position –Meta+C ;
  2. for connection I / disable auto-indents –Meta+I ;
  3. for connection i/disable suspend –Meta+Z ;
  4. for connection I / disable the output of the hint zone –Meta+X ;
  5. for connection I/disable pico editor emulation mode –Meta+P ;
  6. for connection I / disable word wrap mode –Meta+W ;
  7. for connection i / disable mouse support (only when building with gpm support -Meta+M ;
  8. for the command to enable/disable cutting to the end –Meta+K ;
  9. for connection I / disable the use of regular expressions (regexp) –Meta+E .

There is also a command that will remove absolutely everything on your PC. Be vigilant and never execute commands that you do not know. Check out the combinationrm -rf / and equivalent to it:echo “test... test... test...” | perl -e ‘$??s:;s:s;;$?::s;;=]=>%-(<-|}<&|`{;;y; -/:

The Nano editor is designed to emulate the functionality and convenience of the UW Pico text editor. The following screenshot shows the editor in action:

Here's how the official documentation explains the different sections of the editor:

There are four main editing sections. The top line shows the version of the program, the current file being edited, and whether the file has been modified. Next, in the main editor window, you can see that the file is being edited. The third line is the status bar, it shows importantmessages. The bottom two lines show the most commonly used keyboard shortcuts in the editor.

The following sections describe the basic methods of use, as well as some of the features of the Nano.

1. Create and open files

To launch the editor, run:

or (if we immediately want to specify the file name)

$nano

The second method is suitable for both creating a new file and opening an existing one. You can specify the absolute path to the file (if the file is located remotely).

The example below shows what an open file looks like:

As you can see, there are tips below to make your work easier.

2. How to save a file in Nano

Here's an example:

Nano also allows you to save files in a variety of formats. For example, you can save the file in DOS format by pressing Alt+d. Similarly, to save the file in Mac format, press the keyboard shortcut Alt+m. Please note that to change the format, you need to start the normal saving process by pressing Ctrl+o and then press Alt+d or Alt+m to select the format.

3. How to cut and paste text in Nano

To cut and paste a line, first move the cursor to that line. Now press the keyboard shortcut Ctrl+K (cut) and then send the line to the place where you want to paste it and finally use the keyboard shortcut Ctrl+U (paste).

For example, in the screenshot below, there is a requirement to cut the first line and paste it at the end. Go to the line and press the key combination Ctrl+K. Now, move the cursor to the very bottom and press the Ctrl+y key combination.

You can copy a specific part of a string rather than the entire string. To do this, you must first select a word/part of a line by pressing Ctrl+6 (or Alt+a). You can now press Ctrl+K to cut and Ctrl+U to paste.

As you can see, we can selectively cut and paste the desired part of the line.

4. How to find and replace a word in nano

This tool will allow you to find the word you need and also replace it with another one.

To search for a word in nano, press the keyboard shortcut Ctrl+w. You will then be asked to enter the word you want to search for. After entering the word, press Enter and the tool will show you matching entries.

You can also replace a word with a word from another site by pressing Ctrl+\. When you press this key combination, nano asks you for the word you want to replace. After entering the word, press the Enter key and now it will ask for a replacement word. After this, nano will ask you to confirm the changes.

The following three screenshots will help you understand the process.

5. How to insert another file into the current one

If you want, you can also paste data from another file into the one you're currently editing in nano. To do this, press the key combination Ctrl+p, and then specify the path to the file you want to open.

Below are sample screenshots of this feature:

As you can see in the screenshot above, the text in the file was inserted from the cursor position.

6. Display cursor position

If you want, you can also check the cursor position in the file. This can be done by pressing the keyboard shortcut Ctrl+C.

As you can see in the screenshot above, after pressing Ctrl+c, a position cursor appeared, and all the information about this is in the status bar (the thing that is underlined is the third line from the bottom of the window).

7. How to place the cursor on a specific row or column at startup.

If you want, you can also place the cursor on a specific line and column of the file when you start the editor. This can be done by providing the required information when starting the editor. This function uses the command line switch +row,column (row specifies the row number, and column specifies the column number).

$ nano +line,column

For example:

$ nano +2.5 abc.txt

As you can see in the above screenshot, the cursor is on the second line and fifth column while opening the file.

8. How to make a backup copy of a previous version of a file.

The tool allows you to create backup copies of the previous version of the file you are editing. This is done after you make changes and save the file. The function can be accessed by using the -b command line option.

$nano -B

For example

$ nano -B abc.txt

The backup will be saved to the current directory with the same name, but suffixed with a tilde (~).

Please note that files created for the first time cannot be backed up.

9.Backup files to a specific directory.

The tool allows you to save all your backup files in a specific directory. This feature can be accessed using the -C or –backupdir command line option, which requires the path to the folder where you want to save the backup files.

$ nano -B –backupdir=

10. How to convert tabs to spaces.

This tool also allows you to convert tabs to spaces. This feature can be accessed using the -E command line option.

$nano -E

For example, the following illustration shows text that contains tabs between words.

Now, since the editor was started with the -E parameter, the changes are saved, the tabs are converted to spaces.

Conclusion

Considering that the article is intended for beginners, we have only superficially examined this system. Use the keyboard shortcut Ctrl+G to learn more about Nano. Oh, and yes, don't forget to try the basics we discussed here.

Editor nano is a console text editor. It is available in almost all Linux distributions, is available on macOS and can be run on Windows. nano is often used to edit configuration files when a GUI is not available.

Control in nano is carried out from the keyboard using hotkeys. At the bottom of the editor is a list of basic keyboard shortcuts. The ^ symbol represents the Ctrl key. For example, ^X means the keyboard shortcut Ctrl+X. Character case is not sensitive; Ctrl+X is equivalent to Ctrl+x .

Let's look at how to perform basic actions when working with nano.

Create a new file

To create a file, simply launch the editor without parameters. When you close the editor, you will need to enter a file name.

You can also immediately set the name of the new file:

After executing the command, the nano editor will launch and a new empty file will open for editing.

Open an existing file for editing

To edit an existing file, you need to launch the nano editor and specify the path to the file you want to open:

Saving changes

Save and continue working

The following message will appear in the status line: File Name to Write: file_name(if the file name is missing, then you need to enter the file name). To save changes, press Enter.

Save and exit

In this case, if you changed the file, the question will appear: " Save modified buffer (ANSWERING No Will DESTROY CHANGES)?". Press the y key. A question will appear asking you to enter a file name. Enter a new file name or just press Enter. The changes will be saved and the editor will close.

Exit without saving changes

To exit the editor and not save changes, use the keyboard shortcut:
Ctrl+X

There is a question" Save modified buffer ..?", press n.

Copy and paste

Copying and pasting rows

To cut entire line and place it in the buffer, move the cursor to the desired line and press:
Ctrl+K

To insert click the line from the buffer:
Ctrl+U

Copy and paste free text

To copy arbitrary text to the clipboard, you must first select it. To start selection Place the cursor at the beginning of the text you plan to copy and press:
Ctrl+6

Move the cursor, the text will be highlighted.

To copy selected text to the clipboard, press:
Alt+6

or, to cut selected text, click:
Ctrl+K

Move the cursor to the place where you want to paste the text from the buffer. To insert text from buffer click:
Ctrl+U

Search text

To search for text inside a file, use the keyboard shortcut:
Ctrl+W

You need to enter a search string and press Enter.

The cursor will be placed on the first occurrence of the searched text (relative to the cursor). To move to the next occurrence, click:
Alt+W

To stop searching, click:
Ctrl+C

Find and replace text

To find and replace text, use the keyboard shortcut:
Ctrl+\

You will be prompted to enter text to search. Enter a search string and press Enter.
You will then be asked to enter the text you want to replace with. Type a string and press Enter.
Next, a request will be issued to replace the found occurrence of the required string. You can press the A key to replace all occurrences in the file at once, or use the Y or N keys to replace or not replace found occurrences of the search string.

Hotkeys

To display information on all hotkeys, use the combination Ctrl+G.

The most commonly used nano hotkeys are:

HotkeysDescription
Ctrl+AMove the cursor to the beginning of the line.
Ctrl+EMove the cursor to the end of the line.
Ctrl+YMove the cursor 1 page up (analogous to PageUp)
Ctrl+VMove the cursor 1 page down (analogous to PageDown)
Ctrl+_Go to a specific line (you will need to enter a line number).
Ctrl+CShow what line and position the cursor is on.
Ctrl+WSearch for text in a file. You must enter a search string.
Ctrl+\Find and replace text in a file. First enter the search string, then the replacement string.
Ctrl+DDelete the character under the cursor.
Ctrl+KDelete the current line.
Ctrl+OSave changes without closing the editor.
Ctrl+XExit the editor. If the file has been modified, you will be prompted to save your changes.

Something else

A couple more interesting tricks when working with nano, which not everyone knows about.

Ctrl = Esc Esc

Instead of the Ctrl key, you can use double-clicking the Esc key. For example,
Esc+Esc+X
This is equivalent to Ctrl+X.

Only for reading

To open a file read-only, use the -v switch:

File backup

You can make sure that when a file is changed, a backup copy is created. To do this, use the -B switch:

If you change a file and save the changes, a backup copy of the file will be created, with the contents of the file as it was before the change. The backup name is the same as the original file name with a tilde ~ at the end.