In this lesson,
we're going to discuss how you can create and edit text files
on a Linux operating system.
As we start to perform these functions,
we need to take a more high-level approach
to managing the data itself.
By managing files and directories
you're going to be able to quickly create, retrieve,
or otherwise process data
so it provides you with the most benefit
to your organizations
and your day to day administrative tasks.
Now, although you've made some basic file creation
and editing tasks in the past,
it's time for you to become more familiar
with text based tools like Vim and GNU nano.
Almost everything you're going to configure
on your system is going to be done through text files.
So now you need to make sure you have some practice creating
and editing text files in Linux.
Essentially, when I'm talking about a text editor
I'm talking about an application that enables you to view,
create, or modify the contents of different text files.
Text editors were originally created to write programs
in source code, but they're also used to edit
a wide variety of text-based files.
There are many different types of text editors
that are compatible with Linux
but not all of them support formatting options
like a word processor would.
We're going to be discussing in a lot of depth
a couple of basic text editors
that are compatible with Linux.
This includes vi, Vim, Emacs,
gVim, gedit,
and GNU nano.
Now Vi is a visual text editor
and that's how it got its name.
Vi stands for Vi from visual.
Now this was originally created for Unix
but was cloned into free and open source versions
that are also run in Linux.
Now an improved version of this is known as Vim V-I-M.
And this is the default text editor
in most Linux distributions.
Another one we have is Emacs.
Emacs is a flexible, powerful, and popular text editor
that's used in both Unix and Linux.
Another one we can use is gVim.
This is a graphical version of the Vim editor.
And it's used if you have a windowing system
or graphical user operating system.
If you use gedit, this is another simple, yet powerful
GUI-based text editor that's used
in the GNOME desktop environment.
And if you use GNU nano,
this is a small and user-friendly text editor.
Now the first big one we're going to talk about is Vim.
Vim is a contraction of Vi and improved, giving us Vim.
Vim is an extended version of the Vi editor.
Vim implements a text-based user interface
and allows you to do advanced text editing,
and it's favored by many system administrators
and software engineers, because it is highly efficient
and it has the ability to be extensively customized.
Now Vim also includes useful features like text completion,
syntax highlighting, spell checking, and much more.
The Vim command can be called in the command line
to bring up the Vim editor.
When entered without a file name as an argument,
the Vim command is going to open the welcome screen by default
then you type Vim and the file name
if you want to open up a file.
If a file doesn't exist
Vim will create the file with that name
you specified and open it for editing.
Vim supports multiple files
being opened simultaneously as well.
You can also choose to display multiple files
horizontally or vertically.
To do this, you'll click Control + W + V
to create a vertical split
or Control + W + S to split the screen horizontally.
This allows you to have two text files open
and you need to compare what one is versus the other,
you can do that line by line very easily.
Now, Vim is considered a modal editor
and its different modes decide the functionality
of different keys.
The insert mode enables users to insert text
by typing it into the system.
The execute mode enables users to execute commands
within the editor itself.
The command mode enables users to perform
different editing actions using single keystrokes,
and our visual mode enables users
to highlight or select text, to be able to copy it,
delete it and do things like that.
Now the command mode is the default mode of Vim
but you can switch from the command mode
to any other mode by using a single keystroke.
If you want to go to the insert mode, simply press I
and then you can insert text to the left of the cursor.
If you press A, this'll switch to the insert mode
and add text at the end of a line.
If you press capital I, you can switch to insert mode
and then insert text at the beginning of the line
and press a key O to switch to insert mode
and insert text on a new line below the cursor.
If you press the capital O, it'll switch to insert mode
and insert text in a new line above the cursor.
And if you press V it'll switch you
to visual mode to enable selection, one character at a time.
If you press capital V you'll switch to a visual mode
and enable selection one line at a time.
If you press the colon key,
this switches to the execute mode
and enables users to enter commands.
If you press the escape key
you'll return back to the command mode.
In command mode, when you enter the colon operator
a small command prompt section will appear
at the bottom of your editor.
Now this indicates that you are now in execute mode
and can run commands that are supported by Vim.
For example, the colon W and the file name
will actually save the file
or write it with the file name that you're giving it.
If you enter colon Q, this will actually quit the program
and not save any changes that haven't been saved yet.
If you hit colon Q with an exclamation mark
it's going to quit while ignoring any changes that you've made.
And if you hit colon QA, it's going to quit multiple files,
basically quit all.
If you do colon WQ, it's going to write the file first,
then it's going to quit.
If you do colon E exclamation, this is going to revert
to the last saved format without closing the file.
And if you do colon exclamation
with any Linux command, that command will be executed
and the command and the display will result
inside of your Vim interface.
Finally if you need help, you can type colon help,
and the command will open up
Vim's built-in help documentation.
Now motions are considered single-key shortcuts.
They're going to be used to navigate
through the files in command mode.
These key positions are going to move the cursor anywhere
within a document.
They can be used for moving the cursor
through the characters, the words, the lines,
or even huge blocks of text.
By pressing the H key,
you're going to move left one character.
If you press the J key, you'll move down one line.
If you press the K key, you'll move up one line.
And if you press the L key
you'll move to the right one character.
If you want to go to the beginning of the line,
use the Shift and the six key giving you
that upwards caret that brings you to the beginning
of the current line.
If you want to go to the end of the current line,
put the dollar sign in.
If you want to move to the next word, press the W key.
If you want to go back a word, press the B key.
If you want to move to the end of the current word
or to the end of the next word,
if you're already at the end of your word,
just press E for end.
If you press Shift + L this'll move the cursor
to the bottom of your screen.
And if you use Shift + H it's going to move the cursor
to the first line of your screen.
If you press line number and Shift + G you'll move directly
to that specified line number.
If you press GG, you'll move the cursor
to the first line of the file.
If you press Shift + G by itself
it'll move the cursor to the last line of the file.
Now, in addition to using the H, J, K, and L keys
to move side to side or up and down,
you can also use the up, down, left, and right arrow keys.
You also can use conventional navigation keys
like Home and Page Up and Page Down inside of Vim.
When you're doing editing operations in command mode,
this is really some powerful things you can do
to manipulate the text with really simple keystrokes.
And it really shows the power of Vim.
You can also use these things in combination
with other motions to edit multiple characters at once.
The X operator is going to delete the character selected
by the cursor, where D is going to delete the text.
DD will delete the entire current line.
P will paste the text on the line below the cursor.
Capital P will paste the text on the line
directly above the cursor.
The forward slash with a text string will search
the document for that specific text.
And a question mark with the text string
will search backwards through the document
for specific text as well.
If you press Y that'll copy the text,
and YY will copy the line directly above the cursor.
If you type C, a range of lines, and another C,
it's going to begin a change in the specific range,
and U will undo your latest change.
Capital U undoes all changes in your current line,
and capital ZZ will write the file
only if changes were made,
and then it'll quit the Vim editor.
Now a count is a number that multiplies
the effects of keystrokes within Vim.
It can be used in combination with motions,
operators, or both.
When used with a motion, cursor movement is multiplied
according to the count specified.
When using editing operations, the action gets repeated
the number of times specified.
Now another user-friendly text editor
that evolved from the Pico text editor
that was created for Unix-like systems is nano,
also known as GNU nano.
While Vim is a powerful text editor,
it is not really the most user-friendly
as evidenced by its multiple modes, its bare interface,
and all those key stroke commands that I just listed.
Know those are really that intuitive.
Now on the other hand, the nano editor
is much more visually helpful in that it displays
its command shortcuts at the bottom of every open file.
It also doesn't have different modes
that you need to switch between.
Despite these advantages though, nano does lack
many of the features that makes Vim so powerful,
like the split screen, text completion, syntax coloring,
and some other features.
The nano command can be called
if you'd want to use it by just typing in nano
at the command prompt.
Automatically the command will open a new file for editing
and you can later save this file with a specific name.
You can also type nano and the file name
if you want to open an existing file.
If the file doesn't exist, nano will create that file
using the name you just specified
and open a blank file for you to edit.
Like Vim, nano supports multiple files
being open simultaneously.
In GNU nano the functions you use
will work with text editors and the editor itself
are going to be referred to as shortcuts.
Most of these are going to be activated by pressing
the Control key, which is represented
by the upper caret symbol or the Shift + six key
inside the editor.
Then you're going to press the key that corresponds
to the function you're trying to perform.
For example if I saw the upward caret G
that would actually be used as a Control + G shortcut
and it's used to open the nano help screen.
If I use Control + X that's going to exit nano
and close the current buffer.
If I use Control + O it's going to save the currently open file
or it's going to save the output.
Control + J is going to be used
to justify the current paragraph.
Control + R is used to insert another file
into the current one at that position.
Control + W is used to search the file.
Control + K is used to cut the currently selected line.
Control + U is used to paste that line that you just cut.
Control + C is going to be used to display
the cursor's position.
Like other text editors,
you can navigate nano using your arrow keys,
your Page Up, Page Down, Home and End keys,
and other things like that.
If you're missing any of these keys,
nano also provides shortcuts for those,
such as Control + V to go to the next page,
or Control + Y to go back a previous page.
Copying parts of the text on a line also requires you
to mark that text that you want to copy,
and you do that by using the Control
and the upper caret symbol.
Then you're going to navigate your cursor
to highlight the text you want to copy
and press Alt and that upper caret symbol.
This will mark or highlight that text,
and then you can use Control + U to paste it
into another location.
Another program we can use is gedit.
Gedit is a text editor that is the default text editor
inside the GNOME desktop environment
which is a graphical user interface.
And it's a member of the GNU project.
Unlike Vim and nano, gedit uses a graphical user interface.
And it has a typical menu-based design
that makes it really easy to work with.
If you use notepad in Windows or text edit in a Mac,
you're probably pretty familiar
with using something like gedit.
It also has features like syntax highlighting
and spell checking and it can be customized
through different plugins.
Although you can launch gedit from the desktop,
you can also launch it from the command line interface
using the gedit command.
But again, you have to have a graphical user interface
installed on your Linux system
if you want to use gedit.