• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

UnixArena

  • Home
  • kubernetes
  • DevOps
    • Terraform
    • Jenkins
    • Docker
    • Openshift
      • OKD
    • Ansible engine
    • Ansible Tower
      • AWX
    • Puppet
  • Cloud
    • Azure
    • AWS
    • Openstack
    • Docker
  • VMware
    • vCloud Director
    • VMware-Guests
    • Vcenter Appliance 5.5
    • vC OPS
    • VMware SDDC
    • VMware vSphere 5.x
      • vSphere Network
      • vSphere DS
      • vShield Suite
    • VMware vSphere 6.0
    • VSAN
    • VMware Free Tools
  • Backup
    • Vembu BDR
    • Veeam
    • Nakivo
    • Azure Backup
    • Altaro VMBackup
    • Spinbackup
  • Tutorials
    • Openstack Tutorial
    • Openstack Beginner’s Guide
    • VXVM-Training
    • ZFS-Tutorials
    • NetApp cDot
    • LVM
    • Cisco UCS
    • LDOM
    • Oracle VM for x86
  • Linux
    • How to Articles
    • Q&A
    • Networking
    • RHEL7
  • DevOps Instructor-led Training
  • Contact

Quick vi editor hacks for DevOps Engineers

November 8, 2022 By Cloud_Devops Leave a Comment

DevOps engineers have to work on a wide range of toolsets to meet the organization’s requirements. In the DevOps role, one tool is going to stay forever to make an engineer’s life simple and more powerful. As the article heading says, it is “vi” editor. The “vi” editor is created by Bill Joy in 1976 for Unix operating systems. Though it has been created 46 years ago, still “vi” editor is one of the most widely used text editors in the world. “vi” editor is mostly found in *nix operating systems and doesn’t feature on the windows operating system. In the DevOps world, engineers more often need to play around with Linux instances, and “vi” editors can’t be avoided.

Once Dennis Ritchie said,

UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.

But this quote is complementing the “vi” editor as well. This editor looks simple but more powerful than you think. In this article, I will share 10 “vi” hacks to make the job easier. These “vi” tips and tricks will increase productivity for sure. This article is intended for Linux/Unix beginners.

To open a file using the vi editor, (vi file_name)

[lingesh@okd4 ~]$ vi redis_ep.yaml
  1. To set line numbers
  • Press Esc to go to command mode.
  • Enter :set nu
vi editor - set line numbering
vi editor – set line numbering

2. To insert a specific character at the beginning of the line.

  • Press Esc to go to command mode.
  • Enter :1,19s/^/ / (To add a space character from line 1 to line 19 at the beginning of the lines )
Insert space beginning of the line
Insert space beginning of the line

3. To insert a specific text at the beginning of specific lines.

  • Press Esc to go to command mode.
  • Enter :10,19s/^/UnixArena/ (To add text “UnixArena” from line 10 to line 19 at the beginning of the lines )
Inserted text beginning of specific lines - vi editor
Inserted text beginning of specific lines – vi editor

4. To search specific text in the “vi” editor.

  • Press Esc to go to command mode.
  • Enter “/” key and type the text which you want to search.
search text in vi editor
search text in vi editor
  • Repeat the previous search using the n command.
  • Repeat the previous search in the opposite direction by pressing the N command.

5. To perform search and replace operation on the current line.

  • Press Esc to go to command mode.
  • Navigate to the specific line where would you like to perform the search and replace operation.
  • Enter :s/OLDtext/NEWtext. In my example, I have replaced “redis” with “redis-new“
  • If you want to replace multiple occurrences on the current line, just /g
    • Ex: :s/OLDtext/NEWtext/g
Search and replace on current line - vi editor
Search and replace on current line – vi editor

6. To perform search and replace operation on the complete file.

  • Press Esc to go to command mode.
  • Enter :s/OLDtext/NEWtext/g. In my example, I have replaced “redis” with “redis-new” on the whole file.
Search and replace on file - vi editor
Search and replace on file – vi editor

7. To perform a search and replace a range of lines in the file,

  • Press Esc to go to command mode.
  • Enter :#,#s/OLDtext/NEWtext/g. In my example, I have replaced “redis” with “redis-new” from line number 16 to 19.
Search and replace on range of lines - vi editor
Search and replace on a range of lines – vi editor

8. How to select the text in the vi editor?

  • You need to enter into visual mode by pressing the “v” command
  • Use the arrow keys to select the texts.
Select the text in the vi editor
Select the text in the vi editor

9. To Convert to Upper and Lowercase or vice-versa

  • Select the text by using the visual mode. (Refer above steps)
  • To convert the text into UPPER case, press “gUU” in command mode
  • To convert the text to lowercase, press “guu” in command mode
Convert the text to UPPER case - vi editor
Convert the text to UPPER case – vi editor

10. To delete the selected lines on the vi editor, simply enter “dd” (delete lines).

  • You can also navigate to the line and press dd to delete the specific line.
  • To delete 5 lines from the file, navigate to the line and press 5dd in command mode.

Hope this article is informative to you.

Filed Under: Cloud, DevOps, Linux, Linux - How to ? Tagged With: Ansible, Cloud, DevOps, Linux, RHEL7

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Follow UnixArena

  • Facebook
  • LinkedIn
  • Twitter

Copyright © 2025 · UnixArena ·

Go to mobile version