Windows.  Viruses.  Notebooks.  Internet.  office.  Utilities.  Drivers

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

So you can fine-tune each parameter and you will know what this or that line is responsible for. You can use different editors to work with configuration files. Beginners tend to use Gedit most often, more advanced users who are already used to the terminal prefer the nano editor, and many pros only want to use Vim.

We already covered 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 part of the GNU software suite 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 e-mail - Pine. Part of it was the Pico text editor, 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.

Given all this, it was decided to create a TIP editor or Tip Isn "t Pico. 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. Working with it is as easy as with OpenOffice or Gedit. Therefore, it is very easy to learn how to work with 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 is not necessarily enough to just specify a file name, but in some cases the options can be very useful. Let's consider them in more detail:

  • + number- specify the line in the file from which you want to start editing;
  • -B- create a backup copy for 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 editing;
  • -T- tab size in spaces;
  • -m- enable mouse support;
  • -v- read-only mode, you can't save the file.

We have considered only the most important options of the program, of course, that in fact there are many more of them, but for a start these will be quite enough. Next, we will look at how to work with the program itself, its appearance and keyboard shortcuts.

To open any file, it is enough to pass it in the parameters to the nano command. If no such file exists, a new one will be created. For example:

The main window of the program looks very simple, the top line contains the editor's 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 again the file name and the main keyboard shortcuts that you can use to work with the program.

You can use the mouse, keyboard arrows, or special keyboard shortcuts to move through the text. 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 an action, just press Ctrl and the specified letter. The Shift key is not used anywhere.

Basic keyboard shortcuts

And now let's take a closer look at what keyboard shortcuts the nano linux text editor offers. For more information on keyboard shortcuts, click ctrl+g:

When you're done with the editor and save your changes, click Ctrl+X to exit if the file has not been saved, the program will prompt you to do so:

Before closing the program, you need to save the changes to a file on disk. To do this, use a combination Ctrl+O. If you started nano with no options, you will need to enter a filename.

You can paste the contents of another file at the cursor position by pressing ctrl+r and enter the name of the target file:

To undo your last action, press Alt+U. And you can repeat the last action again with Alt+E. To search by string or regular expression, click ctrl+w, you can use standard perl syntax regular expressions:

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

We have reviewed the main key combinations that are used to control the functions of the editor. They may seem complicated to you, but for a start it is enough to know only Ctrl+O And Ctrl+X everything else will come later.

Keyboard shortcuts for navigation

Let's now look at keyboard shortcuts for quickly moving through text in the nano editor. Of course, you can always use the familiar keys PageUP, PageDown, Home, End and arrow keys, but this is not very convenient given that nano has its own shortcuts. While not as practical as Vim's, you can still use them.

Press to move the cursor forward or backward. ctrl+f And ctrl+b and to move up/down one line use ctrl+p And Ctrl+N.

To move to the beginning of a line, use Ctrl+A, and at the end - ctrl+e. There are also shortcuts for moving through pages, ctrl+v moves down the page and ctrl+y- one page up.

But that is not all. You can navigate through words with Ctrl+Space And Alt+Space and also press ctrl+_ and enter the number of the desired line, and even a character 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 that's only if you're using the 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 highlight a section of text, press 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 point, not including the cursor position, will be selected and you can copy or cut it.

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

To cut an entire line of text, just press Ctrl+K, sometimes it is very handy 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 one.

To do this, pass several files to the utility at startup:

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 whole 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 designed primarily for Linux beginners, 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 small comparison of nano and vim:

All commands are given by pressing key combinations - function buttons or a combination of Ctrl-letter, Alt-letter.

Alt is emulated by a single press on Esc, Ctrl - double. Instead of Alt-X you can type Esc X , instead of Ctrl K you can type Esc Esc K .

The up-left-right-down arrows do exactly what they're supposed to.
Line start - Home
End of line - End
Page down - PgDn
Pages up - PgUp

