How to Edit Text Files Using the Linux nano Text Editor

When you are managing a headless Linux server, you do not have access to graphical text editors like VS Code or Notepad. You must edit configuration files directly within the terminal interface. While powerful editors like vi or vim are ubiquitous, they have an incredibly steep learning curve that often leaves beginners trapped in the editor, unable to figure out how to simply save and quit. For quick, straightforward text editing, the nano editor is the best tool for the job. It is simple, intuitive, and clearly displays all of its keyboard shortcuts directly on the screen at all times.

Opening a File with Nano

Unlike other terminal editors that open in a separate command mode, nano drops you straight into the file, ready to type immediately.

  1. Open your Linux terminal.
  2. To open an existing file (or create a new one), type the nano command followed by the filename. Because you are often editing system configurations, you usually need sudo privileges: sudo nano /etc/hostname
  3. Press Enter.

The terminal will clear, and your file’s text will appear. Your blinking cursor will be at the very top left. You can instantly start typing to add text, or use your keyboard’s arrow keys to navigate around the document.

Understanding the Keyboard Shortcuts

Look at the very bottom of the nano screen. You will see a persistent legend displaying two rows of commands, such as ^O Write Out and ^X Exit. The caret symbol (^) represents the Ctrl (Control) key on your keyboard.

  • Search for text: Press Ctrl + W (Where Is). A prompt will appear at the bottom. Type your search term and press Enter.
  • Cut text: Press Ctrl + K (Cut Text) to instantly delete the entire line your cursor is currently resting on and save it to the clipboard.
  • Paste text: Press Ctrl + U (Uncut Text) to paste the line you just cut.

How to Save and Exit Nano

The most common stumbling block for beginners is figuring out how to save their changes and return to the bash prompt.

  1. When you are finished editing, press Ctrl + O (the letter O, not the number zero). This triggers the “Write Out” command (save).
  2. Look at the bottom of the screen. Nano will ask you to confirm the filename: File Name to Write: /etc/hostname.
  3. Press Enter to confirm and save the file. (The editor will tell you how many lines it wrote).
  4. To quit the editor, press Ctrl + X.

The nano interface will vanish, and you will immediately be returned to your standard Linux command prompt.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.