Linux nano. Commands Nano is the simplest console text editor for Linux. Editing files in Nano

In this tutorial, you will learn how to install and use the Linux text editor - Nano. While Vim and Emacs can be tricky for new Linux users, Nano is fairly easy to learn. That is why it is one of the most popular text editors for Unix and other command line operating systems.

Before you start this tutorial, you need the following:

  • Any package installer, for example apt-get

Step 1 - Installing the Nano Text Editor

It all depends on what OS you have installed, some of them already have a pre-installed editor Nano... You can skip this step if the editor Nanoyou already have it installed. Click on here to go to Step 2of this manual.

To install a text editor Nano, you must have a package installer on your system, for example, a command to install Nano on Ubuntu will:

Sudo apt-get install nano

IMPORTANT! Part Sudo means running this command as root user, if you are not currently under the root user account you will have to enter the corresponding password.

This completes the installation, now you should be able to use a text editor Nano... You can check this by typing this command:

Nano test.txt

It should open a new text editor window, and if you make changes to this file, they will be saved in a file under the name test.txt... To exit the editor, press the keyboard shortcut CTRL + X.

Step 2 - Using a Linux Text Editor - Nano

The main command to open the editor is:

Nano filename


IMPORTANT! Instead filename you can enter the file name you need or the name of an existing one. Please note that if you want to edit an existing file, you must be in the same directory as that file. Another way is to specify the full path to the file.

After running the command, you will be taken to the editor window, where you can freely change the text using the editor Nano... Use the arrows on your keyboard to move the cursor over the text.

At the bottom of this window, you can see shortcuts for use with the editor Nano... Symbol ^ means you need to press CTRL + [Button] (CMD + [Button] for Mac users) to use the selected commands:

Command Explanation
CTRL + A Move to the beginning of the line.
CTRL + E Move to the end of the line.
CTRL + Y Scroll down the page.
CTRL + V Scroll up the page.
CTRL + G This command will bring up a windowHelp with all the information you need about the commands you can use with the editorNano.
CTRL + O The command to save. After use, it will ask for confirmation to change or save the file name, and after pressingEnter will save your file.
CTRL + W One of the most useful commands. Used to find a phrase in your text. It works like a familiar teamCTRL + F on other platforms. To search for the same phrase again, clickALT + W.
CTRL + K Cut the entire selected line to the "clipboard".
CTRL + U Paste text from the “clipboard” into the selected line.
CTRL + J Formats the current paragraph.
CTRL + C Shows the current position of the cursor in the text (line / column / letter).
CTRL + X Exits the editorNano. In case changes have been made, it will ask for permission to save the changes.
CTRL + R Opens a file using the "Read File" command. Inserts a file from disk at the current cursor position.
CTRL + \\ Replace string or persistent expression.
CTRL + T Enable spell checker if available.
CTRL + _ Move to a specific line and column number.
ALT + A Select text. First, move your cursor to where the beginning of the portion of the text you want to select is. Next pressALT + A... Now move the cursor with the arrow keys on the keyboard to the right to select the text. You can use this command in combination withCTRL + K to cut a certain part of the text to the “clipboard”.

These are the basic and most useful commands for working in the editor. Nano... Remember to use the command CTRL + G to display the window Help.

IMPORTANT! If you are a Mac user use instead CTRLCMD.

Conclusion

In this tutorial, you learned how to install and use the Nano text editor. For tutorials about bash, SSH and VPS visit our tutorials page.

