The vi and vim primer for beginners

January 18, 2012 § 1 Comment

vi and vim

No matter the subject, you are always smart to start at the beginning. vi is the beginning of almost everything. It is the text  editor you will use while configuring your system during almost every administrative task. It mainly edits ASCII text files; a.k.a. every configuration file on your system. Without a  text editor you may as well power down the box.

A Short History

vi is the descendant of qed, the very first Unix editor written in 1965. From qed came ed (short for editor). From ed came ex, which later begat vi. Today, the old testament ends here. It should be adequate to say that vi has been around for a long while and, as such, it is on every *nix platform developed throughout all history, unless it has specifically been removed for security purposes. There are others, yes; emacs, pico, and nano, and the list goes on without end. This is not to say they are not good tools, they are. But, if you have to know just one – vi is it.

It’s Everywhere *nux Is

Given the prevalence of the Linux OS, vi becomes more and more valid argument. You can find Linux on your DSL modem, in newer Cisco equipment, in Tadiran phone systems, in cash registers and, lest we forget, that thing in your pocket that rings when your friends call. vi is resident on everything and, if it’s not today – it soon will be.

When Will You Need It?

Always – period. Here are some phrases that indicate your going to be using vim; When someone says you’ll have to edit that configuration file. EG:

  • Dude, that’s in smb.conf. Change the workgroup value to WHATEVER.
  • Dude, you’ll need to set an MX record for that new email server.
  • Dude, your command is too long. Write a script.
  • Dude, get your hands outta your pants.

The Difference Between vi and vim

Lots really. In the context of text editors vi is like a classic Mustang while vim is more like a Porsche 918 Spyder. If you ever really ran into vi it would be surprising. vi will exist on older systems but, generally, all you new guys are going to be using vim-minimal; this package includes a minimal version of VIM (vi IMproved), which is installed into /bin/vi – but it’s still not really vi.

Red Hat systems have these packages:
$ rpm -qa vim-*
vim-minimal-7.0.109-7.el5
vim-common-7.0.109-7.el5
vim-enhanced-7.0.109-7.el5

Debian-based systems (Ubuntu/Mint) have these packages:
$ dpkg -l | grep “^ii” | grep ‘vim-*’
ii  vim                    2:7.2.330-1ubuntu4   Vi IMproved – enhanced vi editor
ii  vim-common    2:7.2.330-1ubuntu4   Vi IMproved – Common files
ii  vim-runtime     2:7.2.330-1ubuntu4   Vi IMproved – Runtime files
ii  vim-tiny           2:7.2.330-1ubuntu4   Vi IMproved – enhanced vi editor – compact version

The vim-common package contains files which every VIM binary will need in order to run. The vim-enhanced package contains a version of VIM with extra, recently-introduced features like the ability to identify and be of greater use to you while writing in Python, Ruby and Perl so you can more efficiently write scripts in these languages as well as bash and awk. If you don’t have the more feature-rich packages, get them:

Red Hat:
$ sudo yum -y install vim-enhanced

Debian-based systems (Ubuntu/Mint) have these packages:
$ sudo apt-get install vim vim-runtime

 

The Jelly

I WISH someone had shared these learning resources with me when I started out – even though they probably didn’t exist then. And just to cover all bases, there are those types that only read their information, pulp snobs; for those people you can trudge through the official documentation. To be fair, there is a great deal of information not covered in these videos but you should read the docs when you run into trouble. To get started, this will help with a quick ramp-up.

First, check out The Vim Tutor. It’s not sexy but it has teeth. Not all vim tutor concepts are covered in the videos below. Just bite the bullet, commit one (1) weekend hour to it and you’ll be better-off in the long run. To begin, open a terminal and enter:
$ vimtutor

The vim guys will carry you from there. And don’t worry about mistakes. Every time you begin the tutorial, you will be working on a copy of it. It saves a temporary copy in the /tmp directory; eg: /tmp/tutorX17408. The last part of this file name should indicate a new copy of the tutorial is created every time you begin again. The last 6 characters will change every time. You can’t screw this up.

After that, check out these videos. There are lots of cool tips and tricks that will carry you through the rest of your life. If you pick up some of this now you are only making your life easier later. Give yourself a break, you deserve it:

  • VimCasts.org – Linked directly from vim.org’s web site.
  • Here are some video tutorials to cover even more vim-productivity ideas. There is some over lap here.
  • Official vi (vim) home.
  • Vim Cheat Sheet – simple, affordable; only the commonly used features. Check out other posts as well though.

VIM Addons

Once you have a handle on vim, there are only a few real paths: edit configuration files and writing scripts. Configuration files will take care of themselves but when you start scripting there are some add-ons that will make your life easier.

VAM – Vim Addon Manager: this little wonder will simplify your life – if you can get it installed. We’ll talk more in-depth about that in another post. VAM helps you to easily install some things to make your life easier. snipmate is a good example; it performs auto completion of easy to complex code structures; everything from loops to arrays. You could do the extra work but, unless there is a nuclear holocaust and we all have to start over again, why would you?

Pathogen – This does the same thing as VAM but in a different way. If I didn’t start a “vi vs emacs” fight earlier, that last statement should do the trick.

Personally, I use VAM; I’ve chatted with Marc Weber and he’s a mad man willing to fight and die for his cause. This is the only type of person that should be writing software. Marc makes tools that make his coding life easier. If it’s a pain in the ass for Marc to code something, he updates his tools (like VAM) to reduce the pain in his ass. Due to his nature, he makes these tools available to us for free. No pain in Marc’s ass = no pain in your ass. Strangely, he has great patience for supporting his software as well. What more could you ask of a person?

So, vim – learn it, know it, love it – because you can’t live without it.

About these ads

Tagged: ,

§ One Response to The vi and vim primer for beginners

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading The vi and vim primer for beginners at My Time with Linux and BASH in the POSIX universe.

meta

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: