By Brandon DuongFebruary 11th 2021

In this part of our Ubuntu Linux tutorial, we will go through some more of the common level commands, their usages, as well as being introduced to various text editors.

When working in the command line, you will often find yourself editing small snippets of text in text files, so it would be good to make sure that we have a text editor handy and ready to go. One of the most commonly used text editors for Linux distros is Vim and GVim. Vim and GVim are roughly the same in terms of usability and efficiency, however, Vim is designed to be used without a mouse for efficiency while GVim allows for usage of the mouse pointer to control your cursor position. Additionally, Vim runs straight from the command-line window while GVim makes use of a Graphical User Interface (GUI) as well as enhanced toolbar support to make the user experience a bit more intuitive. 

Installing Vim and GVim

The installation process for Vim and GVim is quite simple. While in a terminal window, input the following command: 

sudo apt install vim-gtk

The installation process should begin and end fairly quickly. Let's try it out! The syntax to create and edit a text file in Vim/GVim is: 

gvim *filename*

In our case, we'll go ahead and name our file test.txt. 

In order to actually type/edit the text file, we must be in Insert mode by pressing the "i" key. If you want to exit Insert mode, press the "esc" key, which puts us in Command mode, which is necessary for us to be in in order to execute commands in Linux.

Notice the "Insert" in black letters near the bottom, notifying you that you are currently in Insert mode.

In order to save and exit the file, input the keys ":" + "w". When working with vim, key combinations involving ":" are called exit commands. The notable ones are:

- :wq , this will save the file and quit the editor

- :q , this will attempt to quit the file, but will also give you a warning first, where you must press "y" in order to acknowledge that you are quitting the file without saving 

- :q! , this will quit the file without the prompts. Be careful! 

While in Command mode, here are some common Vim/GVim commands that you might find useful as well:

- dd , delete the whole, current line

- yy, yanks(cuts) the whole, current line

- p, paste above the cursor

- P, paste below the cursor 

- u, undo the last change

For commands like yank and delete, you can prepend a number to them in order for that command to execute the specified amount of times. For example:

- 5yy will yank 5 lines, and 5dd will delete 5 lines

Redirection

The terminal outputs a lot of information, some of which you might want to store for later usage. Luckily, the Linux kernel includes a tool, redirection, specifically for this purpose. There are two types of redirection: Input and output redirection. We'll go over some examples of both to see how they work, and how they might be useful

Output Redirection:

Sometimes, we'll have an output that we might want to save or a large amount of output that clutters the terminal screen that we don't want. We can instead direct that output into a text file. Our main tool for using this will be the ">" sign. The template for this will be:

command > file.txt

For example, if I wanted to redirect the output of 

ls -a

I would instead input

ls -a > output.txt

Output redirection also has an additional operator, ">>", which allows you to append to a file instead of overwriting/making a new file. 

Input Redirection

Just like we can have our output redirected, we can also redirect our input. The template is similar for output redirection:

command < file.txt

Note that input redirection is a bit more niche. For example, the command

sort < file.txt

will accomplish the same thing as the following command:

sort file.txt

As you become more experienced with Linux and learn more commands, you'll be able to see how to input redirection can be utilized in order to improve efficiency 

Combining Input and Output Redirection

Now we can combine them! The template for using both input and output is as follows: 

command < input.txt > output.txt

Note that the order for "<" and ">" does not matter, but it does matter that these operators come after any flags that you're using in the command. 

The "echo" Command

The command "echo" is used to print is a line of text/string that is passed as an argument to the terminal as standard output. 

Some common flags are:

-n ; omits the trailing newline character, "\n", that comes after inputting a string. 

-e ; allows the kernel to interpret backslash escapes

Backslash escapes, such as \n, \a, and so on are often hidden characters that help format a string. For example, \n is the new line char. These backslash escapes can be inserted into various strings in order to produce formatting effects. Some examples are "\c", which suppresses output past the backslash character, and "\n", which forms a new line.

The "Cat" Command

The cat command, short for "concatenate", is often used to display the contents of files. Some example usages of cat are:

Displaying a single file

cat file1.txt

Displaying multiple files

cat file1.txt file2.txt file3.txt

Displaying a file with the line numbers

cat -n file1.txt

Redirecting the contents of a file into a new file and appending it to a new file

cat file1.txt > file2.txt
cat file1.txt >> existingfile.txt

Redirecting the contents of multiple files into a single file

cat file1.txt file2.txt file3.txt > file4.txt

Summary

The usage of these more niche but still common commands as well as being familiar with text editors are the foundation of being able to efficiently use the terminal. That's all for the moment, good luck on your journey through Linux! 

)