One word ahead - Ctrl-Space
One word back - Alt space(cursor at the beginning of the word)
To the beginning of the file - Alt-| or Alt-\ (in short, Alt- and "this" button)
To the end of the file - Alt-/ or Alt-? (similar to Alt "this" button)
Down to the first blank line - Alt-0(or Alt-))
Up to the first empty line - Alt-9(or Alt-()


Turn on turn off line wrapping - Alt L
Jump to the line with the specified number - Alt-G

Text is selected either with the mouse or with the keyboard. From the keyboard, the selection occurs like this: first, the beginning of the selection is marked: Alt-A or Ctrl-^. This is followed by navigation - and until the action is performed on the text in the buffer, the selection is saved.

The selected text can be copied to the clipboard - combination Alt-6.

Next, we can paste the text from the clipboard − Ctrl-U or F10.

In addition, for ease of editing, Ctrl-K in the absence of a selection deletes the line. Either entirely, or from the cursor to the end of the line. Behavior is switched by combination Alt-K(by default, the mode is to delete the entire line, and Alt-K switches it back and forth).

Note that the deleted line is in the buffer, so it can be pasted with Ctrl-U .

Another combination for lazy people - Alt-T, erasing everything from the cursor to the end of the file (trunkate command).

To insert text from another file, use the button Ins(I think all vim users are well aware of the behavior of this button in nano).

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

Control-sequences are the following (in parentheses - duplicate function keys and, sometimes, Meta-sequences):

    control+G (F1) - call the full tooltip menu;
  • control+X (F2) - exit from the program;
  • control+O (F3) - record of 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) - replacement of text in the current file; first, the replacement text is entered, then, after pressing 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) the line at the cursor position and saving it in the buffer (cutbuffer);
  • control+U - (F10) - inserting the contents of cutbuffer "a into the line at the cursor position; if the latter has not changed, it plays the role of Undo (cancellation), not normally provided; can also be used for unlimited cloning of lines in an arbitrary part of the text - it is enough to move the cursor to the right place after deletion;
  • control+C (F11) - display information about the position of the cursor in the form vr
  • control+T (F12) - spell checker (using an external spelling program, if it is installed and the corresponding option is enabled in the config, see below);
  • control+P- moving the cursor one line up;
  • control+N- moving the cursor one line down;
  • control+F- moving the cursor one character forward;
  • control+B- moving 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) - selection (and buffering) of text, starting from the current cursor position;
  • control+D- deleting a character at the cursor position;
  • control+H- delete 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.

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

Management in nano is carried out from the keyboard using hot keys. At the bottom of the editor is a list of the main keyboard shortcuts. The ^ character means the Ctrl key. For example, ^X means the keyboard shortcut Ctrl+X . Characters are case insensitive, Ctrl+X is equivalent to Ctrl+x .

Let's take a look at how to perform basic operations when working with nano .

Create new file

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

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

After executing the command, the nano editor will start and a new empty file will open in it 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

A message will appear in the status line: File Name to Write: filename(if there is no file name, then you need to enter the file name). Press Enter to save changes.

Save and exit

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

Exit without saving changes

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

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

Copy and paste

Copying and pasting rows

To cut the whole line and put it in the buffer, move the cursor to the required line and press:
Ctrl+K

To insert line from buffer press:
Ctrl+U

Copying and pasting free text

To copy arbitrary text to the clipboard, it must first be selected. To start selection place the cursor at the beginning of the text to be copied and press:
ctrl+6

Move the cursor, the text will be highlighted.

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

or to cut highlighted text, press:
Ctrl+K

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

Text Search

To search for text within 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 search text (relative to the cursor). To move to the next occurrence, press:
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 prompted to enter the text you want to replace with. Type in a string and press Enter .
Next, a request will be issued to replace the found occurrence of the search 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 the found occurrences of the search string.

Hotkeys

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

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 cursor 1 page up (similar to PageUp )
ctrl+vMove cursor 1 page down (similar to PageDown )
ctrl+_Jump to a specific line (you will need to enter the line number).
ctrl+cShow on which line and at what position the cursor is located.
ctrl+wFinding text in a file. You must enter a search string.
Ctrl+\Find and replace text in a file. The search string is entered first, 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 the 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 double-click the Esc key. For example,
Esc+Esc+X
This is equivalent to Ctrl+X .

Only for reading

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

File backup

You can make sure that when a file is changed, a backup copy of it is created. For this, the -B switch is used:

If you modify 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 modification. The name of the backup corresponds to the name of the original file with a tilde ~ at the end.

