Don't fear the command line: Text Editors
Tags: codenewbie, beginners, vim, linux
Published: 2020-09-10 14:20:23

One topic that can be pretty intimidating when learning the command line is text editors. Their interface looks different and they have all these new commands that we need to use in order to enter text, make changes to the text and save all of our work. What I find the most scary part and where  I fear that things could go wrong, is not knowing  how to exit those editors.So I make sure that is the first command I learn.

![trying to find a way out](https://media.giphy.com/media/3WdUt1PX5mlSo/giphy.gif)

So in this post I'll give a basic overview of two of the most frequently used and  most popular command line text editors in Linux and [Unix like systems](https://en.wikipedia.org/wiki/Unix-like) : nano and Vim. My goal is to show you how to do basic editing and saving.

## Text Editors. What is that?

Text editors are programs used to edit files. Those files could be text files we have written ourselves or our shell's startup and configuration files where we could make certain changes if we wanted to. So, text editors can be similar to word processors like Google Docs or Microsoft Word in a way, as they allow us to edit words on a screen and move the cursor around.
The main difference of text editors with word processors is the fact that text editors only support pure text and  they contain features that allow us to write and run programs.

![Hermit the frog typing](https://media.giphy.com/media/XIqCQx02E1U9W/giphy.gif)

### Types of  text editors

There are two basic categories when it comes to  text editors. Those are **Graphical GUI** editors and **Text Based** command line editors .
 Some of the most popular **graphical** editors in Linux are KDE and GNOME. GNOME comes with a default  editor called *gedit* or if we search in the GNOME menu it's called *'text editor'*.KDE comes with three, which are : *kedit*,*kwrite* and *kate*. Graphical editors have the advantage of a friendlier user interface.
Now for the **text-based** console ones, the most popular are *nano*,*vi* and *emacs*. Emacs is not usually installed on most Linux Systems by default.
It's important to note that there are so many editors available  to choose from and Linux Distributions have several already installed.
![so many possibilities](https://media.giphy.com/media/giEftvZuPAiZQBphEI/giphy.gif)

### Their Purpose

It is useful to know how to do the most basic text editing in the command line as there will be times where the command line will just throw us into a text editor without any warning. So it's important to know how to get stuff done when these unexpected events happen.Nano and/or Vim are  most likely to be installed by default on your system and you may be thrown in one of these accidentally.
For example ,I was learning about the *less* command, which allows us to view the contents of files(`less filename.txt`). I could view the contents but at some point wanted to edit something out, pressed `v` on my keyboard to start editing but ended up thrown in nano ,a completely new environment.First instinct was to panic and then close the terminal window.

![where am I?](https://media.giphy.com/media/Bp3dFfoqpCKFyXuSzP/giphy.gif)

It can look intimidating at first sight. So my goal was to learn basic text editing and saving of my work.

## NANO

As a bit of background history, [nano](https://en.wikipedia.org/wiki/GNU_nano) was designed as a replacement for the *pico* text editor that was supplied with the PINE email suite therefore it is quite short on editing features. Nano is a tiny sized open source editor, light weight, fast and included by default on practically every Linux and Unix-like system.

- To get started make sure you are in your home directory(if not type `cd ~`).
- Type the name of the editor followed by the name of the file you want to edit.If the file does not exist already, the editor will create it. So for example type `nano textfile.txt` to create an empty text file in your home directory. Once nano starts you will see a screen like this :

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/050cw2pfo3ltxwoiuk45.png)

- At the bottom bar we see a list of commands with various available options:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lx7u2na4kzg1sxgl8w57.png)

- As mentioned earlier the most important command is the `exit` one. To do so, we press `^x`. The `^` represents the `CTRL(Control) command`.This will make nano exit entirely.If we want to re-open the file, we have to press the `up arrow` for the last command(`nano textfile.txt`) used and then `enter`.
- The "write out" option means to save the file, so use the `^o` command.
- We will be prompted about the filename at this point, so we press `Enter`.

With that knowledge we can start typing.

![monkey](https://media.giphy.com/media/f5BwvEFBcgzU4/giphy.gif)

## VIM
![hole](https://media.giphy.com/media/l378ycV5Pt6ysGsTe/giphy.gif)

When you first come across Vim, it is for sure much scarier looking than nano ,the interface is more intimidating to say the least.The  learning curve can be steep  and it feels like more could go wrong, maybe even feels like you are trapped there forever, at least I did at first. After reading a bit about it, people either love it or hate it and have quite strong opinions about it. I would like to invest some of my time to learn  this tool in a bit more depth and customise it in different ways but to get started you only need the absolute basics.

Initially there was the [vi](https://en.wikipedia.org/wiki/Vi) editor which has been replaced by a program called `vim` ,short for ` vi improved`.It's an enhanced replacement,  almost always available on Unix systems, lightweight and allows for fast typing speed.There are many improvements over the traditional `vi` editor and it is easier to use with more features.If we run `vi` on macOS or Ubuntu it just runs `vim`.

### Openning Vim

![you can do it](https://media.giphy.com/media/26tk1VprH3MKCW4eY/giphy.gif)

Typing `vim` will make the startup screen appear and look something like this 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0k97flhjy14i3vfhv9et.png)

Thankfully, it let's us know how to exit that, by typing `:q`.

Now, to open or create a file in `vim` from our home directory, we can type `vim textfile.txt`. If it exists already it will open it, if not it will create a new file called `textfile.txt`.

Vim has different `modes` and the `default` one is the `command mode`.That is what we'll see when we first open a file in vim. Do not type in this mode as it will just mess things up and nothing that you typed will appear. We need `insert mode` and we achieve this by typing `i`. The bottom of the screen will look like this 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/em11c4nendqgbynr0awt.png)

This lets us know that it's ok  to type now, so we can add the text we want.In this case, my file is a new one and therefore empty, those tildes at the side `~` represent empty lines.Once done with  writing ,hit `esc` and it's back to `command mode`, the `insert` at the bottom of the screen will disappear.

### Exiting Vim 😨

After we have made changes to our file, finished our work, and are in `command mode` , the next step is to save what we have done.We do that with `:w`. Then we can quit without a warning showing up, with `:q` as we saw before.That can be also done in one step with `:wq`.We save and exit at the same time.

`:q` does not work when trying to quit with unsaved changes.If we made changes to a file but don't want to save, just exit then we do `:q!` for quitting without saving.

There will be times when `:q` doesn't work and to exit vim *no matter what* we type `:qa!`. This basically says 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/iwasuq0549o13665a6nu.jpeg)

### Extra Reading 📚
- One article on exiting vim that caught my eye by it's title was [this one](https://www.freecodecamp.org/news/one-out-of-every-20-000-stack-overflow-visitors-is-just-trying-to-exit-vim-5a6b6175e7b6/) by Quincy Larson.
- [On vim editor modes](https://www.freecodecamp.org/news/vim-editor-modes-explained/)

### Learn Interactively
- [vim adventures](https://vim-adventures.com/) 
- [open vim](https://www.openvim.com/)
- [vim genius](http://vimgenius.com/lessons/vim-intro)
- [vim for VSCode](https://vimforvscode.com/)
- we can type `:help` from the `command mode` and it'll start the tutor or if we type `vimtutor` from our home directory before entering vim there are some interactive lessons there.


This was a very basic overview of nano and vim and how to get started using them.Thanks for reading.😃