VI Editor med kommandoer i Linux/Unix Tutorial
โก Smart opsummering
The vi editor is the classic, universally available text editor of Linux and Unix, working entirely through the keyboard in two modes: a command mode for actions and an insert mode for typing tekst.

The vi editor is the text editor you will meet on almost any Linux or Unix machine, so learning its keyboard-driven workflow is a skill that transfers everywhere. This guide explains its two modes, its editing commands, and how to save and quit a file.
What is the vi editor?
The vi editor is the most popular and classic text editor in the Linux family. Below are some of the reasons that make it a widely used editor:
- It is available in almost all Linux distributions.
- It works the same across different platforms and distributions.
- It is user-friendly, so millions of Linux users rely on it for their editing needs.
Nowadays there are advanced versions of the vi editor available, and the most popular one is VIM, which stands for Vi Improved. Some of the others are Elvis, Nvi, Nano, and Vile. It is wise to learn vi because it is feature-rich and offers endless possibilities to edit a file.
vi Editor Operation Modes
To work on the vi editor, you need to understand its operation modes. They can be divided into two main parts: command mode and insert mode. The screenshot below shows the vi editor open in a Linux terminal.
vi Command Mode
- The vi editor opens in this mode, and it only understands commands.
- In this mode you can move the cursor and cut, copy, and paste text.
- This mode also saves the changes you have made to the file.
- Commands are case-sensitive, so you should use the right letter case.
vi Insert Mode
- This mode is for inserting text into the file.
- You can switch to insert mode from command mode by pressing โiโ on the keyboard.
- Once you are in insert mode, any key is taken as input for the file you are currently working on.
- To return to command mode and save the changes you have made, press the Esc key.
Hvordan man bruger vi editor
To launch the vi editor, open the terminal (CLI) and type:
vi <filename_NEW> or <filename_EXISTING>
If you specify an existing file, the editor opens it for you to edit. Otherwise, it creates a new file. The screenshot below shows a new file being created and edited in vi.
VI Redigeringskommandoer
The vi editing commands below let you insert, delete, and change text. Use them from command mode.
| Kommando | Beskrivelse |
|---|---|
| i | Indsรฆt ved markรธren (gรฅr i indsรฆttelsestilstand) |
| a | Skriv efter markรธren (gรฅr i indsรฆttelsestilstand) |
| A | Skriv i slutningen af โโlinjen (gรฅr i indsรฆttelsestilstand) |
| ESC | Afslut indsรฆttelsestilstand |
| u | Fortryd sidste รฆndring |
| U | Fortryd alle รฆndringer pรฅ hele linjen |
| o | ร bn en ny linje (gรฅr i indsรฆttelsestilstand) |
| dd | Slet linje |
| 3dd | Slet 3 linjer |
| D | Slet indholdet af linjen efter markรธren |
| C | Slet indholdet af en linje efter markรธren og indsรฆt ny tekst. Tryk pรฅ ESC-tasten for at afslutte indsรฆttelsen. |
| dw | Slet ord |
| 4dw | Slet 4 ord |
| cw | Skift ord |
| x | Slet tegn ved markรธren |
| r | Erstat tegn |
| R | Overskriv tegn fra markรธren og frem |
| s | Substitute one character under the cursor and continue inserting |
| S | Substitute entire line and begin inserting at the beginning of the line |
| ~ | Skift mellem store og smรฅ bogstaver |
Bemรฆrk: You should be in command mode to execute these commands. The vi editor is case-sensitive, so make sure you type the commands in the right letter case. Pressing the wrong command can make undesirable changes to the file. You can also enter insert mode by pressing a, A, or o as required.
Flytning i en fil
You need to be in command mode to move within a file. The default navigation keys are listed below, but you can also use the arrow keys on the keyboard.
| Kommando | Beskrivelse |
|---|---|
| k | Move cursor up |
| j | Move cursor down |
| h | Move cursor left |
| l | Move cursor right |
Gemmer og lukker filen
You should be in command mode to exit the editor and save changes to the file. The commands below cover the common ways to save and quit.
| Kommando | Beskrivelse |
|---|---|
| Shift+zz | Save the file and quit |
| :w | Save the file but keep it open |
| Q! | Quit vi and do not save changes |
| : WQ | Save the file and quit |
The screenshot below shows a file open in the vi editor, ready to be saved and closed.

.jpg)