List of hotkeys for nano. It's incomplete - missing some combinations that I haven't been able to test in action. Either because I don’t do yoga, or the Gnome settings are such that some combinations or certain 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 I have a change of layouts on Alt-Shift in the system. It overlaps those hot keys, which I send to nano.But, it's all lyrics and lack of time, and here is 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 the beginning of the line-Home
  • Go to end of line- end
  • Go to next paragraph-Alt-0
  • Skip 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 parenthesis-Alt-]
  • rewind down- Alt-=
  • rewind up-Alt-
  • Toggle text selection mode- Ctrl-6 (Ctrl-^). One of the few combinations where both 6 and ^ are unambiguous (i.e. you don't need to press shift for ^). To select text after switching on the selection mode, use the cursor keys.
  • Copy current selection- Alt-6 (Alt-^ or Ctrl-k Ctrl-u). Another combination that does not 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 the place after the cursor, then first you 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 the next occurrence (Find Next…)- Alt-w or Ctrl-w Enter
  • Find-replace- Ctrl-\ or Alt-r
  • Search Help- Ctrl-w Ctrl-g
  • Justify the current paragraph (where the cursor is located)- Ctrl-j
  • Justify the entire document-Alt-j

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

I also mentioned syntax highlighting.

Syntax highlighting, like other nano settings, must be specified in the nanorc. For the current user, this file is usually located here ~/.nanorc , and an example with a 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 the nano configuration files, one for each type of backlight. They are enabled by default.

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

,

Gone are the days when the Linux OS seemed like a puzzle to beginners. Most of the currently popular linux-distribution kits have a convenient and intuitive graphical interface, as well as standard programs supplied with them. But there are exceptions. Text editor Nano, for example, does not have GUI, using the command line interface instead. What is it for if there is gedit?

Nano mainly used in servers linux, it is convenient when editing configuration files, and if you decide to master the terminal, it would be highly desirable to master and Nano. There is nothing particularly difficult about this, of all console text editors Nano is the easiest to use, it is enough to know the basic key combinations with which you work with files and text.

Installation and launch

In the latest versions ubuntu editor Nano is present by default, and therefore does not need to be installed.

To run it, you need to open a terminal and run the command nano. If the terminal responds with a message "command not found" or something like that, then it will mean that the editor is not installed. In this case, you will need to install it by running the following command right there in the console:

sudo apt-get install nano

One command is enough to launch the editor - nano. This will create a new file in the current directory. To determine which folder is the current one, you can use the command pwd in the terminal. If you need to open a specific file, specify its name, and if it is located in a folder other than the current one, add the full path to it, as shown in the example of the second command:

nano hello.txt
nano /home/compick/hello.txt

As for the configuration files, when they are opened after nano add a key -w, this is important because it disables line wrapping, which is not used in system files. The editor interface is represented by a console window, in the upper part of which the version of the program and the name of the current file are indicated, as well as the status (whether it was changed or not) . At the bottom of the window are two lines showing the most commonly used key combinations. A status bar is also displayed at the bottom, which displays various messages and fields for commands, for example, the F6 button starts a text search.

Basic principles of working with the Nano editor

The hotkey labels can be somewhat confusing for a novice user, but this is only at first. Symbol (^) means the Ctrl key, and (M) Meta- Alt or Esc key. All this can be found in the built-in help. nano called by pressing ^+G, i.e. Ctrl + G .

Arrow keys can be used to move through the content, Ctrl + O followed by typing to save changes. Y for confirmation and N to cancel. At the time of saving, the editor asks for a file name, which must be entered in a special field at the bottom of the window.

To cancel the changes made to a file that has not yet been saved, you need to press Ctrl + C . For cutting out entire lines and paragraphs in nano the combination Ctrl + K is responsible, for their insertion - Ctrl + U. To select certain elements of the line, use Ctrl + 6 or Alt + A , to call the search line - Ctrl + W or F6 . The Ctrl + A and Ctrl + E commands allow you to quickly move to the beginning and end of a line, the Ctrl + Y and Ctrl + V commands scroll the contents of a text file up and down. Finally, Ctrl + X is used to exit the editor to the terminal.

But this is only part of the functionality. nano. The editor supports counting words, lines and symbols, working with several files at once, creating backup copies of documents being edited, using the "only for reading", enable partial mouse support, describe syntax for highlighting, change tab width, enable file autosave on exit, and more. Additional parameters are set mainly when the editor is launched, but some, for example, the key responsible for backup -b can be used when saving a file.

From additional features nano It is worth noting the setting of the appearance of the console window.

If you notice an error, select a piece of text and press Ctrl + Enter
SHARE: