国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Perform Basic File Editing Operations in Vi Editor
Why Should I Want to Learn Vi?
Launching Vi Editor
Understanding Vi Modes
Vi Command-Line Mode
Vi Ex Mode
Vi Insert Mode
Vi Commands Usage
Vi Options
Search and Replace Text in Vi
Searching Within a Line
Searching an Entire File
Search and Replace Text
Editing Multiple Files in Vi
Temporary Vi Buffers
Summary
Home System Tutorial LINUX LFCS #2: How to Install and Use Vi/Vim in Linux

LFCS #2: How to Install and Use Vi/Vim in Linux

Jun 09, 2025 am 09:45 AM

A couple of months ago, the Linux Foundation launched the LFCS (Linux Foundation Certified Sysadmin) certification in order to help individuals from all over the world to verify they are capable of doing basic to intermediate system administration tasks on Linux systems.

The Linux administration tasks involve first-hand troubleshooting and maintenance, plus intelligent decision-making to know when it’s time to raise issues to upper support teams.

The series will be titled Preparation for the LFCS (Linux Foundation Certified Sysadmin) Parts 1 through 33 and cover the following topics:

No. Title
Part 1 How to Use ‘Sed’ Command to Manipulate Files in Linux
Part 2 How to Install and Use Vi/Vim in Linux
Part 3 How to Compress Files & Directoires and Finding Files in Linux
Part 4 Partitioning Storage Devices, Formatting Filesystems, and Configuring Swap Partition
Part 5 Mount/Unmount Local and Network (Samba & NFS) Filesystems in Linux
Part 6 Assembling Partitions as RAID Devices – Creating & Managing System Backups
Part 7 Managing System Startup Processes and Services (SysVinit, Systemd, and Upstart
Part 8 How to Manage User & Groups, File Permissions, and Sudo Access
Part 9 Linux Package Management with Yum, RPM, Apt, Dpkg, Aptitude, and Zypper
Part 10 Learning Basic Shell Scripting and Filesystem Troubleshooting
Part 11 How to Manage and Create LVM Using vgcreate, lvcreate, and lvextend Commands
Part 12 How to Explore Linux with Installed Help Documentations and Tools
Part 13 How to Configure and Troubleshoot Grand Unified Bootloader (GRUB)
Part 14 Monitor Linux Processes Resource Usage and Set Process Limits on a Per-User Basis
Part 15 How to Set or Modify Kernel Runtime Parameters in Linux Systems
Part 16 Implementing Mandatory Access Control with SELinux or AppArmor in Linux
Part 17 How to Set Access Control Lists (ACLs) and Disk Quotas for Users and Groups
Part 18 Installing Network Services and Configuring Automatic Startup at Boot
Part 19 An Ultimate Guide to Setting Up FTP Server to Allow Anonymous Logins
Part 20 Setup a Basic Recursive Caching DNS Server and Configure Zones for Domain
Part 21 How to Install, Secure, and Performance Tuning of MariaDB Database Server
Part 22 How to Install and Configure NFS Server for File System Sharing
Part 23 How to Setup Apache with Name-Based Virtual Hosting with SSL Certificate
Part 24 How To Setup an Iptables Firewall to Enable Remote Access to Services in Linux
Part 25 How to Turn a Linux into a Router to Handle Traffic Statically and Dynamically
Part 26 How to Setup Encrypted Filesystems and Swap Using Cryptsetup Tool
Part 27 How to Monitor System Usage, Outages, and Troubleshoot Linux Servers
Part 28 How to Setup a Network Repository to Install or Update Packages
Part 29 How to Audit Network Performance, Security, and Troubleshooting
Part 30 How to Install and Manage Virtual Machines and Containers
Part 31 Learn the Basics of Git to Manage Projects Efficiently
Part 32 A Beginner’s Guide to Configuring IPv4 and IPv6 Addresses in Linux
Part 33 A Beginner’s Guide to Creating Network Bonding and Bridging in Ubuntu

This post is Part 2 of a 33-tutorial series, here in this part, we will cover the basic file editing operations and understanding modes in Vi/Vim editor, that are required for the LFCS certification exam.

Perform Basic File Editing Operations in Vi Editor

Vi was the first full-screen text editor written for Unix. Although it was intended to be small and simple, it can be a bit challenging for people used exclusively to GUI text editors, such as NotePad , or gedit, to name a few examples.

To use Vi, we must first understand the 3 modes in which this powerful program operates, in order to begin learning later about its powerful text-editing procedures.

Please note that most modern Linux distributions ship with a variant of vi known as vim (“Vi improved”), which supports more features than the original vi does. For that reason, throughout this tutorial, we will use vi and vim interchangeably.

If your distribution does not have Vim installed, you can install it as follows.

$ sudo apt install vim         [On <strong>Debian, Ubuntu and Mint</strong>]
$ sudo yum install vim         [On <strong>RHEL/CentOS/Fedora</strong> and <strong>Rocky/AlmaLinux</strong>]
$ sudo emerge -a sys-apps/vim  [On <strong>Gentoo Linux</strong>]
$ sudo apk add vim             [On <strong>Alpine Linux</strong>]
$ sudo pacman -S vim           [On <strong>Arch Linux</strong>]
$ sudo zypper install vim      [On <strong>OpenSUSE</strong>]    

Why Should I Want to Learn Vi?

There are at least 2 good reasons to learn vi editor.

  1. vi is always available (no matter what distribution you’re using) since it is required by POSIX.
  2. vi does not consume a considerable amount of system resources and allows us to perform any imaginable task without lifting our fingers from the keyboard.

In addition, vi has a very extensive built-in manual, which can be launched using the :help command right after the program is started. This built-in manual contains more information than vi/m’s man page.

LFCS #2: How to Install and Use Vi/Vim in Linux

Launching Vi Editor

To launch vi, type vi in your command prompt.

# vi

LFCS #2: How to Install and Use Vi/Vim in Linux

Then press i to enter Insert mode, and you can start typing.

Another way to launch vi/m is.

# vi filename

This will open a new buffer (more on buffers later) named filename, which you can later save to disk.

Understanding Vi Modes

Vi editor has multiple modes, each serving a distinct purpose and allowing users to perform specific actions.

Here are the main modes you should be aware of:

Vi Command-Line Mode

In command mode, vi allows the user to navigate around the file and enter vi commands, which are brief, case-sensitive combinations of one or more letters. Almost all of them can be prefixed with a number to repeat the command that number of times.

For example, yy (or Y) copies the entire current line, whereas 3yy (or 3Y) copies the entire current line along with the two next lines (3 lines in total).

We can always enter command mode (regardless of the mode we’re working on) by pressing the Esc key. The fact that in command mode the keyboard keys are interpreted as commands instead of text tends to be confusing to beginners.

Vi Ex Mode

In ex mode, we can manipulate files (including saving a current file and running outside programs). To enter this mode, we must type a colon (:) from command mode, directly followed by the name of the ex-mode command that needs to be used. After that, vi returns automatically to command mode.

Vi Insert Mode

In insert mode (the letter i is commonly used to enter this mode), we simply enter text. Most keystrokes result in text appearing on the screen (one important exception is the Esc key, which exits insert mode and returns to command mode).

LFCS #2: How to Install and Use Vi/Vim in Linux

Vi Commands Usage

The following table shows a list of commonly used vi commands. File edition commands can be enforced by appending the exclamation sign to the command (for example, :q! enforces quitting without saving).

?Key Command ?Description
?h or left arrow ?Go one character to the left
?j or down arrow ?Go down one line
?k or up arrow ?Go up one line
?l (lowercase L) or right arrow ?Go one character to the right
?H ?Go to the top of the screen
?L ?Go to the bottom of the screen
?G ?Go to the end of the file
?w ?Move one word to the right
?b ?Move one word to the left
?0 (zero) ?Go to the beginning of the current line
?^ ?Go to the first nonblank character on the current line
?$ ?Go to the end of the current line
?Ctrl-B ?Go back one screen
?Ctrl-F ?Go forward one screen
?i ?Insert at the current cursor position
?I (uppercase i) ?Insert at the beginning of the current line
?J (uppercase j) ?Join the current line with the next one (move next lineup)
?a ?Append after the current cursor position
?o (lowercase O) ?Creates a blank line after the current line
?O (uppercase o) ?Creates a blank line before the current line
?r ?Replace the character at the current cursor position
?R ?Overwrite at the current cursor position
?x ?Delete the character at the current cursor position
?X ?Delete the character immediately before (to the left) of the current cursor position
?dd ?Cut (for later pasting) the entire current line
?D ?Cut from the current cursor position to the end of the line (this command is equivalent to d$)
?yX ?Give a movement command X, copy (yank) the appropriate number of characters, words, or lines from the current cursor position
?yy or Y ?Yank (copy) the entire current line
?p ?Paste after (next line) the current cursor position
?P ?Paste before (previous line) the current cursor position
?. (period) ?Repeat the last command
?u ?Undo the last command
?U ?Undo the last command in the last line. This will work as long as the cursor is still on the line.
?n ?Find the next match in a search
?N ?Find the previous match in a search
?:n ?Next file; when multiple files are specified for editing, this command loads the next file.
?:e file ?Load the file in place of the current file.
?:r file ?Insert the contents of the file after (next line) the current cursor position
?:q ?Quit without saving changes.
?:w file ?Write the current buffer to a file. To append to an existing file, use :w >> file.
?:wq ?Write the contents of the current file and quit. Equivalent to x! and ZZ
?:r! command ?Execute the command and insert the output after (next line) the current cursor position.

Vi Options

The following options can come in handy while running vim (we need to add them in our ~/.vimrc file).

# echo set number >> ~/.vimrc
# echo syntax on >> ~/.vimrc
# echo set tabstop=4 >> ~/.vimrc
# echo set autoindent >> ~/.vimrc

LFCS #2: How to Install and Use Vi/Vim in Linux

  • set number shows line numbers when vi opens an existing or a new file.
  • syntax on turns on syntax highlighting (for multiple file extensions) in order to make code and config files more readable.
  • set tabstop=4 sets the tab size to 4 spaces (default value is 8).
  • set autoindent carries over the previous indent to the next line.

Search and Replace Text in Vi

vi has the ability to move the cursor to a certain location (on a single line or over an entire file) based on searches. It can also perform text replacements with or without confirmation from the user.

Searching Within a Line

The f command searches a line and moves the cursor to the next occurrence of a specified character in the current line.

For example, the command fh would move the cursor to the next instance of the letter h within the current line. Note that neither the letter f nor the character you’re searching for will appear anywhere on your screen, but the character will be highlighted after you press Enter.

For example, this is what I get after pressing f4 in command mode.

LFCS #2: How to Install and Use Vi/Vim in Linux

Searching an Entire File

Use the / command, followed by the word or phrase to be searched for. A search may be repeated using the previous search string with the n command, or the next one (using the N command). This is the result of typing /Jane in command mode.

LFCS #2: How to Install and Use Vi/Vim in Linux

Search and Replace Text

Vi uses a command (similar to sed’s) to perform substitution operations over a range of lines or an entire file.

To change the word “old” to “young” for the entire file, we must enter the following command.

 :%s/old/young/g 

Notice: The colon at the beginning of the command.

LFCS #2: How to Install and Use Vi/Vim in Linux

The colon (:) starts the ex command, s in this case (for substitution), % is a shortcut meaning from the first line to the last line (the range can also be specified as n,m which means “from line n to line m”), old is the search pattern, while young is the replacement text, and g indicates that the substitution should be performed on every occurrence of the search string in the file.

Alternatively, a c can be added to the end of the command to ask for confirmation before performing any substitution.

:%s/old/young/gc

Before replacing the original text with the new one, vi/m will present us with the following message.

LFCS #2: How to Install and Use Vi/Vim in Linux

  • y: perform the substitution (yes)
  • n: skip this occurrence and go to the next one (no)
  • a: perform the substitution in this and all subsequent instances of the pattern.
  • q or Esc: quit substituting.
  • l (lowercase L): perform this substitution and quit (last).
  • Ctrl-e, Ctrl-y: Scroll down and up, respectively, to view the context of the proposed substitution.

Editing Multiple Files in Vi

Let’s type vim file1 file2 file3 in our command prompt.

# vim file1 file2 file3

First, vim will open file1. To switch to the next file (file2), we need to use the :n command. When we want to return to the previous file, :N will do the job.

In order to switch from file1 to file3.

a). The :buffers command will show a list of the file currently being edited.

