Simple text files are at the heart of Linux, so editing these is a key sysadmin skill. There are a range of simple editors aimed at beginners such as: nano
, pico
, joe
or jed
. These all look horribly ugly, and as if they were written for DOS back in the 1980's - but are pretty easy to "just figure out".
The Real Sysadmin however, uses vi
- this is the editor that's always installed - and today you'll get started using it.
Bill Joy wrote vi
back in the mid 1970's - and even the "modern" descendant vim
that we'll concentrate on is over 20 years old, but despite their age, these remain the standard editors on command-line server boxes. Additionally, they have a loyal following among programmers, and even some writers.
Very often when you type vi
, what the system actually starts is vim
. To see if this is true of your system type:
vi --version
to check.
- There are two "modes" - with very different behaviours
- Little or nothing onscreen lets you know which mode you're currently in!
The two modes are "command mode" and "editing mode", and as a beginner, there is one simple technique to remember - simply:
"Press Esc twice or more to return to command mode"
So, first grab a text file to edit. A copy of /etc/services
will do nicely:
cd
pwd
cp -v /etc/services testfile
vim testfile
At this point we have the file on screen, and we are in "command mode". Unlike nano
, however, there’s no onscreen menu and it's not at all obvious how anything works!
Start by pressing Esc once or twice to ensure that we are in command mode (remember this trick from above), then type :q!
and press Enter. This quits without saving any changes - a vital first skill when you don't yet know what you're doing!
Now let's go in again and play around, seeing how powerful and dangerous vim
is - then again, quit without saving:
vim testfile
Use the keys h j k and l to move around (this is the traditional vi
method) then try using the arrow keys - if these work, then feel free to use them - but remember those hjkl keys because one day you may be on a system with just the traditional vi
and the arrow keys won't work.
Now play around moving through the file. Then exit with Esc Esc :q!
as discussed earlier.
Now that you've mastered that, lets get more advanced.
vim testfile
This time, move down a few lines into the file and press 3 then 3 again, then d and d again - and suddenly 33 lines of the file are deleted!
Why? Well, you are in command mode and 33dd is a command that says "delete 33 lines". Now, you're still in command mode, so press u - and you've magically undone the last change you made. Neat huh?
Now you know the three basic tricks for a newbie to vim
:
- Esc Esc always gets you back to "command mode"
- From command mode
:q!
will always quit without saving anything you've done, and - From command mode
u
will undo the last action
So, here's some useful, productive things to do:
- Finding things: From command mode, type
G
to get to the bottom of the file, thengg
to get to the top. Let's search for references to "sun", type/sun
to find the first instance, then press n repeatedly to step through all the next occurrences. Now go to the top of the file (gg remember) and try searching for "Apple" or "Microsoft". - Cutting and pasting: Go back up to the top of the file (with gg) and look at the first few lines of comments (the ones with "#" as the first character. Play around with cutting some of these out and pasting them back. To do this simply position the cursor on a line, then (for example), type 11dd to delete 11 lines, then immediately paste them back in by pressing P - and then move down the file a bit and paste the same 11 lines in there again with P
- Inserting text: Move anywhere in the file and press i to get into "insert mode" (it may show at the bottom of the screen) and start typing - and Esc Esc to get back into command mode when you're done.
- Writing your changes to disk: From command mode type
:w
or:wq
to “write and quit”.
This is as much as you ever need to learn about vi
- but there's an enormous amount more you could learn if you had the time. Your next step should be to run vimtutor
- this official tutorial should always be installed, and takes only 30 minutes.
However, if you're serious about becoming a sysadmin, it's important that you commit to using vim
for all your editing from now on.
One last thing, you may see reference to "vi versus emacs" . This is a long running argument for programmers, not system administrators - vi/vim
is what you need to learn.
Let the forum know how you went.
If you're already familiar with vi
/ vim
then use today's hour to research and test some customisation via your ~/.vimrc
file. The link below is specifically for sysadmins:
- Here is why
vim
uses the hjkl keys as arrow keys (http://www.catonmat.net/blog/why-vim-uses-hjkl-as-arrow-keys/) - Graphical vi-vim Cheat Sheet and Tutorial (http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html)
- Vi - Vim Tutorial (http://www.youtube.com/watch?v=71YTkxUNwmg) (video)
Copyright 2012-2020 @snori74 (Steve Brorens). Can be reused under the terms of the Creative Commons Attribution 4.0 International Licence (CC BY 4.0). This means you can copy, distribute and adapt the material as long as you credit Steve Brorens, and abide by the CC BY 4.0 licence terms.