How to Edit a File in Linux from the Terminal (nano, Vim, sed)

An editor buffer feeding a file on disk through the write command

Learning how to edit a file in Linux is mostly a question of which key commits the change you just typed into the terminal. Nano, Vim, and Emacs all hold your text in a buffer while you work, so a screen that looks finished can still be a file that has not changed on disk. Each of those three editors needs its own write-and-quit key, and two shorter commands change a file without opening an editor at all.

Two Ways to Change a File on Linux

Any text file on a Linux system is an ordinary file you can open and change, whether it holds a hosts entry, an Nginx server block, or a shell script. Typing the new text is easy, and the key that writes your buffer to the disk is what decides whether the work survives.

Two routes reach that disk, and the one you choose decides how much control you keep over the file while you work on it.

RouteReach for it whenHow you finish
nanoYou want the fastest interactive edit and no mode to learnCtrl+O writes out, then Ctrl+X exits
VimThe file is already open in Vim, or the machine only has VimEsc, then a colon, then wq, then Enter
EmacsYou already use Emacs for everything elseCtrl+X Ctrl+S saves, Ctrl+X Ctrl+C exits
sed -iOne line has to change and you want no editor sessionThe command exits on its own
RedirectionA line is being added, or the whole file is being replacedThe command exits on its own

Every interactive editor in the table works the same way underneath. It reads the file into memory, you change the in-memory copy, and the file on disk stays untouched until a write command runs, so the buffer is the only place your edit exists until then.

The split between buffer and file is where work gets lost on Linux. Closing an editor with the change unsaved is the common one, and expecting a write that never fired is the other.

A screen full of your new text is therefore not evidence that the file changed.

Which route you take depends on how much of the file is changing and how often. A one-line value in a config file suits sed, a rewrite of a long script suits an editor, and a file the machine depends on suits a copy taken beforehand either way.

What You Need Before You Start

You need a terminal and the path to the file you want to change. Nano and Vim are already installed on Debian, Ubuntu, Fedora, Rocky, and most of their derivatives, so the only decision at this point is which one you want.

Check what the machine actually has before installing anything.

nano --version | head -1; vim --version | head -1; emacs --version | head -1

I ran that on an Ubuntu 24.04 box and it returned GNU nano 7.2, Vim 9.1, and GNU Emacs 29.3. Emacs was the only one missing, so I installed it.

Terminal output showing the installed versions of nano, Vim, and Emacs
The three editors report their versions in one line each

The terminal build of Emacs is enough when you reach the machine over SSH. The plain emacs package pulls in a windowed version and a long dependency list, which is a lot of disk for a host with no display.

sudo apt install emacs-nox

Whichever editor you pick, take a copy of the file first. One line now beats hunting for a backup after a bad edit, and the copy gives you something to diff against when the change turns out wrong.

cp nginx.conf nginx.conf.bak

The path also matters more than the filename. Editors resolve a bare name against the directory your shell is sitting in, so nginx.conf and /etc/nginx/nginx.conf can be two different files. I used a three-line server block in the home directory as the example for everything below, and the way to create a new file in Linux is worth knowing before an edit is possible at all.

Edit a File with nano

The nano text editor is the one to reach for when the change is small and you do not want to learn a mode system first. It behaves like an ordinary text box, and every key you need is printed on the screen rather than hidden behind a menu.

Open the file

Pass the file path as the argument.

nano nginx.conf

Nano fills the terminal with the file and puts the filename in the top bar. The word Modified appears beside it as soon as you type, which means the buffer differs from the copy on disk.

Nano editing a file, with Modified shown beside the filename
Nano marks a changed buffer as Modified

Make the change

There is no insert mode to enter, so typing puts text where the cursor sits and the arrow keys move the cursor.

Nano prints its own key list across the bottom two rows of the screen.

The two-row nano key list at the bottom of the screen
Nano lists every key you need at the bottom
KeyWhat it does
Ctrl+OWrites the buffer out to a file
Ctrl+XLeaves the editor
Ctrl+WSearches the buffer
Ctrl+KCuts the current line to the cutbuffer
Ctrl+GOpens the help viewer, which documents the rest

The list changes with context, so the entries switch when you open a menu such as Write Out or Read File. Search stays on Ctrl+W and cutting stays on Ctrl+K for as long as you are in the buffer, and the nano versus Vim comparison covers the rest of the key map.

