INTRO

Simple text files are at the heart of Linux, so editing these is a key sysadmin skill. There are a range of simple text editors aimed at beginners. Some more common examples youā€™ll see are nano and pico. These look as if they were written for DOS back in the 1980ā€™s - but are pretty easy to ā€œjust figure outā€.

The Real Sysadmin<sup>tm</sup> however, uses vi - this is the editor thatā€™s always installed by default - and today youā€™ll get started using it.

Bill Joy wrote Vi back in the mid 1970ā€™s - and even the ā€œmodernā€ 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. Vim is actually a contraction of Vi IMproved and is a direct descendant of Vi.

Very often when you type vi, what the system actually starts is vim. To see if this is true of your system type, run:

vi --version

You should see output similar to the following if the vi command is actually symlinked to vim:

user@testbox:~$ vi --version
VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Oct 01 2021 01:51:08)
Included patches: 1-2434
Extra patches: 8.2.3402, 8.2.3403, 8.2.3409, 8.2.3428
Modified by team+vim@tracker.debian.org
Compiled by team+vim@tracker.debian.org
...

YOUR TASKS TODAY

  • Run vimtutor
  • Edit a file with vim

WHAT IF I DONā€™T HAVE VIM INSTALLED?

The rest of this lesson assumes that you have vim installed on your system, which it often is by default. But in some cases it isnā€™t and if you try to run the vim commands below you may get an error like the following:

user@testbox:~$ vim
-bash: vim: command not found

OPTION 1 - ALIAS VIM

One option is to simply substitute vi for any of the vim commands in the instructions below. Vim is reverse compatible with Vi and all of the below exercises should work the same for Vi as well as for Vim. To make things easier on ourselves we can just alias the vim command so that vi runs instead:

echo "alias vim='vi'" >> ~/.bashrc
source ~/.bashrc

OPTION 2 - INSTALL VIM

The other option, and the option that many sysadmins would probably take is to install Vim if it isnā€™t installed already.

To install Vim on Ubuntu using the system package manager, run:

sudo apt install vim

Note: Since Ubuntu Server LTS is the recommended Linux distribution to use for the Linux Upskill Challenge, installing Vim for all of the other various Linux ā€œdistrosā€ is outside of the scope of this lesson. The command above ā€œshouldā€ work for most Debian-family Linux OSā€™s however, so if youā€™re running Mint, Debian, Pop!_OS, or one of the many other flavors of Ubuntu, give it a try. For Linux distros outside of the Debian-family a few simple web-searches will probably help you find how to install Vim using other Linuxā€™s package managers.

THE TWO THINGS YOU NEED TO KNOW

  • 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 ā€œnormal modeā€ and ā€œinsert modeā€, and as a beginner, simply remember:

"Press Esc twice or more to return to normal mode"

The ā€œnormal modeā€ is used to input commands, and ā€œinsert modeā€ for writing text - similar to a regular text editorā€™s default behaviour.

INSTRUCTIONS

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 ā€œnormal 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 normal 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, letā€™s 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 normal mode and 33dd is a command that says ā€œdelete 33 linesā€. Now, youā€™re still in normal 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 ā€œnormal modeā€
  • From normal mode :q! will always quit without saving anything youā€™ve done, and
  • From normal mode u will undo the last action

So, hereā€™s some useful, productive things to do:

  • Finding things: From normal mode, type G to get to the bottom of the file, then gg to get to the top. Letā€™s search for references to ā€œsunā€, type /sun to find the first instance, hit enter, 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 normal mode when youā€™re done.
  • Writing your changes to disk: From normal mode type :w to ā€œwriteā€ but stay in vim, or :wq to ā€œwrite and quitā€.

This is as much as you ever need to learn about vim - but thereā€™s an enormous amount more you could learn if you had the time. Your next step should be to run vimtutor and go through the ā€œofficialā€ Vim tutorial. It typically takes around 30 minutes the first time through. To solidify your Vim skills make a habit of running through the vimtutor every day for 1-2 weeks and you should have a solid foundation with the basics.

Note: If you aliased vim to vi for the excercises above, now might be a good time to install vim since this is what provides the vimtutor command. Once you have Vim installed, you can run :help vimtutor from inside of Vim to view the help as well as a few other tips/tricks.

However, if youā€™re serious about becoming a sysadmin, itā€™s important that you commit to using vim (or vi) for all of your editing from now on.

One last thing, you may see reference to is the Vi vs. Emacs debate. This is a long running rivalry for programmers, not system administrators - vi/vim is what you need to learn.

WHY CANā€™T I JUST STICK WITH NANO?

  • In many situations as a professional, youā€™ll be working on other peopleā€™s systems, and theyā€™re often very paranoid about stability. You may not have the authority to just ā€œsudo apt install <your.favorite.editor>ā€ - even if technically you could.

  • However, vi is always installed on any Unix or Linux box from tiny IoT devices to supercomputer clusters. It is actually required by the Single Unix Specification and POSIX.

  • And frankly itā€™s a shibboleth for Linux pros. As a newbie in an interview itā€™s fine to say youā€™re ā€œonly a beginner with vi/vimā€ - but very risky to say you hate it and can never remember how to exit.

So, it makes sense if youā€™re aiming to do Linux professionally, but if youā€™re just working on your own systems then by all means choose nano or pico etc.

EXTENSION

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:

RESOURCES

Some rights reserved. Check the license terms here

  • SaintWacko
    link
    fedilink
    English
    arrow-up
    2
    Ā·
    9 months ago

    Definitely agree that everyone should use vim. Itā€™s got a learning curve, but once you get the hang of it itā€™s so much faster. The one thing I disagree with here is the use of the arrow keys. Moving your hand to them any time you want to move the cursor will slow you down. Instead you should use your vimrc to noop your arrow keys, forcing you to get used to hjkl.

    • Livia Lima@programming.devOPM
      link
      fedilink
      arrow-up
      1
      Ā·
      8 months ago

      Oh yeah, it slows things down, for sure. But if you never used anything like vim before, that slow pace can benefit the learning experience (at least that happened to me when I started). Bottom line is: vim is a lot. Be patient and it will get easier (and faster) over time.