Table of Contents
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.
It will create a test.php file at your specified location.
To create a folder
Insert data into File
Open file (test.php) to work in vi editor by this command.
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
It will go at line number 10 of file.
View File Contents
Open terminal and type this command
$ cat <file-name>
$ 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
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.
Hi, I am Sanjay the founder of ONLINE WEB TUTOR. I welcome you all guys here to join us. Here you can find the web development blog articles. You can add more skills in web development courses here.
I am a Web Developer, Motivator, Author & Blogger. I also used to take online classes including tech seminars over web development courses. We also handle our premium clients and delivered up to 50+ projects.