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.

  • ๐Ÿ“ Hvad er det: The vi editor ships with almost every Linux and Unix system and behaves the same across platforms and distributions.
  • โŒจ๏ธ To tilstande: Command mode runs actions like copy, delete, and save, while insert mode is for typing tekst.
  • ๐Ÿš€ Lancering: Type vi followed by a filename to open an existing file or create a new one.
  • โœ๏ธ redigering: Single-letter commands such as i, a, dd, and x insert, append, delete lines, and remove characters.
  • ๐Ÿ’พ Gemmer: From command mode, :w saves, :wq saves and quits, and :q! quits without saving.
  • ๐Ÿค– AI assistance: AI assistants and GitHub Copilot can explain vi commands and suggest edits while you learn.

vi editor commands and command and insert modes in Linux and Unix

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.

The vi editor open in a Linux terminal window

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.

Creating and editing a new file in the vi editor

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.

A file open in the vi editor ready to save and quit

Ofte Stillede Spรธrgsmรฅl

Vim stands for โ€œVi Improvedโ€ and is an enhanced version of vi. It adds features such as syntax highlighting, multilevel undo and redo, split windows, tabs, and word completion. The classic vi is available on nearly every Unix system, which is why its commands are worth learning.

In command mode, press / followed by the text and Enter to search forward, or use ? to search backward. Press n to jump to the next match and N to jump to the previous one. Searches are case-sensitive unless you change the setting.

In command mode, yy copies (yanks) the current line and p pastes it below the cursor. Use a number, such as 3yy, to copy several lines at once. Deleting with dd also places the removed text on the clipboard, so p can paste it elsewhere.

From command mode, type :set number (or :set nu) and press Enter to show line numbers along the left margin. Run :set nonumber to hide them again. Line numbers make it easier to jump to a specific line and to follow error messages.

Nano is a simpler editor that shows its shortcuts on screen and needs no separate modes, so beginners find it approachable. The vi editor has a steeper learning curve but is faster for experienced users and is installed on more systems by default, including minimal servers.

Run vi -R filename, or use the view command, to open a file in read-only mode. This lets you browse and search safely without accidentally changing the contents. You can still force a write with :w! if you decide to save an edit.

AI assistants can explain what an unfamiliar vi command does, translate a plain-English request into the right keystrokes, and troubleshoot mistakes. Machine-learning-based tools can also suggest the next edit, lowering the steep learning curve that vi is known for.

Ja. GitHub Copilot offers an official plugin for Vim og Neovim that suggests code as you type in insert mode. It brings AI completion to the keyboard-driven workflow, though you still accept or reject each suggestion yourself.

Opsummer dette indlรฆg med: