0
0
Linux CLIscripting~5 mins

vim basics (insert, command, save, quit) in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Vim is a text editor used in the command line to create and edit files. It helps you write and save text quickly without leaving the terminal.
When you need to quickly edit a configuration file on a remote server via SSH.
When you want to write or modify scripts directly in the terminal.
When you want a lightweight editor that works without a graphical interface.
When you need to make quick notes or edits without opening a full desktop editor.
Commands
This command opens the file named example.txt in vim. If the file does not exist, vim creates a new empty file with that name.
Terminal
vim example.txt
Expected OutputExpected
No output (command runs silently)
Pressing 'i' switches vim into insert mode, allowing you to type and edit text like a normal editor.
Terminal
i
Expected OutputExpected
No output (command runs silently)
Type this text while in insert mode to add content to the file.
Terminal
Hello, this is a test.
Expected OutputExpected
No output (command runs silently)
Press the Escape key to exit insert mode and return to command mode where you can run commands like save or quit.
Terminal
Esc
Expected OutputExpected
No output (command runs silently)
Type ':w' and press Enter to save (write) the changes you made to the file without quitting vim.
Terminal
:w
Expected OutputExpected
"example.txt" 1L, 23C written
Type ':q' and press Enter to quit vim if you have no unsaved changes.
Terminal
:q
Expected OutputExpected
No output (command runs silently)
Type ':wq' and press Enter to save your changes and then quit vim in one step.
Terminal
:wq
Expected OutputExpected
No output (command runs silently)
Type ':q!' and press Enter to quit vim without saving any changes you made.
Terminal
:q!
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from vim, remember: press 'i' to start typing, Esc to stop typing, ':w' to save, and ':q' to quit.

Common Mistakes
Trying to type text without pressing 'i' first.
Vim starts in command mode where typing letters runs commands instead of inserting text.
Press 'i' to enter insert mode before typing your text.
Trying to quit with ':q' when there are unsaved changes.
Vim will prevent quitting to avoid losing changes and show a warning.
Use ':wq' to save and quit or ':q!' to quit without saving.
Pressing Esc multiple times thinking it saves the file.
Esc only switches modes; it does not save the file.
After pressing Esc, type ':w' to save your changes.
Summary
Open a file with 'vim filename' to start editing.
Press 'i' to enter insert mode and type your text.
Press Esc to return to command mode.
Use ':w' to save, ':q' to quit, ':wq' to save and quit, and ':q!' to quit without saving.