How to use Nano on Linux? (text editor nano). Nano - Nano text editor cannot write file

💖 Do you like it? Share the link with your friends

List of hotkeys for nano. It is incomplete - it is missing some combinations that I was not able to test in action. Either because I don’t do yoga, or Gnome’s settings are such that some combinations or individual keys are intercepted by it - for example, combinations like Alt-( and Alt-). When typing them, you need to use Shift (i.e. Alt-( on a regular keyboard is converted to Alt-Shift-[). And in my system, Alt-Shift is linked to changing layouts. This is what overrides those hot keys that which I send to nano. But it’s all lyrics and lack of time, but here’s the list.

  • Home= Ctrl-a
  • End= Ctrl-e
  • PgUp= Ctrl-y
  • PgDn= Ctrl-v
  • Cursor keys= Ctrl-(f,b,n,p)
  • Tab= Ctrl-i
  • Backspace= Ctrl-h
  • Delete= Ctrl-d
  • Return= Ctrl-m
  • Help- Ctrl-g
  • Exit- Ctrl-x
  • Save file- Ctrl-o
  • Toggle syntax highlighting- Alt-y
  • Go to next word- Ctrl-Space
  • Go to previous word- Alt-Space
  • Go to start of line- Home
  • Go to end of line-End
  • Go to next paragraph-Alt-0
  • Go to previous paragraph-Alt-9
  • Next page-PgDn
  • Previous page-PgUp
  • Go to line no.- Ctrl-_
  • Go to end of file-Alt-/
  • Go to the beginning of the file-Alt-\
  • Go to matching bracket-Alt-]
  • Rewind down-Alt-=
  • Rewind up-Alt–
  • Switching text selection mode- Ctrl-6 (Ctrl-^). One of the few combinations in which both 6 and ^ are unambiguous (i.e., you don’t need to press shift for ^). To select text after turning on the selection mode, you must use the cursor keys.
  • Copy current selection- Alt-6 (Alt-^ or Ctrl-k Ctrl-u). Another combination that doesn't require pressing Shift for ^.
  • Cut current selection- Ctrl-k
  • Insert- Ctrl-u
  • Paste the contents of a file into a new buffer- Ctrl-r<имя файла>. If you need to insert the contents of the file into the current buffer in a place after the cursor, then you first need to turn off Multi-buffer mode for nano. This is done using the Alt-f combination.
  • Paste command output- Ctrl-r Ctrl-x
  • Search- Ctrl-w
  • Find next occurrence (Find Next…)- Alt-w or Ctrl-w Enter
  • Find-replace- Ctrl-\ or Alt-r
  • Search Help- Ctrl-w Ctrl-g
  • Align the width of the current paragraph (where the cursor is located)- Ctrl-j
  • Align the width of the entire document- Alt-j

I mentioned Multi-buffer mode - this is the nano operating mode in which you can edit several files at the same time. Switching between several buffers is done using combinations Alt-, and Alt-. .

I also mentioned syntax highlighting.

Syntax highlighting, like other nano settings, must be written in the nanorc file. For the current user, this file is usually located here ~/.nanorc, and an example with the configuration can be taken from /usr/share/doc/nano/examples/nanorc.sample.gz

In Ubuntu, you can find ready-made configurations with syntax highlighting at /usr/share/nano/ . This directory contains nano configuration files, one for each backlight type. They are enabled by default.

If you need to change colors and other backlight settings, you can also look at ready-made configurations with syntax highlighting. You can change files both in /usr/share/nano and in your home directory in the .nanorc file. In the first case, the changes will be global, in the second - only for the current user.

Nanotext editor command line, which comes pre-installed on almost every Linux distribution. It is often preferred by new users due to its simplicity compared to other command line text editors such as vi/vim And emacs. It has many useful functions, such as syntax highlighting, line numbering, simple search and many others.

Installing Nano Editor on Linux

If for any reason nano is not already installed on your Linux distribution, you should be able to install it easily using the following commands:

# apt install nano [For Ubuntu/Debian] # yum install nano [For CentOS/Fedora]

Nano Hotkeys

Nano uses key combinations to various functions, such as searching for text in a file, aligning text, etc. These combinations are very easy and visible while editing the file. They change automatically depending on what actions you take.

One thing you should know is that the keyboard shortcut with a ^ and a symbol (like ^W) is a combination of the Ctrl key and that symbol (Ctrl + W in our example).

A combination shown to begin with M means it must be completed by pressing the Alt key and the next character.

Below are the options you will see when you first open nano :

  • G Get help
  • ^O Write down
  • ^W Where
  • ^K Cut text
  • ^J Align
  • ^C Current position
  • M-U cancel (back)
  • ^X Exit
  • ^R Read file
  • ^\ Replace
  • ^U Split text
  • ^T Check spelling
  • ^_ Go to line
  • M-E cancel (forward)

You don't need to remember every option since it's always in front of you. You can get full list keyboard shortcuts by pressing ^G (or pressing F1), which will open the nano help menu. You will notice that some shortcuts can be used with a single key.

For example, F1 for help or F2 to exit nano.

Nano Editor Keyboard Shortcuts

Creating a new file in Nano

Create new file as easy as running nano:

This will open the editor and after saving the file, it will ask you to provide a name to save the new file with.


Create a new file in Nano

Open file in Nano

To open the file you can run:

$nano~/my_text_file.txt

The above command will try to open the file "my_text_file.txt" from your home directory. If the file does not exist, nano will try to create it.

Sometimes you may need to open a file and go to an exact row or column. Nano allows you to do this with:

$ nano +line,columns file

For example:

$ nano +3.2 ~/.bashrc

Will open your .bashrc file and the cursor will be located on the third line, second column.


Opening a file in Nano

Editing Files in Nano

After opening or creating files, you can start editing/recording immediately. Unlike vim, V nano there is no need to switch to edit mode. To move the cursor around the file, you can use the arrow keys on your keyboard.


Editing a file in Nano

Searching for text in Nano

You can search for text inside a file using ^W , which represents the " Where" This will open a search box above the menu where you can enter the text you're looking for:


Searching for text in Nano

You'll also see the bottom menu change to show some additional options. They pretty much explain themselves, so we'll look at the more important ones.

  • Search with regular expressions- press M-R (Alt + R keys) and enter your request using regular expressions that you want to use.
  • Go to line- press ^T (Ctrl + T) and then the line where you want to move the cursor.
  • Replace text— press the ^R (Ctrl + T) button in search mode or ^\ in normal mode. You will be prompted to enter your original text, after pressing Enter you will be prompted to enter the text that will be used for the replacement. Finally, you will be asked whether you want to replace the matching instance of your search or all matches. If you select "No", the cursor will be moved to the next match.
  • Go to first line— press ^Y (Ctrl + Y).
  • Move to last line- press ^V (Ctrl + V).

Copy/Paste/Cut Text in Nano

Nano's interface is very similar to GUI text editors. If you want to copy or cut text in a graphics editor, you first need to select it. The same thing happens in nano. To mark up text, press Ctrl + ^, then move the cursors using the arrow keys.

  • To copy selected text, press Alt + ^.
  • To cut selected text, press ^K (Ctrl + K).
  • To paste the selected text, move the cursor to the desired position and press ^U (Ctrl + U).

Copying and pasting text in Nano

Save file to Nano

If you want to save the current changes to the file, press the combination ^O (Ctrl + O). If you are editing a new file, you will be prompted to provide a name for the file. This will save your current changes and nano will remain open so you can continue making changes to the file.

Save file with backup

Sometimes when editing a file, you may need to keep temporary copies of the same file just in case. You can use the -B parameter in nano, which will create backup copy file you are editing. You can use it in combination with the -C option to tell nano where to save these backups as shown in the example:

$ nano -BC ~/backups myfile.txt

The above steps will make backup copies of the file myfile.txt in the folder " backups", located in the user's home directory. Please note that the catalog backup must exist, otherwise nano will tell you that the directory is invalid.

Exit Nano Editor

To exit nano, simply press ^X (Ctrl+X keys). If the file has not yet been saved, you will be prompted to save changes using yes/no or cancel exit.

Conclusion

Nano is an easy-to-use command line text editor that attracts users with its simplicity. Its interface is similar to that of graphic editors, making it ideal for Linux newbies.

Despite the fact that most actions in operating system Linux can be done through a GUI, if you want more control over the process and more flexibility, you will have to resort to manually editing the 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 Those who are already used 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 intuitiveness clear 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 the 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. 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. To open more detailed information about keyboard shortcuts press 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's 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 the mouse, but this is only if you are working in graphical interface. But if you are using the program in a clean terminal, this option will not work. For such cases, nano has own tools selection and copying.

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:

The nano editor may well play the role of a kind of shock absorber for a novice user. Yes, this is not emacs, or even joe. But it copes with the configuration task successfully. And in mastering and handling it is as simple as a rake. It is no coincidence that in many Linux distributions it is offered as a system-wide one. In Gentoo Linux, where during installation the need to manually edit configuration files arises quite often - this is simply the only editor available at the system installation stage. In CRUX and Archlinux, however, during installation it is adjacent to vi - which imitates the original one, and therefore may also be preferable.

The nano editor is launched in the command line with the following syntax nano /file_path/file. rice. 1

If you specified a non-existent file, it will be created.

After running the above command, you will see something similar to Fig. 2.

I think there is no point in explaining that moving the cursor, as well as deleting text, is done using the corresponding keyboard keys, as in other editors. The remaining functions of nano are controlled using control sequences, the list of which was taken from http://posix.ru/apps/nano_editor/ and is given below.

In nano there are two types of control sequences - actual control sequences, Control+letter, and meta-sequences, Meta+litera. The former are used for text editing and file operations. Control sequences are partially duplicated by function keys F1-F16(call F13-F16- through combination Shift+F1-F4). Meta sequences are intended to change editor settings (the same result is achieved with command line options).

Let me remind you that on a PC keyboard the role of the Meta key is usually performed by pressing the key Alt(in some layouts - specifically Alt"a right, or, conversely, left), or pressing and releasing a key Escape.

All basic Control sequences are listed at the bottom of the terminal window.

Control sequences are as follows (in parentheses are duplicate function keys and, sometimes, Meta sequences):

    Control+G (F1) - call the full help menu;
  • Control+X (F2) - exit the program;
  • Control+O (F3) - record the current file;
  • Control+R (F5) - insert a file into the current one;
  • Control+W (F6) - search for text in the current file;
  • Control+\ (F14 or Meta+R) - replacing text in the current file; First, the replacement text is entered, then, after clicking Enter- replaceable;
  • Control+Y (F7 or PgUp) - move to the previous screen;
  • Control+V (F8 or PgDwn) - move to the next screen;
  • Control+K (F9) = deleting (Cut, cut) a line at the cursor position and storing it in a buffer (cutbuffer);
  • Control+U - (F10) - inserts the contents of the cutbuffer into a line at the cursor position; if the latter has not changed, it performs the role of Undo (cancellation), which is not normally provided; can also be used for unlimited cloning of lines in an arbitrary part of the text - after deleting, it is enough to move the cursor to right place;
  • Control+C (F11) - display information about the position of the cursor in the form of VR
  • Control+T (F12) - spell checking (via an external spelling program, if it is installed and the corresponding option is enabled in the config, see below);
  • Control+P- move the cursor one line up;
  • Control+N- move the cursor one line down;
  • Control+F- move the cursor one character forward;
  • Control+B- move the cursor one character back;
  • Control+A- moving the cursor to the beginning of the current line;
  • Control+E- moving the cursor to the end of the current line;
  • Control+L- redrawing the current screen;
  • Control+^ (Meta+A) - selecting (and placing in the buffer) text, starting from the current cursor position;
  • Control+D- deleting a character at the cursor position;
  • Control+H- deleting a character to the left of the cursor;
  • Control+I- insert a tab character;
  • Control+J (F4) autocomplete the current paragraph;
  • Control+M inserting a newline character (CR) at the cursor position;
  • Control+_ (F13 or Meta+G) - jump to the specified line number.

Meta sequences usually work like switches. With their help, the following actions are performed:

  • Meta+C- enable/disable permanent cursor position;
  • Meta+I- enable/disable auto-indents;
  • Meta+Z- enable/disable suspension;
  • Meta+X- turn on/off the display of the hint zone;
  • Meta+P- enable/disable pico editor emulation mode;
  • Meta+W- enable/disable word wrap mode;
  • Meta+M- enable/disable mouse support (only when building with gpm support;
  • Meta+K- allow/prohibit cutting to the end;
  • Meta+E- enable/disable the use of regular expressions (regexp).
In addition, nano also provides an external configuration tool - the user config ~/.nanorc. By performing some manipulations in it, you can somewhat expand the functionality of the editor, in particular, provide syntax highlighting.

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 changed. 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 in at the moment edited 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 was inserted into the file 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 was 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 backups previous version edited file. 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 image 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 scratched the surface 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.

Tell friends