Create and Edit Files with Vi Editor

LinuxLinuxBeginner
Practice Now

Introduction

The Vi text editor is a powerful and widely-used tool for editing files in the Linux operating system. In this tutorial, we will guide you through the process of saving changes and exiting the Vi editor, equipping you with the essential skills to efficiently manage your files and workflows.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/VersionControlandTextEditorsGroup -.-> linux/vim("`Text Editing`") linux/VersionControlandTextEditorsGroup -.-> linux/nano("`Simple Text Editing`") linux/VersionControlandTextEditorsGroup -.-> linux/gedit("`Graphical Text Editing`") linux/UserandGroupManagementGroup -.-> linux/set("`Shell Setting`") linux/VersionControlandTextEditorsGroup -.-> linux/vimdiff("`File Difference Viewing`") subgraph Lab Skills linux/vim -.-> lab-392949{{"`Create and Edit Files with Vi Editor`"}} linux/nano -.-> lab-392949{{"`Create and Edit Files with Vi Editor`"}} linux/gedit -.-> lab-392949{{"`Create and Edit Files with Vi Editor`"}} linux/set -.-> lab-392949{{"`Create and Edit Files with Vi Editor`"}} linux/vimdiff -.-> lab-392949{{"`Create and Edit Files with Vi Editor`"}} end

Introduction to Vi Editor

What is Vi Editor?

Vi (Visual) is a powerful, screen-based text editor widely used in Linux and Unix-like operating systems. As a command-line text editor, Vi provides efficient text manipulation capabilities for developers, system administrators, and power users. It is pre-installed on most Linux distributions, making it a universal tool for text editing.

Key Characteristics of Vi

Vi editor offers several unique features that distinguish it from other text editors:

Feature Description
Modal Editing Operates in different modes with distinct functionalities
Lightweight Minimal resource consumption
Ubiquitous Available on almost all Unix-like systems
Keyboard-Driven Efficient editing without mouse interaction

Vi Editor Workflow

graph TD A[Open Terminal] --> B[Launch Vi] B --> C{Select Mode} C -->|Normal Mode| D[Navigate Text] C -->|Insert Mode| E[Edit Text] C -->|Command Mode| F[Execute Commands]

Basic Vi Usage Example

Here's a simple example of creating and editing a text file using Vi on Ubuntu 22.04:

## Create a new file
vi sample.txt

## Vi enters Normal mode by default
## Press 'i' to switch to Insert mode
## Type your text content

## Press 'Esc' to return to Normal mode
## Type ':wq' to save and quit

This introduction provides a foundational understanding of the Vi text editor, its core principles, and basic usage in a Linux command-line environment.

Vi Modes and Commands

Vi Editor Modes

Vi operates through distinct modes, each serving a specific purpose in text editing:

Mode Primary Function Key Characteristics
Normal Mode Navigation and Command Execution Default mode, no text insertion
Insert Mode Text Input Active text editing state
Command Mode Advanced Operations File saving, searching, etc.

Mode Transition Commands

graph TD A[Normal Mode] -->|i| B[Insert Mode] A -->|:| C[Command Mode] B -->|Esc| A C -->|Esc| A

Key navigation commands in Normal mode:

  • h: Move left
  • j: Move down
  • k: Move up
  • l: Move right
  • 0: Move to line start
  • $: Move to line end
  • gg: Go to document start
  • G: Go to document end

Insert Mode Techniques

## Enter Insert Mode
vi sample.txt
i   ## Start inserting text
Esc ## Return to Normal Mode

## Alternative Insert Commands
a   ## Insert after cursor
o   ## Insert new line below
O   ## Insert new line above

Command Mode Operations

## Common Command Mode Actions
:w      ## Save file
:q      ## Quit editor
:wq     ## Save and quit
:q!     ## Quit without saving
/text   ## Search for text
:%s/old/new/g  ## Global text replacement

This section provides a comprehensive overview of Vi's core modes and essential commands for effective text editing in a Linux environment.

Advanced Vi Operations

Text Manipulation Techniques

Advanced Vi provides powerful text editing capabilities beyond basic operations:

Operation Command Description
Copy Line yy Yank (copy) entire line
Cut Line dd Delete and copy line
Paste p Paste copied content
Undo u Revert last action
Redo Ctrl + r Restore undone action

Multiple File Handling

graph TD A[Open Multiple Files] --> B[Split Window] B --> C[Navigate Between Files] C --> D[Edit Simultaneously]

Advanced Editing Commands

## Open multiple files
vi -o file1.txt file2.txt

## Split screen horizontally
:split filename

## Split screen vertically
:vsplit filename

## Switch between windows
Ctrl + w + (direction key)

Vi Configuration and Customization

## Create Vi configuration file
vi ~/.vimrc

## Example configuration
set number         ## Show line numbers
syntax on          ## Enable syntax highlighting
set autoindent     ## Auto-indent new lines
set tabstop=4      ## Set tab width

Text Replacement and Searching

## Global text replacement
:%s/oldtext/newtext/g

## Case-sensitive search
/SearchTerm

## Case-insensitive search
/SearchTerm\c

## Replace with confirmation
:%s/oldtext/newtext/gc

This section explores advanced Vi operations, demonstrating sophisticated text editing techniques in a Linux environment.

Summary

By the end of this tutorial, you will have a solid understanding of how to save your work and exit the Vi text editor in Linux. You will learn the necessary commands and techniques to seamlessly integrate the Vi editor into your daily tasks, empowering you to become more productive and efficient in your Linux environment.

Other Linux Tutorials you may like