Create, Edit or Read File Content (Cat, Nano n Vi)

  1. cat — Use the cat command to display the contents of a file. It is usually used to easily view programs.

2. Nano:  Learning Vi and Vim text editor could be quite intimidating for the new Linux user. Thus most prefer to use Nano- powerful and user friendly text editor. In most of the Linux Distro Nano came pre-installed. 

To check if Nano is installed or not use following command

nano –version

To install Nano on Centos use following command 

$ Sudo yum install nano

To create new file on nano under current directory 

$ nano filename

Once open it will look like this

Use these commands for specific purposes

crtl + O : save file

Crtl + x : to exist nano

ctrl + k: to cut text

ctrl + j: to justify

3. Vi text editor

Vi is a terminal application, you can start it from a terminal window using following command

$ vi testfile 

Command Mode:

Once the testfile is open you will find Vi in Command Mode. In this mode you cannot insert any text or word.

While in command mode, you can move the cursor around with the arrow keys. Press the x key to delete the character under the cursor. There are a variety of other delete commands — for example, typing dd (press the d key twice) deletes an entire line of text.

You can select, copy, cut and paste text in command mode. Position the cursor at the left or right side of the text you want to copy and press the v key. Move your cursor to select text, and then press y to copy the selected text or x to cut it. Position your cursor at the desired location and press the p key to paste the text you copied or cut.

Insert Mode:

Aside from command mode, the other mode you need to know about is insert mode, which allows you to insert text in Vi. Entering insert mode is easy once you know it exists — just press the i key once after you’ve positioned the cursor in command mode. Start typing and Vi will insert the characters you type into the file rather than trying to interpret them as commands.

Saving and Quit: 

You can save and quit vi from command mode. First, exist insert mode by pressing ESC key. 

Then type  :wq and press enter to write the file to disk and quit vi. You can also split this command up — for example, type :w and press enter to write the file to disk without quitting or type :q to quit vi without saving the file.

 

 

Leave Comment

Your email address will not be published.