:buffers

LFCS #2: How to Install and Use Vi/Vim in Linux

b). The command :buffer 3 (without the s at the end) will open file3 for editing.

In the image above, a pound sign (#) indicates that the file is currently open but in the background, while %a marks the file that is currently being edited.

On the other hand, a blank space after the file number (3 in the above example) indicates that the file has not yet been opened.

Temporary Vi Buffers

To copy a couple of consecutive lines (let’s say 4, for example) into a temporary buffer named a (not associated with a file) and place those lines in another part of the file later in the current vi section, we need to…

  • Press the ESC key to be sure we are in vi Command mode.
  • Place the cursor on the first line of the text we wish to copy.
  • Type “a4yy” to copy the current line, along with the 3 subsequent lines, into a buffer named a. We can continue editing our file – we do not need to insert the copied lines immediately.
  • When we reach the location for the copied lines, use “a before the p or P commands to insert the lines copied into the buffer named a:
    1. Type “ap to insert the lines copied into the buffer after the current line on which the cursor is resting.
    2. Type “aP to insert the lines copied into buffer a before the current line.

If we wish, we can repeat the above steps to insert the contents of the buffer in multiple places in our file. A temporary buffer, such as the one in this section, is disposed of when the current window is closed.

Summary

As we have seen, vi/m is a powerful and versatile text editor for CLI. Feel free to share your own tricks and comments below.

Update: If you want to extend your VI editor skills, then I would suggest you read the following two guides that will guide you to some useful VI editor tricks and tips.

The LFCS eBook is available now for purchase. Order your copy today and start your journey to becoming a certified Linux system administrator!

Product Name Price Buy
The Linux Foundation’s LFCS Certification Preparation Guide $19.99 [Buy Now]

Last, but not least, please consider buying your exam voucher using the following links to earn us a small commission, which will help us keep this book updated.

Become a Linux Certified System Administrator

The above is the detailed content of LFCS #2: How to Install and Use Vi/Vim in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

10 Best File Comparison and Difference (Diff) Tools in Linux 10 Best File Comparison and Difference (Diff) Tools in Linux Jun 11, 2025 am 10:26 AM

While writing program files or normal text files, programmers and writers sometimes want to know the difference between two files or two versions of the same file. When you compare two computer files on Linux, the difference between their contents is

How to create a new, empty file from the command line? How to create a new, empty file from the command line? Jun 14, 2025 am 12:18 AM

There are three ways to create empty files in the command line: First, the simplest and safest use of the touch command, which is suitable for debugging scripts or placeholder files; Second, it is quickly created through > redirection but will clear existing content, which is suitable for initializing log files; Third, use echo"> file name to create a file with an empty string, or use echo-n""> file name to avoid line breaks. These three methods have their own applicable scenarios, and choosing the right method can help you complete the task more efficiently.

5 Best Open Source Mathematical Equation Editors for Linux 5 Best Open Source Mathematical Equation Editors for Linux Jun 18, 2025 am 09:28 AM

Are you looking for good software to write mathematical equations? If so, this article provides the top 5 equation editors that you can easily install on your favorite Linux distribution.In addition to being compatible with different types of mathema

How to Install Eclipse IDE in Debian, Ubuntu, and Linux Mint How to Install Eclipse IDE in Debian, Ubuntu, and Linux Mint Jun 14, 2025 am 10:40 AM

Eclipse is a free integrated development environment (IDE) that programmers around the world use to write software, primarily in Java, but also in other major programming languages using Eclipse plugins.The latest release of Eclipse IDE 2023?06 does

dutree - Analyze File System Disk Usage in Linux dutree - Analyze File System Disk Usage in Linux Jun 11, 2025 am 10:33 AM

dutree is a free, open-source, fast command-line tool for analyzing disk usage, written in the Rust programming language. It was created by combining durep (disk usage reporter) and tree (list directory content in tree-like format) command-line tools

15 Useful 'ifconfig' Commands to Configure Network in Linux 15 Useful 'ifconfig' Commands to Configure Network in Linux Jun 11, 2025 am 10:01 AM

ifconfig in short “interface configuration” utility for system/network administration in Unix/Linux operating systems to configure, manage, and query network interface parameters via command-line interface or in a system configuration scripts

SCP Linux Command – Securely Transfer Files in Linux SCP Linux Command – Securely Transfer Files in Linux Jun 20, 2025 am 09:16 AM

Linux administrators should be familiar with the command-line environment. Since GUI (Graphical User Interface) mode in Linux servers is not commonly installed.SSH may be the most popular protocol to enable Linux administrators to manage the servers

24 Hilarious Linux Commands That Will Make You Laugh 24 Hilarious Linux Commands That Will Make You Laugh Jun 14, 2025 am 10:13 AM

Linux has a rich collection of commands, and while many of them are powerful and useful for various tasks, there are also some funny and whimsical commands that you can try out for amusement. 1. sl Command (Steam Locomotive) You might be aware of the

See all articles