This article explains how to create and edit a text file using the two most popular Linux editors. Virtually all Linux distributions integrate the "Nano" editor, an intuitive and very simple to use text editor. If you don't like using the "Nano" editor (or if you haven't installed it) you can also use the "Vi" editor (or "Vim" depending on the distribution you are using). The "Vi" and "Vim" text editors are a little more complex to use, as they have many commands and two modes of use.
Steps
Method 1 of 2: Nano Editor
Step 1. Press the key combination Control + Alt + T to open a "Terminal" window
This hotkey combination opens a "Terminal" window and is supported by almost all Linux distributions.
- Alternatively, you can click on the app icon Terminal listed in the list of installed applications. If you are using GNOME, you can access the "Dash" menu and search using the terminal keyword.
- "Nano" is a very simple to use text editor, pre-installed on all Ubuntu-based Linux distributions. If you don't have the "Nano" program available, you can install it manually by running the command sudo apt install nano (on Ubuntu and Debian) or sudo yum install nano (on CentOS and Fedora).
- If you have used the "Pico" editor before, you will find that "Nano" is basically identical and, unlike the "Vi" and "Vim" programs, you will not have to switch between command entry and text entry mode during use.
Step 2. Navigate to the directory where you want to create the file
Most likely you will want to store it inside your home directory or in a subfolder which is the default working directory that is used when opening the "Terminal" window. If you want to create the file inside an existing subfolder, you can use the command CD to access it.
- To view the list of all the folders present in the current working directory (your "home" directory), type the command ls and press the key Enter.
- To access a directory present in the "home" folder, type the command cd name_directory and press the key Enter (replace the directory_name parameter with the name of the directory you want to access).
- If you need to create a new directory, execute the makedir command directory_name (replace the parameter directory_name with the name you want to assign to the new directory. After creating the folder in question, use the command cd directory_name to access it.
- It should be noted that it is possible to create files outside of your "home" directory, but to do so you need to have access rights as "root".
Step 3. Type the command nano filename and press the Enter key
Replace the filename parameter with the name you want to give the new text file. This will create a new text file with the given name which will be opened automatically.
- For example, if you want to create a file named "test_file", type the command nano test_file and press the key Enter.
- It may be useful to add the extension ".txt" to the end of the file name so that you know that it is a text file.
- If there is a file with the same name in the current working directory, it will be opened without creating a new one.
Step 4. Find the command list visible at the bottom of the window
The commands you can use while creating the content of your text file will appear at the bottom of the editor window. To see a larger list of commands, simply zoom in on the window by dragging one of the corners with the mouse.
-
The commands are characterized by the initial character (^) or the letter "M". The first character represents the key Control, while the letter "M" represents the key Alt.
- For example, the ^ U command represents the "Paste" command. To paste previously copied content into the file you will need to press the key combination Ctrl + U.
- The M-U command allows you to undo the last action performed, so to use this command press the key combination Alt + U.
- To view the list of all commands in the "Nano" editor, press the key combination Ctrl + G.
Step 5. Insert the text into the new file
If you need to move the text cursor within the file, use the arrow keys on the keyboard.
You can use the mouse to select a piece of text you want to copy and paste. To copy the selected text, press the key combination Alt + 6, then use the directional arrows to move the text cursor to the place in the file where you want to paste the copied text and press the key combination Ctrl + U.
Step 6. Press the key combination Ctrl + O to save the file
Since the file already has a name, you won't be prompted to give it one. However, if you started typing text without first creating a file (simply by running the nano command from the "Terminal" window), you will be prompted to enter the file name and press the key Enter.
Resist the urge to hit the key combination Ctrl + S to save the changes made to the file, since this command only serves to lock the session of the "Terminal" window.
Step 7. Press the key combination Ctrl + X to close the "Nano" editor
This will redirect you to the "Terminal" window.
If you need to reopen the file you are working on, you will simply need to run the nano filename command as you did earlier
Method 2 of 2: Vi or Vim editor
Step 1. Press the key combination Control + Alt + T to open a "Terminal" window
This hotkey combination opens a "Terminal" window and is supported by almost all Linux distributions.
- Alternatively, you can click on the app icon Terminal listed in the list of installed applications. If you are using GNOME, you can access the "Dash" menu and search using the terminal keyword.
- "Vi" is one of the older text editors and is Unix based. The "Vim" program whose name derives from "Vi iMproved" is a version of the "Vi" editor enriched with new features. Using most modern versions of Linux, when running the command you from the command prompt, the editor starts automatically Vim. The basic commands are identical in both editors.
- The "Vi" program offers a longer learning curve than the "Nano" editor, but with practice it will be easy to use.
Step 2. Navigate to the directory where you want to create the file
Most likely you will want to store it in your "home" directory or in a subfolder which is the default working directory that is used when opening the "Terminal" window. If you want to create the file inside an existing subfolder, you can use the command CD to access it.
- To view the list of all the folders present in the current working directory (your "home" directory), type the command ls and press the key Enter.
- To access a directory in the "home" folder, type the command cd directory_name and press the key Enter (replace the directory_name parameter with the name of the directory you want to access).
- If you need to create a new directory, run the makedir command directory_name (replace the directory_name parameter with the name you want to assign to the new directory). After creating this folder, use the cd directory_name command to access it.
- It should be noted that it is possible to create files outside of your "home" directory, but to do so you need to have access rights as "root".
Step 3. Type the command vi filename and press the Enter key
Alternatively, you can use the vim filename command to make sure the file will open within the "Vim" editor, rather than the "Vi" editor. The "vi" part of the command will select the "Vim" editor as the program to use. Replace the filename parameter with the name you want to give the new file.
- For example, to create a file named "example.txt", you will need to run the vi example.txt command.
- If there is a file with the same name in the current working directory, it will be opened without creating a new one.
Step 4. Press the i button
When you start the "Vi" or "Vim" editor, the program will start in the operating mode called "Command". By pressing the key THE the text insertion mode will be activated, which is necessary in order to insert the relative textual content in the file.
After pressing the I key, the text should appear at the bottom of the window - INSERT -.
Step 5. Enter the contents of the file
When text entry mode is on, you can create your document simply by typing its content just like you would with any other text editor. To create a new line of text, simply press the key Enter.
Step 6. Press the Esc key
This will reactivate the command input mode. In this mode of use it is possible to perform operations such as copy and paste portions of the text, save the file and close the editor. When insert mode is active the wording "INSERT" will no longer be visible in the lower part of the window.
- When the command input mode is active (both in "Vi" and in "Vim") it is possible to use the arrow keys on the keyboard to move the text cursor within the document. The "Vim" editor allows you to use the arrow keys on the keyboard to move the text cursor even in "Insert" mode.
- You can switch to text entry mode at any time by pressing the key the.
Step 7. Type the command: w and press the Enter key
All commands of both editors ("Vi / Vim") start with the ":" symbol. The command: w allows you to save the file (to make it mnemonic think that the letter "w" refers to the English word "write", that is, to write data to disk).
- If you have created an empty file with no name (or if you need to save the new changes as a new document), use the command: w filename replacing the filename parameter with the name you want to give the new file.
- To get support and learn how to use the commands of the "Vi / Vim" editors, type the command: help and press the key Enter (perform this step in command entry mode).
Step 8. Type the command: q and press the Enter key to close the editor
This will close the file you were working on and the command prompt will appear.
- To open a file again, execute the command vi filename or vim filename.
- You can save your changes and close the editor by running the command: wq when command entry mode is active.
Advice
- Remember to save the file before closing the editor, as you will not be notified of any unsaved changes.
- You can execute the man vi or man nano commands in the "Terminal" window to display the user manual of the text editor you have chosen to use.
- One of the most useful features of the Vim editor that the editor lacks is the color coding of the syntax which is crucial when programming. It also features built-in spelling check and the ability to navigate text using the directional arrows when "Insert" mode is active.