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.
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 : 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.
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.
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.
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 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 :
- At the bottom bar we see a list of commands with various available options:
- As mentioned earlier the most important command is the
exit
one. To do so, we press^x
. The^
represents theCTRL(Control) command
.This will make nano exit entirely.If we want to re-open the file, we have to press theup arrow
for the last command(nano textfile.txt
) used and thenenter
. - 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.
VIM
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 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
Typing vim
will make the startup screen appear and look something like this
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
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
Extra Reading 📚
- One article on exiting vim that caught my eye by it's title was this one by Quincy Larson.
- On vim editor modes
Learn Interactively
- vim adventures
- open vim
- vim genius
- vim for VSCode
- we can type
:help
from thecommand mode
and it'll start the tutor or if we typevimtutor
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.😃
Top comments (14)
Nice article!
I never really understood why the learning curve of Vim is considered steep. It's different than modern editors for sure, but learning it is not that bad. It took me two weeks to be able to modify any text file I wanted, and maybe three weeks to one month to ditch my IDE.
Oh and you can type
vimtutor
anywhere, not only from the home directory.Thanks for reading and for the tip!
"First instinct was to panic and then close the terminal window." 😂 So relatable! 😅
I feel as if I am repeating myself here, but that was such a great article again, Denise! I wish you would have been around three years ago already, you would have saved me so many headaches! 😄 I love the way you approach these articles, you deliver exactly the right information in such a beautifully humorous and concise way.
(Ugh, I can already see how if you keep going down this path of vim exploration, you will take me right with you into that rabbit hole again.. 😅)
Thank you for this! Truth be told, I always get confused with nano, so this actually really helped and I learned some new things again 😍 You're a great tech writer!
Heh. I'm the other way round. On the few occasions I use a GUI editor I find them so complicated, so hard to navigate. If anyone can write a guide to VS Code for old Unix grey-beards I'd love to read it!
Yes, I can also relate to that too. VSCode can be pretty intimidating too at times, especially if you haven't used it before. I'm definitely going to be writing about it in the future :)
Emacs FTW!
I believe that's XEmacs. Without doing any research ;), I think emacs came first as a terminal editor, then it was adapted to a GUI.
Exiting Vim - Philanthropy, save someone's psyche)
I'm new on Linux environment and just start exploring centos on cloud, maybe I should try vim.
I know I should vim more.
The nano gang
Also checkout Micro micro-editor.github.io/
Just cloned it!
I already tried it before, but it didn't recognize my terminal, kitty.
This is now fixed.
If I can use it with a language server, I might switch (of course nano can't, but that's ok for vim/neovim).
vim is ok i only use it on my server but use IntelliJ for EVERYTHING ELSE
Helpful article, but so many animated gifs made it difficult to concentrate on the text.