List of hotkeys for nano. It is incomplete - some combinations are missing that I was unable to test in action. Either because I don’t do yoga, or the Gnome 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 (ie Alt- (on a regular keyboard it 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, but the list

  • Home \u003d Ctrl-a
  • End \u003d Ctrl-e
  • PgUp \u003d Ctrl-y
  • PgDn \u003d Ctrl-v
  • Cursor keys \u003d Ctrl- (f, b, n, p)
  • Tab \u003d Ctrl-i
  • Backspace \u003d Ctrl-h
  • Delete \u003d Ctrl-d
  • Return \u003d Ctrl-m
  • Help - Ctrl-g
  • Exit - Ctrl-x
  • Save file - Ctrl-o
  • Toggle syntax highlighting - Alt-y
  • Move to next word - Ctrl-Space
  • Move to the previous word - Alt-Space
  • Move to start of line - Home
  • Move to end of line - End
  • Go to next paragraph - Alt-0
  • Go to the previous paragraph - Alt-9
  • Next page - PgDn
  • Previous page - PgUp
  • Go to line No. - Ctrl-_
  • Move to the end of the file - Alt- /
  • Go to the beginning of the file - Alt- \\
  • Move to matching parenthesis - Alt-]
  • Rewind down - Alt- \u003d
  • 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 turning 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 the current selection - Ctrl-k
  • Paste - Ctrl-u
  • Paste file contents into a new buffer - Ctrl-r<имя файла>... If you need to insert the contents of the file into the current buffer at 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 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 the nano mode in which you can edit multiple files at the same time. Switching between several buffers is performed using the Alt- and Alt- combinations. ...

I also mentioned syntax highlighting.

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

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

If you need to change colors and other highlighting settings, you can also see ready-made configurations with syntax highlighting. You can change files 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.

The nano editor may well play the role of a shock absorber for a novice user. Yes, it's not emacs, or even joe. But he copes with the task of configuration successfully. And in the development and handling - as simple as a rake. It is no coincidence that many Linux distributions offer it as a system-wide one. In Gentoo Linux, where the need for manual editing of configuration files arises quite often during installation, this is simply the only editor available during the installation phase of the system. In CRUX and Archlinux, however, during installation, it sits next to vi - imitating the original one, and therefore may also be preferable.

The nano editor is launched on the command line with the following syntax nano / path_to_file / file. fig. 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 with the corresponding keyboard keys, as in other editors. The rest of the nano functions are controlled using control sequences, the list of which is taken from http://posix.ru/apps/nano_editor/ and is given below.

There are two types of control sequences in nano - control sequences themselves, Control+ letter, and meta-sequences, Meta+ letter. Through the former, text editing and file operations are carried out. Control sequences are partially duplicated by function keys F1-F16 (call F13-F16 - by combining Shift+F1-F4). Meta sequences are used to change editor settings (the same result is achieved with command line options).

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

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

The 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) - write 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) - replace text in the current file; the replacement text is entered first, 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) \u003d deleting (Cut, cut) the line at the cursor position, saving it to 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 (undo), 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 the desired place;
  • Control+C (F11) - output information about the position of the cursor in the form of vr
  • Control+T (F12) - spell check (using 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 down one line;
  • 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) - selection (and placing in the buffer) of the 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 tabulation character;
  • Control+J (F4) autocomplete the current paragraph;
  • Control+M inserting a line feed 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 constant cursor position;
  • Meta+I - enable / disable auto-indentation;
  • Meta+Z - enable / disable suspension;
  • Meta+X - enable / disable display of the prompt zone;
  • Meta+P - enable / disable pico editor emulation mode;
  • Meta+W - enable / disable hyphenation mode;
  • Meta+M - enable / disable mouse support (only when building with gpm support;
  • Meta+K - enable / disable 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. Having performed some manipulations in it, you can slightly expand the functionality of the editor, in particular, provide syntax highlighting. ,

The days when Linux was a puzzle for beginners are long gone. Most popular today Linux -distributions have a convenient and intuitive graphical interface, as well as the standard programs supplied with them. But there are also 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 used mainly in server rooms Linux, it is convenient when editing configuration files and, if you have decided to master the terminal, along the way it will be highly desirable to master and Nano... There is nothing particularly complicated in this, of all console text editors Nano is the easiest to use, you just need to know the basic key combinations used to work with files and text.

Installation and launch

In the latest versions Ubuntu editor Nano is present by default and therefore does not need installation.

To start 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 in the console right there:

sudo apt-get install nano

To start the editor, one command is enough - 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.tхt
nano /home/kompick/hello.tхt

As for the config files, when opening them after nano should 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, at the top of which the program version and the name of the current file, as well as the status (whether it was changed or not) ... At the bottom of the window there are two lines showing the most frequently 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

Hotkey designations may confuse a novice user a little, but this is only at first. Symbol (^) means Ctrl key and (M) Meta - Alt or Esc key. All this can be found in the built-in help nanoby pressing ^ + G, that is, Ctrl + G.

You can use the arrow keys to move through the content, to save changes - Ctrl + O followed by entering Y to confirm 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 undo the changes made to the still unsaved file, press Ctrl + C. For cutting entire lines and paragraphs into nano the combination Ctrl + K is responsible, for their insertion - Ctrl + U. To select certain line items, use Ctrl + 6 or Alt + A, to call the search line - Ctrl + W or F6. Commands Ctrl + A and Ctrl + E allow you to quickly move to the beginning and end of a line, commands Ctrl + Y and Ctrl + V - scroll the contents of a text file up and down. Finally, Ctrl + X is used to exit the editor to the terminal.

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

Additional features nano it is worth noting the customization of the appearance of the console window.

Although most of the actions in the Linux operating system can be done through a graphical interface, if you want more control over the process and more 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. Different editors can be used to work with configuration files. Newbies tend to use Gedit most often, more experienced users who are already accustomed to the terminal prefer the nano editor, and many professionals want to use only Vim.

We have already covered how to use Vim in a previous article, it is very flexible, but this flexibility came at the price 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. It is a very simple editor and any beginner can handle it. But, in turn, this will be an excellent step towards mastering the terminal.

The Nano text editor is part of the GNU suite of programs and is the default console editor for many Linux distributions. 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 that you could use to write your messages. Many newbies liked Pico for its simplicity and intuitive interface. But Linux distributions, including Debian, could not include proprietary binaries for Pine and Pico in their repositories.

With all this in mind, it was decided to create an editor TIP or Tip Isn "t Pico. But then due to a name conflict with another utility, the editor was renamed to nano.

The main advantage of nano is its ease of use. It's as easy to work with as OpenOffice or Gedit. Therefore, working with the program is very easy to learn. Now let's move on to 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. For this, the following syntax is used:

$ nano options / address / file / file

Using command line options is not necessary just to specify the filename, but in some cases the options can be very useful. Let's consider them in more detail:

  • + number - specify the line in the file with which you want to start editing;
  • -B - create a backup copy for the file when saving;
  • -FROM - folder for backup;
  • -D - display text in bold;
  • -E - convert tabs to spaces;
  • -F - enable support for editing multiple files;
  • -H - save the history of search and replace;
  • -I - do not read configuration from nanorc files;
  • -P - remember and restore the cursor position during the last edit;
  • -T - the size of tabs 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, of course, there are actually 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 a file, just pass it in parameters to the nano command. If no such file exists, a new one will be created. For instance:

The main window of the program looks very simple, the top line contains the version of the editor and the 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 basic 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 a few times of practice, everything will work out. Moreover, all the essentials are 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, press Ctrl + G:

When you're done with the editor and save your changes, click Ctrl + X to exit, if the file was not saved, the program will offer 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 started nano with no parameters, 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 the 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 the standard regular expressions of the perl syntax:

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

We have covered the basic keyboard shortcuts that are used to control editor functions. 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 the keyboard shortcuts for quickly navigating through text in the nano editor. Of course, you can always use the already familiar keys. PageUP, PageDown, Home, End and arrow keys, but this is not very convenient considering that the nano has its own shortcuts. While they are not as practical as Vim, 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 at a time, 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 combinations for moving through pages, Ctrl + V moves one page down and Ctrl + Y - one page up.

But that's not all. You can navigate words with Ctrl + Space and Alt + Spaceand also press Ctrl + _ and enter the number of the required 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 the text with the mouse, but this is only if you are working in the graphical interface. 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 copying tools.

To select 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 points, 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 insert the cut text move the cursor to the desired position and press Ctrl + U.

To cut an entire line of text, just press Ctrl + Kthis is sometimes very handy when editing configuration files.

Multiple files in Nano

Not everyone knows, but the nano linux text editor supports opening multiple files. You cannot 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 at startup:

nano file1 file2

And then use keyboard shortcuts to move between them Alt +\u003e and Alt +< ... To close the unnecessary file, press Ctrl + X... Not the entire program will close, but only the active buffer with the file.

conclusions

In this article, we have covered how to use the nano linux editor. Despite the fact that it is very simple and is intended primarily for Linux newbies, there are a huge number of features that make the program a very powerful editor. If you have any questions, ask in the comments!

To finish, a little comparison of nano and vim: