How to Work with vi Editor – Ubuntu

Reading Time: 4 minutes
3,315 Views

Working with Unix environment provides several editors which we can use via Terminal or Shell. Inside this article we will see about VI Editor of Unix.

The VI editor is the most popular and classic text editor in the Linux family.

It will be interesting to know How to work with vi Editor. To understand the things in a very clear way. We will create a .php file and work with vi editor for Insert, Save, Search, View operations.

This article will give you the following concepts –

  • Create file from terminal
  • Insert data into file
  • Search for any specific keyword
  • View file
  • Quit from vi editor panel

Let’s get started.


Create File from Terminal

Open your directory path in terminal and type this command.

$ touch test.php

It will create a test.php file at your specified location.

To create a folder

$ mkdir <folder-name>

Insert data into File

Open file (test.php) to work in vi editor by this command.

$ vi test.php

We can see, there are ~ signs. It indicates the unused lines in that file. Let’s insert some data into it.

To insert data into file – type ‘i’

After pressing i button, it will now allow to insert data into it.

Write some content like

This is sample content for this file

To save file, press ESC button (exit from insert mode) and then type :wq (save and exit from vi mode)

To see the contents of file, use this command.

$ cat test.php

It will display the contents what the file contains.


Search for Keyword in File

To search any keyword or content. Follow these steps –

  • Open file – vi sample.txt
  • Press ESC (button)
  • Type – /search-keyword & Hit Enter
  • To search all occurrence of /search-keyword, Press “n”

To go to Specific Line number

  • Press ESC
  • Type – :<Line Number>
  • Hit Enter
:10

It will go at line number 10 of file.


View File Contents

Open terminal and type this command

$ cat <file-name>

Example –

$ cat sample.txt

It will display file contents to terminal.


Quit From vi Editor

To quit from vi editor mode.

  • Press ESC
  • Type – :q
  • Hit Enter
:q

It means quit from vi editor mode.

We hope this article helped you to learn How to Work with vi Editor – Ubuntu in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more