Time to study: ~60 min
You will learn: nano for when u just want to edit a file, and vim for when u want to be fast, including the modes idea that makes vim make sense.
← Chapter 5 | back to Terminal 101 | next: Chapter 7 →
1. why we need an editor at all
until now, every time we wanted to see what was inside a file we used
cat,less,headortailfrom Chapter 3. all of those only read the file.
and for writing into a file there is
>and>>, which are coming in Chapter 8. they do work, but try writing a 40 line script withechoand>>and u will see the problem after the third line.
so we need something in between. something that opens the file, lets us move around inside it and change any line we want, and saves it. that is a text editor.
nano,vimandemacsare the most common ones u can use in the terminal.
nano notes.md
vim notes.md
emacs notes.mdall three take the file name as the argument, and if the file doesn’t exist yet they create it for u when u save.
- u will meet these on a server one day where there is no GUI at all, and then this chapter stops being optional.
- we will do
nanoandvimhere.emacsis a whole world of its own and u don’t need it to get started.
2. nano, the easy one
nanois the one to start with. it works the way u expect: u open it, u type, the letters appear. there is no mode and no trick.
nano notes.md GNU nano 7.2 notes.md
hello, this is my file.
i can just type here like a normal editor.
^G Help ^O Write Out ^W Where Is ^K Cut
^X Exit ^R Read File ^\ Replace ^U Paste
look at the bottom of the screen. that is the whole reason to use
nano, it tells u the shortcuts while u are working, so u are never stuck.
the
^means thectrlkey. so^Xmeansctrl + x.
ctrl + o: save the file. it asks u the name, press Enter to keep it.ctrl + x: exit. if u have unsaved changes it asks u first, pressythen Enter.ctrl + w: search for something.ctrl + k: cut the line u are standing on.ctrl + u: paste it back.- the arrow keys move u around, exactly like u expect.
that is really all of
nano. u already know it now.
3. vim and the modes idea
now
vim. the first time everyone opens it, they type some letters, weird things happen, and then they can not even close it. so before any command, u need one idea:
in vim, ur keyboard does two different jobs, and u switch between them.
in a normal editor the letter keys always type letters. so to do anything else, like copy or delete a line, u need
ctrlor the mouse or a menu.
vim made a different choice. most of the time u are not typing, u are moving around and changing things. so vim gives the letter keys to those jobs, and typing becomes the thing u switch into.
these are the modes:
- normal mode: where u start. the letters are commands, not text.
ddeletes,ycopies. this is where u live. - insert mode: the letters are letters. this is a normal editor. u get here with
i. - visual mode: for selecting text. u get here with
v.
and one key gets u back to normal mode from anywhere:
Esc.
- if u are ever lost, press
Esc. press it twice, it costs nothing. u are back in normal mode and u can start again.
getting out, first
this is the part that traps everyone, so we do it before anything else.
press
Escfirst, always. then:
:q quit. only works if u changed nothing
:wq write and quit. this is the normal one
:q! quit and throw away everything u changed
:w write, but stay in the file
the
:puts u on the last line of the screen where u type the command, and Enter runs it.
:q!is the escape hatch. u opened a file by accident, u typed junk into it, u want out with no damage.Escthen:q!and u are safe.
typing something
vim notes.mdu are in normal mode. press
iand look at the bottom of the screen:
-- INSERT --
now type whatever u want, it works like any editor. when u are done, press
Escand the-- INSERT --disappears. u are back in normal mode.
there is more than one way in, and the small differences save u a lot of time:
i: insert before the character u are ona: insert after ito: open a new line below and start typing on itO: open a new line aboveA: jump to the end of the line and start typing there
oandAare the two u will use the most, once u notice u keep pressingithen moving to where u actually wanted to be.
moving around
in normal mode, these move the cursor:
h left j down k up l right
the arrow keys also work, so don’t fight this on ur first day.
hjklis faster later because ur hand never leaves the middle of the keyboard, but it is not the point right now.
the ones that actually save time:
w: jump forward one wordb: jump back one word0: start of the line$: end of the linegg: top of the fileG: bottom of the file5G: go to line 5
ggandGare worth learning today. jumping to the top or the bottom of a 500 line file instantly is the moment vim starts to feel good.
changing things
all of these are in normal mode, no
ctrlneeded:
x: delete one characterdd: delete the whole lineyy: copy (“yank”) the linep: paste it after the cursoru: undoctrl + r: redo
and now the actual idea behind vim. these commands take a number and a target, and u can combine them freely:
dd delete a line
3dd delete 3 lines
dw delete a word
d$ delete from here to the end of the line
yy copy a line
3yy copy 3 lines
so u are not memorizing a list of shortcuts. u learn
dmeans delete, u learnwmeans word, anddwis free. u learnymeans copy, and3yyis free too. that is why people who use vim get fast, and it is the only reason it is worth the ugly first day.
searching
same as
grepfrom Chapter 4 but inside the file u have open:
/hello search forward for "hello"
n jump to the next result
N jump to the previous one
- press
Escwhen u are done searching.
4. so which one do i use
nanowhen u want to change 2 lines in a config file and get out. no thinking, no modes, the shortcuts are on the screen.
vimwhen u are going to be in the file for a while, or when u are on a server, becausevimis installed on basically every linux machine in the world andnanosometimes is not.
- i prefer
vimoveremacsbecause it has more features and is more powerful. - be honest with urself on the first week. use
nanofor real work and practisevimon a copy of a file. trying to learn vim while u are in a hurry is how people end up hating it.
NOTES
vimtutor: vim ships with its own tutorial and it is genuinely good. 30 minutes, hands on, inside vim itself.
vimtutornvim(neovim) is a newer version of vim, and it comes up again in Chapter 8 with the package managers. everything in this chapter works exactly the same in it.
sudo pacman -S neovim # arch
sudo apt install neovim # ubuntu- some commands open an editor for u instead of taking an argument, like
git commit. which editor they open is decided by theEDITORenvironment variable, and u set it withexport, which is the next chapter, Chapter 7:
echo $EDITORvim
if that comes back empty, u can set it in ur
.bashrc:
export EDITOR=nanothis is a real thing worth doing early, because the first time
gitdrops u into vim with no warning is exactly the moment u don’t want to be learning:wq.
- vim has its own config file,
~/.vimrc, same idea as the.bashrcfile we get to in Chapter 7. u don’t need it yet, but that is where people’s setups come from.
Assignment
- Create a file
about-me.mdusingnano, write 3 lines about urself, save and exit. - Read the file back without opening an editor.
- Open it with
vim, useoto add a 4th line at the bottom, save and quit. - Read it back again and confirm ur new line is there.
- Open it with
vim, delete a whole line withdd, then quit without saving and confirm the line is still there. - Open it with
vimand copy the first line, then paste it 3 times at the bottom. Save. - Open it with
vim, jump straight to the last line without pressing the arrow keys, then jump back to the first. - Set ur
EDITORvariable tovimand make it survive closing the terminal.
stuck, or done and want to check? the solutions are here