I opened the same file in nano 7.2 and the list along the bottom carried the same entries this table lists.

Write out and exit

Ctrl+O writes the buffer to the disk. Nano prompts for a filename at the bottom of the screen with the current name already filled in, so pressing Enter accepts it.

Ctrl+X then leaves the editor.

When the buffer has unsaved changes, Ctrl+X asks before it lets you go, which is the safety net that keeps a half-finished edit from vanishing.

Nano asking whether to save the modified buffer
Nano asks before discarding an unsaved change

Y saves the buffer, N throws the change away, and Ctrl+C cancels the exit and returns you to the text. Choosing Y writes the file and puts the prompt back with no extra step.

Edit a File with Vim

Vim opens in normal mode, where typing is read as a command instead of text. That one design choice is why the editor feels broken to a newcomer, and it is also what makes the write-and-quit sequence so short once you know it. The Vim tutorial takes the same editor further once this file is saved.

Open the file

vim nginx.conf

Vim reports the filename at the bottom of the screen. A name that does not exist yet is labelled New File, and Vim creates the file as soon as you write the buffer.

Vim opened on a file that does not exist yet, marked New File
Vim tells you when the named file does not exist yet

Type into the file

Press i to enter insert mode.

Vim showing INSERT at the bottom left
The INSERT marker confirms you can type

The bottom of the screen shows INSERT while you type, the arrow keys still move the cursor, and Esc returns you to normal mode.

Esc is also the answer when the editor stops responding to the keys you expect, because most surprises in Vim are a mode you did not mean to enter. Nothing in the buffer is lost when you leave insert mode, and you can go back in with i at any point.

Write and quit

From normal mode, type a colon, then w, then q, then Enter.

The write-and-quit command typed on Vim's command line
Vim runs the command when you press Enter

The w writes the buffer to the file and the q quits, so the two halves of the command do their jobs in order. ZZ does the same thing from normal mode without the colon, and it is the shorter habit once your fingers know it.

I tested that sequence on Vim 9.1 with the same file, and the buffer reached the disk on the write half of the command.

To leave and throw the change away, quit with an exclamation mark after the q. Vim refuses a plain quit while the buffer is dirty, which is deliberate rather than a fault, and the other ways of getting out of the Vim editor are worth a look when the write key is not the one you want.

Edit a File with Emacs

Emacs is worth knowing when you already use it for anything else, because the same keys work everywhere inside it, including the terminal build. It also drags in the longest dependency chain of the three.

Install and open

The nox package is the terminal build and is what you want on a remote host.

sudo apt install emacs-nox

The -nw flag keeps the editor inside the terminal that started it, which matters when the machine has no display at all.

emacs -nw nginx.conf

Save and exit

Emacs is modeless, so you type straight into the buffer and save with a key sequence.

  • Ctrl+X then Ctrl+S saves the current buffer
  • Ctrl+X then Ctrl+C saves and leaves Emacs, asking about anything still unsaved
  • Ctrl+X then U undoes the last change
  • Ctrl+G abandons a command you started by mistake

I checked those four bindings against Emacs 29.3 itself rather than repeating them from memory. The terminal build and the windowed build answer to the same keys, so learning them once covers both, and the full Emacs editor tutorial picks up from there.

Change a File Without Opening an Editor

Not every file edit needs an editor session, and a script that changes one line in a config file is the clearest case for doing it with a single command. There is no buffer to write out and nothing to save.

Replace a value with sed

The sed command substitutes text and, with the -i flag, writes the result back over the same file. Without -i it prints the result and leaves the file alone, which is how you check a substitution before committing to it.

sed -i 's/old\.example\.com/new.example.com/' nginx.conf; cat nginx.conf
Terminal showing nginx.conf before and after an in-place sed substitution
The file before the substitution and the same file afterwards

The output shows the file before the substitution and the same file afterwards. The slashes separate the text to find from the text to put in its place, and a g on the end of the expression replaces every match on a line instead of only the leftmost one.

I ran the substitution against the same file, and the two halves of that output are the file before the change and after it.

The sed command in Linux goes well beyond one substitution, and this form is the one to learn first.

Add a suffix straight after the flag and sed writes a copy of the original before it edits.

sed -i.bak 's/listen 80;/listen 8080;/' nginx.conf; cat nginx.conf; ls -l nginx.conf.bak

