Vim basics: edit, save and quit files on a server

Learn just enough Vim to edit a config file on any Linux server, including modes, inserting text, saving, quitting without saving, moving around, deleting, undoing and searching.

  • Beginner
  • Linux VPS
  • 5 min read

By RunRacks Team · Published

Vim (and its older form, vi) is installed on almost every Linux server, including minimal ones and rescue environments where nothing else is available. It looks confusing at first because typing does not insert text straight away. Once you understand its modes, a dozen keys are enough to edit, save and quit with confidence.

Before you start

  • SSH access to your Linux VPS. See Connect to your Linux VPS with SSH.
  • On a minimal install, only vi may be present. Every key in this guide works in both vi and vim. To install full Vim on Ubuntu or Debian:
Terminal
sudo apt install vim

On AlmaLinux or Rocky Linux:

Terminal
sudo dnf install vim-enhanced

Understand the modes

Vim has several modes. You need three:

  • Normal mode: the mode Vim starts in. Keys are commands (move, delete, undo), not text.
  • Insert mode: keys type text, like a normal editor. The bottom line shows -- INSERT --.
  • Command-line mode: you type a command after : at the bottom of the screen, for example to save or quit.

The golden rule: press Esc to get back to normal mode. If you are ever unsure what mode you are in, press Esc once or twice. It is always safe.

Open a file

Terminal
vim notes.txt

To open a system file, use sudo:

Terminal
sudo vim /etc/ssh/sshd_config

Add +42 to jump straight to line 42:

Terminal
vim +42 /etc/nginx/nginx.conf

Insert text

From normal mode, press one of these to start typing:

  • i: insert before the cursor.
  • a: append after the cursor.
  • o: open a new line below and start typing.
  • O: open a new line above.

Type your text, then press Esc to go back to normal mode.

Save and quit

In normal mode, type a colon command and press Enter:

  • :w saves (writes) the file.
  • :q quits. Vim refuses if there are unsaved changes.
  • :wq saves and quits. :x and ZZ do the same, but only write if something changed.
  • :q! quits without saving, throwing away your changes.

Move around

The arrow keys, Page Up and Page Down work in most terminals. The classic Vim keys are worth learning because they work everywhere:

  • h j k l: left, down, up, right.
  • w / b: next word / previous word.
  • 0 / $: start / end of the line.
  • gg / G: first / last line of the file.
  • 42G or :42: go to line 42.

Turn on line numbers with :set number and off again with :set nonumber.

Delete, copy, paste and undo

All of these are typed in normal mode:

  • x: delete the character under the cursor.
  • dd: delete (cut) the whole line. 3dd cuts three lines.
  • dw: delete to the end of the word.
  • yy: copy ("yank") the line. 3yy copies three lines.
  • p: paste below the cursor. P: paste above.
  • u: undo. Press it repeatedly to undo more.
  • Ctrl + R: redo.
  • .: repeat the last change.
  • Type /word and press Enter to search forward. n jumps to the next match, N to the previous one.
  • ?word searches backward.
  • :%s/old/new/g replaces every old with new in the whole file.
  • :%s/old/new/gc does the same, but asks you to confirm each change with y or n.

Paste from your computer without a mess

When you paste a block of text from your PC into Vim over SSH, auto-indent can add extra spaces on every line. Recent Vim versions handle this automatically in most terminals. If you see a staircase of indentation, undo with u, then:

  1. Type :set paste and press Enter.
  2. Press i, paste with your terminal's paste shortcut, and press Esc.
  3. Type :set nopaste and press Enter.

Edit configs safely

The same routine as with any editor applies:

  1. Back up the file: sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.
  2. Edit and save with :wq.
  3. Test the config before restarting the service, for example sudo sshd -t or sudo nginx -t.

Check it works

Practise on a scratch file:

  1. Run vim ~/practice.txt.
  2. Press i, type hello vim, and press Esc.
  3. Type :wq and press Enter.
  4. Run cat ~/practice.txt. You should see hello vim.

For a guided tour, run vimtutor. It takes about half an hour and teaches the basics hands-on.

Troubleshooting

  • Typing does nothing or does strange things: you are in normal mode. Press i to insert text.
  • `E37: No write since last change`: you tried :q with unsaved changes. Use :wq to save or :q! to discard.
  • `E45: 'readonly' option is set` or `Can't open file for writing`: you opened a root-owned file without sudo. Save a copy with :w ~/file.new, quit, and copy it into place with sudo cp. Or, in Vim, :w !sudo tee % writes it with sudo.
  • `Found a swap file` warning on open: Vim crashed or the file is open in another session. Choose (R)ecover to restore unsaved work, or (D)elete the swap file if you know the other session is gone.
  • The screen froze: you may have pressed Ctrl + S. Press Ctrl + Q to resume.

Next steps

Was this guide helpful?

Still stuck?

Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.

Getting started

Edit files with nano: a beginner's guide

Open, edit, search and save files with the nano text editor on a Linux server, including cutting and pasting lines, showing line numbers and safely editing system config files with sudo.

  • Beginner
  • Linux VPS
  • 5 min read

Getting started

Your first hour with a new RunRacks server

A step-by-step roadmap for the first hour after your RunRacks server is delivered, from finding your login details to changing the password, updating the OS and picking your next guides.

  • Beginner
  • All
  • 5 min read

Getting started

How to choose a server location

Pick a data center location based on where your users, broker or team are, confirm it with ping tests, and weigh the legal and latency points that come with each choice.

  • Beginner
  • All
  • 4 min read