How-To Guides and Blog

How to Create, View and Edit text files in Linux

NBH Support
NBH Support
No Comments

We have talked about using the touch command to create text files, but they are other ways in which you can create and edit text files from the command line outside the touch command. One of the importance of text files in Linux is that it can be moved and shared between systems without requiring conversion and can also be viewed and edited with any text editor.

Creating and editing text files is inevitable as a system administrator, and as such, they are various text editors available today. One of these is a vim text editor. Vim can be used to create, view, and edit a text in the command-line interface of a Linux system. Vim is an improved version of previously vi editor which is widely used by Linux and UNIX systems. If you are very good with your keywords, vim should be the best way to edit your textfiles. Another command for editing and creating text from the command line is the cat command with the standard Output redirection symbol (>). It is useful when you immediately want to add some text to your files.

So, in this article, you will get to know more about vim text editors and how to use the cat command to create files


The cat command is typically used to view the content of a text, but it can also be used to create textfiles. Enter the cat command followed by the output redirection key > and the name of the text you want to create, press enter and type your text on the shell. Once done, press Ctrl+D to save your files. Note that, creating a text file in a location with the same text name, will force the command to overwrite the text with the newly created one

, How to Create, View and Edit text files in Linux, New Blog Hosting, New Blog Hosting


Every system administrator has one or two text editor of choice. They are various file editors aside vim; like emacs, nano, and gedit. Your preference on text editor shouldn’t stop you from understanding the basics of vim because vim is an editor you can also find in all Linux systems

To start editing file content with vim command, enter the command vim and the filename. If a filename already exists, then you will be able to view it but if no filename of such in the current working directory, the vim command creates a new file.

When first open, vim command starts in the command mode. In vim command mode, you won’t be able to edit a text, but you can view, navigate, cut and paste, and other text manipulation.

There are three modes in the vim environment. To switch between modes, you
can enter single character keystroke:

The character “i” will switch to insert mode where you can input or edit your text. Pressing ESC key will return to command mode. The character “v” will switch to a visual mode where multiple texts can be selected for manipulation.


Unlike other operating systems, vim uses a different term to describe copy and paste. Copy in vim means yank, and paste means put. This can be represented on the keyboard as y for copy and p or P for paste.

Pasting is done with the p or P letter. The lower case p will paste after the current cursor or below the current line. Upper case P will paste before the current cursor or above the current line.

                             HOW TO DELETE IN VIM EDITOR

To delete text, we use the letter d. The letter d deletes the entire text in a line. To delete from the cursor to end of the line, we use D. to delete just a character under the cursor, use letter x.

Please note that all these text operations can only be done while in the command mode. If you are making all these operations in the insert mode, the editor will assume you are typing a text.

                             HOW TO MOVE YOUR CURSOR

To move the cursor, press the h,k,l,j.

The letter h will move your cursor to the left

The letter k moves your cursor to the top of the text

The letter l moves your cursor to the right

The letter j moves your cursor to the bottom of the text.

Please use the “:help” command in the vim editor or man vim on the command line to see more keys and their functions.

                                HOW TO SAVE FILES IN VIM EDITOR

When you are done with text manipulation in the insert mode, you have to press the ESC key to leave the insert mode to the command mode then; you have various options of letters: to either quit, quit without saving changes, save changes and save/quit the vim.

Enter :w to save changes

Enter :q! to quit without saving changes

Enter :wq or 😡 to save and quit vim


They are so many keys with various functions necessary to navigate through your text in vim editor, and it is impossible to keep all in one article. So this is why the article provided a self-help guide on the keys available to use while using vim editor

To learn the key functions, press “:help” while in the command mode; this will display the first screen, which includes the help needed to work in vim environment. You can also enter “:q” to close the help screen.

There are other ways to get help from the command line, which shows how to work around on vim environment. From the command line, enter vimtutor; this will lunch a guide that takes a new user through the basics on how to use vim. Also, you can go through the vim manual page (man vim)


Output redirection changes the destination of a file with filenames representing either output files or devices. Using redirection will send the output or the error message to a file, the terminal, or a device.

Please note that output redirection is not regarded as a text editor; instead, it just creates a file when it doesn’t exist. If the file does exist and the redirection is not one that appends, it will overwrite the content of the file. The output can be sent to a special file /dev/null, which discards every message passed to it.


The order of redirection is essential; the following operators redirect standard output to a file to append or overwrite the content of the file or redirects to /dev/null and discard the message.

>file: This will redirect the standard output to overwrite content in an existing file

, How to Create, View and Edit text files in Linux, New Blog Hosting, New Blog Hosting

You noticed from the command line the content of “file.txt” and “file2.txt”. When the command is invoked, the content of “file2.txt” was overwritten with content of “file.txt.”

>>file: This will redirect the standard output to append to a file

, How to Create, View and Edit text files in Linux, New Blog Hosting, New Blog Hosting

The content of file.txt append to the content of file2.txt

2>file: This will redirect the standard error message to overwrite a file

, How to Create, View and Edit text files in Linux, New Blog Hosting, New Blog Hosting

The journalctl command will display an error message because of the absence of –p option. Using this redirection operator will redirect the error message to a file.

2>/dev/null: This will redirect standard error messages to a special file /dev/null; telling it to discard the error message

, How to Create, View and Edit text files in Linux, New Blog Hosting, New Blog Hosting

The error message is redirected to a special file called /dev/null. Telling it to discard the message

>file2>&1 or &>file: it will redirect the standard output and standard error to overwrite in a file

, How to Create, View and Edit text files in Linux, New Blog Hosting, New Blog Hosting

As seen in the command, the error message is being redirected to overwrite a file called “file.txt.”

>>file2>&1 or &>>file: it redirects the standard output and standard error to append to a file

, How to Create, View and Edit text files in Linux, New Blog Hosting, New Blog Hosting

          The error message is redirected to a file and append to it


It is worthwhile to mention that most advanced vim users tend to offer shortcuts before the basics are mastered. Vim requires constant practice before you can become proficient in it. It is important you keep learning the necessary keys to use vim editor.

Please, all the necessary keys and further information to kick-off your learning on the keystroke is available at the manual page of vim (man vim) and also by typing “:help” at the command mode of vim editor. You can also enter the vimtutor on the command line to view further information on how to use vim editor.

I guess you should be wondering why the system arrow key is not used in place of the hklj keystroke. There is this habit that when you are not using the recommended keys for vim editor, you probably doing the wrong thing. Yes!, that could be true because you are advised to get used to your keys before you think of breaking the rules. I feel it goes down to doing what is convenient and keeps your hand free


Vim(1) man page

Red Hat Enterprise Linux 7 System Administration II Course

Cat (1) man page

Leave a Comment