When working in the Ubuntu terminal, you will frequently need to edit configuration files or write short scripts. However, because the terminal has no graphical user interface (GUI), you cannot simply double-click a file and open it in a traditional word processor like Notepad or Microsoft Word.
You must use a command-line text editor. While advanced users often debate the merits of complex editors like Vim or Emacs, the best choice for beginners and everyday tasks is nano. It is simple, intuitive, and comes pre-installed on almost every Ubuntu system.
How to Open a File in nano
To start using nano, you simply type the command followed by the name of the file you want to edit.
- Open your Ubuntu terminal.
- Type
nano filename.txt(replace “filename.txt” with the actual name of your file). - Press Enter.
If the file already exists, nano will open it, displaying its contents on the screen. If the file does not exist, nano will create a brand new, empty file with that name.
Note: If you are editing a system configuration file (usually located in the /etc/ directory), you must run the command with administrator privileges by typing sudo nano /etc/filename.conf.
How to Navigate and Edit Text
Unlike complex terminal editors, nano behaves exactly as you would expect a text editor to behave.
- Typing: Simply start typing on your keyboard. The text will appear at the cursor’s location.
- Deleting: Use the Backspace or Delete keys.
- Navigating: You cannot use your mouse to click on a specific sentence. You must use the Arrow Keys on your keyboard to move the cursor up, down, left, and right through the text.
How to Use nano Commands
Look at the bottom of the terminal window. You will see a small menu listing the most important commands.
In this menu, the caret symbol (^) represents the Ctrl key on your keyboard. For example, ^O Write Out means you must press Ctrl + O to trigger that action.
Here are the essential commands you need to know:
- Search for text: Press Ctrl + W (“Where Is”). Type the word you are looking for and press Enter. The cursor will immediately jump to the first matching result.
- Cut a line: Move the cursor to a line of text and press Ctrl + K (“Cut Text”). The entire line will disappear.
- Paste a line: Move the cursor to a blank space and press Ctrl + U (“Uncut Text”) to paste the line you just cut.
How to Save and Exit
This is where most beginners get stuck, as there is no traditional “File > Save” button.
- When you are finished editing, press Ctrl + O (“Write Out”) to save your changes.
- nano will ask you to confirm the file name at the bottom of the screen. Simply press Enter to confirm. (You will see a message saying “Wrote X lines”).
- Now that the file is saved, press Ctrl + X (“Exit”) to close nano and return to the standard terminal command prompt.
If you press Ctrl + X without saving first, nano will warn you and ask if you want to save the modified buffer. Press Y for Yes, or N for No.