- How to open, create, edit, and view a file in Linux
- GUI text editors
- NEdit
- Geany
- Gedit
- Terminal-based text editors
- emacs
- Redirecting command output into a text file
- Creating an empty file with the touch command
- Redirecting text into a file
- Redirecting to the end of a file
- How to edit text files from the command line
- Using the Nano editor
- Using the Vim editor
- More Information
- Using Nano Text Editor Commands in Linux
- Why Edit Files in Linux Using Nano?
- Installing the Nano Text Editor in Linux
- Nano Command Keys
- Using Nano to Create And Open Files in Linux
- Create a New File using Nano
- Open an Existing File Using Nano
- Open Configuration Files Using Nano
- Edit Files Using Nano Text Editor in Linux
- Cut and Paste Lines of Text Using Nano
- Valid Shortcuts in Nano Text Editor
- Search Text Using Nano
How to open, create, edit, and view a file in Linux
One thing GNU/Linux does as well as any other operating system is give you the tools you need to create and edit text files. Ask ten Linux users to name their favorite text editor, and you might get ten different answers. On this page, we cover a few of the many text editors available for Linux.
GUI text editors
This section discusses text editing applications for the Linux windowing system, X Windows, more commonly known as X11 or X.
If you are coming from Microsoft Windows, you are no doubt familiar with the classic Windows text editor, Notepad. Linux offers many similar programs, including NEdit, gedit, and geany. Each of these programs are free software, and they each provide roughly the same functionality. It’s up to you to decide which one feels best and has the best interface for you. All three of these programs support syntax highlighting, which helps with editing source code or documents written in a markup language such as HTML or CSS.
NEdit
NEdit, which is short for the Nirvana Editor, is a straightforward text editor that is very similar to Notepad. It uses a Motif-style interface.
The NEdit homepage is located at https://sourceforge.net/projects/nedit/. If you are on a Debian or Ubuntu system, you can install NEdit with the following command:
For more information, see our NEdit information page.
Geany
Geany is a text editor that is a lot like Notepad++ for Windows. It provides a tabbed interface for working with multiple open files at once and has nifty features like displaying line numbers in the margin. It uses the GTK+ interface toolkit.
The Geany homepage is located at http://www.geany.org/. On Debian and Ubuntu systems, you can install Geany by running the command:
Gedit
Gedit is the default text editor of the GNOME desktop environment. It’s a great, text editor that can be used on about any Linux system.
The Gedit homepage is located at https://wiki.gnome.org/Apps/Gedit. On Debian and Ubuntu systems, Gedit can be installed by running the following command:
Terminal-based text editors
If you are working from the Linux command line interface and you need a text editor, you have many options. Here are some of the most popular:
pico started out as the editor built into the text-based e-mail program pine, and it was eventually packaged as a stand-alone program for editing text files. («pico» is a scientific prefix for very small things.)
The modern version of pine is called alpine, but pico is still called pico. You can find more information about how to use it in our pico command documentation.
On Debian and Ubuntu Linux systems, you can install pico using the command:
nano is the GNU version of pico and is essentially the same program under a different name.
On Debian and Ubuntu Linux systems, nano can be installed with the command:
vim, which stands for «vi improved,» is a text editor used by millions of computing professionals all over the world. Its controls are a little confusing at first, but once you get the hang of them, vim makes executing complex editing tasks fast and easy. For more information, see our in-depth vim guide.
On Debian and Ubuntu Linux systems, vim can be installed using the command:
emacs
emacs is a complex, highly customizable text editor with a built-in interpreter for the Lisp programming language. It is used religiously by some computer programmers, especially those who write computer programs in Lisp dialects such as Scheme. For more information, see our emacs information page.
On Debian and Ubuntu Linux systems, emacs can be installed using the command:
Redirecting command output into a text file
When at the Linux command line, you sometimes want to create or make changes to a text file without actually running a text editor. Here are some commands you might find useful.
Creating an empty file with the touch command
To create an empty file, it’s common to use the command touch. The touch command updates the atime and mtime attributes of a file as if the contents of the file had been changed — without actually changing anything. If you touch a file that doesn’t exist, the system creates the file without putting any data inside.
For instance, the command:
The above command creates a new, empty file called myfile.txt if that file does not already exist.
Redirecting text into a file
Sometimes you need to stick the output of a command into a file. To accomplish this quickly and easily, you can use the > symbol to redirect the output to a file.
For instance, the echo command is used to «echo» text as output. By default, this goes to the standard output — the screen. So the command:
The above command prints that text on your screen and return you to the command prompt. However, you can use > to redirect this output to a file. For instance:
The above command puts the text «Example text» into the file myfile.txt. If myfile.txt does not exist, it is created. If it already exists, its contents will be overwritten, destroying the previous contents and replacing them.
Be careful when redirecting output to a file using >. It will overwrite the previous contents of the file if it already exists. There is no undo for this operation, so make sure you want to completely replace the file’s contents before you run the command.
Here’s an example using another command:
The above command executes ls with the -l option, which gives a detailed list of files in the current directory. The > operator redirects the output to the file directory.txt, instead of printing it to the screen. If directory.txt does not exist, it is created first. If it already exists, its contents will be replaced.
Redirecting to the end of a file
The redirect operator >> is similar to >, but instead of overwriting the file contents, it appends the new data to the end of the file. For instance, the command:
Источник
How to edit text files from the command line
Although you can edit text files in cPanel (if your account includes cPanel), it is often quicker and easier to do so from the command line. This article describes how to use the Nano and Vim editors to edit text files from the command line.
The Nano editor is probably easier for beginners to use initially. On the other hand, the Vim editor is in more widespread use, and has a long list of features. Try both editors, and use whichever one you feel more comfortable with.
Using the Nano editor
Editing files with the Nano text editor is easy. To open a file in Nano, type the following command at the command line:
Replace filename with the name of the file that you want to edit.
To edit the file, just start typing your changes. To navigate around the file, you can use the arrow keys on your keyboard. If the file’s contents are too long to fit on the screen, you can press Ctrl+V to move forward a page, and Ctrl+Y to move back a page.
When you are ready to save your changes, press Ctrl+O, verify the filename, and then press ENTER. To exit Nano, press Ctrl+X.
You can access Nano’s online help at any time by pressing Ctrl-G.
Using the Vim editor
To open a file for editing in Vim, type the following command at the command line:
Editing files with Vim is not as intuitive as with Nano. You can’t just start typing your changes, because Vim starts in normal mode. In normal mode, anything you type on the keyboard is interpreted as a potential command, not changes to the text.
To make changes to the text, you must enter insert mode. To do this, type i . Note that the status line at the bottom of the screen changes to —INSERT— . You can now make changes to the file. To navigate around the file while you are in insert mode, use the arrow keys and Page Up/Page Down keys.
To return to normal mode, press ESC. Note that the —INSERT— status line at the bottom of the screen goes blank. Now you can type commands to save your changes, search for text, and so on.
To write your changes without exiting Vim, type :w and then press ENTER. To exit Vim, type :q and then press ENTER. To write your changes and exit Vim at the same time, type :wq and then press ENTER.
More Information
This article is a very basic introduction to using the Nano and Vim text editors. Both of these editors, and Vim in particular, have many more features and customizations available:
Источник
Using Nano Text Editor Commands in Linux
GNU nano is a popular command-line text editor that is included in most Linux distributions. The interface is comparable to GUI-based text editors, which makes nano a popular choice for those who find vi or emacs commands non-intuitive. This guide shows you how to use Nano Text Editor Commands in Linux.
Why Edit Files in Linux Using Nano?
Editing files in Linux using Nano is a popular option. Compared to other editors such as Vim, Nano’s GUI makes it very easy to edit, save and interact with the files. On other editors such as Vim, you have to change to edit mode to input text.
Installing the Nano Text Editor in Linux
Nano is included with many Linux distributions by default, but some users may need to install it through the package management tool of the distribution.
Debian/Ubuntu:
CentOS/Fedora:
Nano Command Keys
When using nano, control characters (CTRL) are represented by a carat (^) . For example, if you wish to cut a line of text, you would use the CTRL key followed by the K key. This sequence of commands is represented as ^K in nano. Some commands use the Alt key in order to function, which is represented by the letter M.
A command represented as M-R in nano is performed by pressing the Alt key followed by the R key. Mac users may need to use the Escape (Esc) key instead of the Alt key to use these commands.
Using Nano to Create And Open Files in Linux
Create a New File using Nano
Typing nano without any arguments opens a blank file for editing:
A blank new file in the terminal similar to the following appears:
You can make changes to the file. You may exit the file by pressing Ctrl + X. It prompts you to save the file and name it before you exit.
When prompted, select press Y and then write the name of the file.
After you enter a filename, press the Enter key to save the file. In this guide, the file is saved as a tutorial.txt with the following content:
Open an Existing File Using Nano
To open a file, provide the filename as an argument in the following command:
The editor opens an existing file in the current working directory.
To practice, use the file in the current working directory that was created earlier: tutorial.txt .
If you wish to open a file in a different directory, you can provide a path instead. To do that, provide the path to the file relative to your current working directory. For instance, if the path to tutorial.txt is /nano-text-editor-tutorial/tutorial.txt , you could use the following command:
If tutorial.txt does not exist in /nano-text-editor-tutorial/ , nano creates a file named tutorial.txt in the directory.
You can also open files at a specific line or column number:
For example, to open the tutorial.txt file at Line 2 :
To open a file as read-only:
Open Configuration Files Using Nano
When editing files used to configure applications or system utilities, start nano with the -w flag:
This flag prevents nano from wrapping lines that are too long to fit on the screen. Wrapping lines can create problems if configuration directives are saved across multiple lines.
Edit Files Using Nano Text Editor in Linux
Type nano to enter nano text editor. Use the arrow keys to move the cursor. A partial menu of available commands appears at the bottom of the terminal window.
Cut and Paste Lines of Text Using Nano
To cut a line of text, use ^K . To paste, move the cursor where you want the text to be placed and use ^U . To cut multiple lines, use a series of ^K commands until all lines you wish to cut have been removed. When you paste them back with ^U , the lines are pasted at once.
Valid Shortcuts in Nano Text Editor
To open up a list of commands in Nano, for example, in the tutorial.txt file, use Ctrl+G.
Nano help menu appears and displays the following commands:
Below is an abbreviated list of commands:
Keyboard Shortcut | Alternative Command Key | Description |
---|---|---|
^G | F1 | Display this help text |
^X | F2 | Close the current file buffer / Exit from nano |
^O | F3 | Write the current file to disk |
^R | F5 | Insert another file into the current one |
^W | F6 | Search forward for a string or a regular expression |
^|M-R | Replace a string or a regular expression | |
^K | F9 | Cut the current line and store it in the cut buffer |
^U | F10 | Uncut from the cut buffer into the current line |
^J | F4 | Justify the current paragraph |
^T | F12 | Invoke the spell checker, if available |
^C | F11 | Display the position of the cursor |
^_ | M-G | Go to line and column number |
M-U | Undo the last operation | |
M-E | Redo the last undone operation | |
M-A | ^6 | Mark text starting from the cursor position |
M-6 | M-^ | Copy the current line and store it in the cut buffer |
M-] | Go to the matching bracket | |
M-W | F16 | Repeat the last search |
M-в–І | Search next occurrence backward | |
M-в–ј | Search next occurrence forward | |
^B | в—Ђ | Go back one character |
^F | в–¶ | Go forward one character |
^в—Ђ | M-Space | Go back one word |
^в–¶ | ^Space | Go forward one word |
^A | Home | Go to beginning of current line |
^E | End | Go to end of current line |
Search Text Using Nano
To search for a specific text in the document, use ^W The editor opens a search prompt and a submenu of search-specific commands.
Источник