This article talks about the basics of text-editing in Ubuntu using Gedit and VIM, and its commonly used commands.
For more on this subject, please refer to the SOC Table of Contents.
Gedit
As mentioned before, Gedit is the Ubuntu equivalent of the Windows Notepad. We can use it to edit text documents. Another editor is the VI/VIM editor.
VI/VIM
We can use VI/VIM to edit documents directly from the Terminal. VIM is an upgraded version of VI, with more features and is easier to use. You may think of this as Windows 10 vs Windows XP.
First, we would need to download VIM. We can do this by using what we just learned about APT and execute the following command.
sudo apt-get install vim
After the installation of VIM, we can start using it. There are 2 main work modes in VIM: Insert and Command. Each mode has different working functions. Here, we will create and edit a document as an example.
First, execute the following in the terminal.
vim test.txt
Which will create and open a new text file named “test”.
You can try to type in some numbers or some letters, and you may find that it’s not as straight-forward as the Microsoft Notepad.
There are a few commands that are needed in order to use VIM efficiently.
i Changes all that is before where the cursor is to insert mode.
I Changes the first entry of the line that the cursor is on to insert mode.
a Change all that is after where the cursor is to insert mode.
A Change the last entry of the line that the cursor is on to insert mode.
o Create a new line under where the cursor is and set that line to insert mode.
O Create a new line above where the cursor is and set that line to insert mode.
s Delete the character that the cursor is on.
r Replace the character that the cursor is on.
Note that the most commonly used command is a. After we press a in the terminal, notice that the bottom left of the terminal now reads “INSERT” to show that we are now in Insert Mode. We are now able to freely type what we want.
Note that hitting [Ctrl] + S doesn’t save the document, but rather pauses the terminal. Any changes that you make at this time won’t show up. You may resume this by hitting [Ctrl] + Q.
In order to save the document, as well as control and manipulate it, we need to first transition into Command mode. We first exit Insert mode by pressing [esc]. You will notice that the INSERT prompt on the bottom left is now gone.
In Command mode, the most common controls are as follows:
Cursor Control
h / [.left arrow key] Move cursor left by one character.
l / [.right arrow key] Move cursor right by one character.
j / [bottom arrow key] Move cursor down one line.
k / [up arrow key] Move cursor up one line.
Where n is the number of lines,
nG Move cursor to the start of line n.
n+ Move cursor down by n lines.
n- Move cursor up by n lines.
Screen Control
[Ctrl] + f Go to next page.
[Ctrl] + b Go to previous page.
Copy Paste and Delete
cc Delete the entire line and go into Insert mode to start editing.
dd Delete the entire line, but don’t edit.
ndd Delete the entire line, as well as n lines under it.
x Delete the character that the cursor is on.
X Delete the character that is to the left of the cursor.
nyy Copy the entire line, and n lines under it.
p Paste what was copied.
Saving
If we want to save the document, we will need to first type in “:”, which shows up on the bottom left of the window, and type in a command after it.
The most common ones are:
x Save the document and quit.
q Quit without saving.
w Save the document.
q! Quit VI/VIM and don’t save.
If you want to search the document, you may do so by typing in “/” in command mode.
Next, type in what you want to search in the bottom left of the window and hit [Enter].
The cursor should jump to instances of where the search is found, as shown on the next page.
Now we will choose to save the document and quit like so:
After we quit, we can use the cat command to view the contents of the document we just created.