The copy arrives beside the original with the suffix on the end of its name. That is one extra character to type, and it turns a typo into something you undo rather than something you rebuild from memory.

Append or replace with redirection

A single greater-than sign replaces the whole file with the output of the command on its left.

printf 'server_name new.example.com;\n' > nginx.conf; cat nginx.conf

That is the form to be careful with, because the truncation happens before the command on the right runs. Two greater-than signs append instead, which leaves the existing lines in place, and appending text to the end of a file is the same move with a different sign.

printf 'listen 443 ssl;\n' >> nginx.conf; cat nginx.conf
Terminal showing a line appended to nginx.conf with the double greater-than sign
The appended line lands under the lines already in the file

Both forms work with any command that writes to standard output, so cat, echo, and printf are interchangeable as the writer. The appended line lands under the lines that were already in the file.

When the Save Is Refused

A refused save comes from one of three places, and each one has a different fix. The message matters more than the editor, because the shell produces some of them and Vim produces others.

What you seeWhat it meansWhat to do
Permission deniedThe file has no write bit for your userChange the mode with chmod, or write as root
E325 and a swap file nameAnother Vim session is on the file, or an earlier one crashedQuit the other session, or recover with vim -r
Vim will not quit with qThe buffer still has unsaved changesWrite with w first, or quit with an exclamation mark to discard

A file set to read-only refuses the write at the shell level, before any editor is involved.

chmod 444 nginx.conf; ls -l nginx.conf; printf 'listen 443 ssl;\n' >> nginx.conf
Terminal showing a read-only file and the Permission denied message that follows
The shell refuses the append before an editor is involved

I set the file read-only and the mode came back as three r characters with no w anywhere, so the shell would not open it for appending. An editor meets the same wall when it tries to write its buffer, which is why the failure arrives at save time rather than when the file opens. The chmod command sets those bits.

For a file owned by root, sudoedit makes a temporary copy owned by you, runs your editor on the copy, and moves the result back with root permissions. That keeps your own editor configuration and keeps the editing process out of the root account.

sudoedit /etc/nginx/nginx.conf

A write that has to come from a script instead gives up the temporary copy in exchange for working without a terminal.

sudo sh -c "printf 'listen 443 ssl;\n' >> /etc/nginx/nginx.conf"

Vim’s swap file message is the third case and the least dangerous. Vim leaves a .swp file beside any file it has open, so the message usually means a second terminal still has the file loaded rather than a crash.

Removing the swap file is safe only when no other session is using it. Quit the other session or recover the text with vim -r first, then delete the .swp file once you have what you need.

What to Reach For Next Time

Pick one editor and stay in it long enough for the exit keys to stick.

  • Read the file back after every write, so the change is confirmed rather than assumed
  • Keep the .bak suffix on sed while you are learning, then drop it once the search text is right
  • Practise the write-and-quit sequence on a throwaway file before you use it on a file the machine needs

Nano covers a quick change, Vim covers the machine that has nothing else, and Emacs covers the rest if you already live in it. The sed and redirection forms are worth keeping in a note, because they are the two you will reach for inside a script.

Frequently Asked Questions

How do I edit a file in Linux from the terminal?

Open the file with nano filename or vim filename, make the change, then write the buffer out. In nano that is Ctrl+O followed by Ctrl+X, and in Vim it is Esc followed by a colon, then wq, then Enter.

How do I save and exit a file in Vim?

Press Esc to leave insert mode, then type a colon, w, q and press Enter. The w writes the buffer to the file and the q quits the editor.

What does Modified mean in nano?

The buffer in memory differs from the file on disk. The change is not saved until you press Ctrl+O, or answer Y when Ctrl+X asks you about the unsaved buffer.

Can I change a file in Linux without opening an editor?

Yes. sed with the -i flag substitutes text in place, a single greater-than sign replaces a file with a command’s output, and two greater-than signs append to it.

Why does Linux say Permission denied when I try to edit a file?

The file’s mode has no write bit for your user, so the shell refuses to open it for writing. Either change the mode with chmod or edit it under sudo or sudoedit.

Why does Vim show a swap file warning when I open a file?

Vim found a .swp file beside your file, which means another edit session is still open or a previous one ended without saving. Quit the other session, or recover the edits with vim -r.