Episode: 1714
Title: HPR1714: Vim Hints 001
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr1714/hpr1714.mp3
Transcribed: 2025-10-18 08:08:30

---

This is HPR episode 1,714 entitled Vim Hints 1. It is hosted by Dave Morris' and is about 18 minutes
long. The summary is Hints and Tips for Vim users Part 1. This episode of HPR is brought to you by
an honesthost.com. Get 15% discount on all shared hosting with the offer code HPR-15. That's HPR-15.
Better web hosting that's honest and fair at An Honesthost.com.
Hello everybody. This is Dave Morris. I've got a new show for you today. I'm calling it Vim Hints.
This is Episode 1. I chose Vim Hints because there's already a website called Vim Tips. Probably
it's one called Vim Hints as well. I didn't look for that. So first episode I'm calling an introduction
to Vim and I want to make this into a series. Not sure how long it will go on for but I'm hoping that
I'll be able to produce some interesting material for you this way. So as a Linux user there are many
editors available to you. I'm sure you know that. Which one you want to use depends on your needs
and the amount of time you want to dedicate to learning how to use it. One of the editors from
the early days of Unix is VI. This was written in 1976 and interestingly enough contemporary with
it is EMAX, also originating in 1976. However in my experience anyway it seemed to become the norm
that VI rather than EMAX was provided in standard versions of Unix. The sort of things you got from
Evenda, HPUX or digital Unix, those types of things back in the day. And this trend has often
continued into Linux. So your standard Linux installation comes with VI. So I originally started Unix
around 1988. That was using it. I'd come across it before that but we had work stations available to
around that time and I found VI was there and was available to me to do things. I was a systems
manager at that time so often necessary to change configuration files and that type of thing.
So I found that VI was the way to make changes and I learned it in a very rudimentary way
and I would find it on any of the Unix systems I came across. Many derivatives and clones of VI have
been created over the years. The one that's become the most popular and available is VIM.
This is an acronym for VI Improved and it was created in 1991. This is what I use these days.
I've not wanted to learn another editor since adopting it and even though I've experimented with
others I find this one is the one I want to continue with partly because I've invested quite a
lot of effort into learning it. So this is the editor we're going to be looking at in this series.
So what's the series about then? Well the thinking behind it is that you may already be using VIM
but there are features that you may not be aware of that I can perhaps point you to.
I certainly find this in my work where I find colleagues using VIM but not knowing the power of it
and therefore struggling a bit and I was able to point them in the in the right direction.
So I'm hoping to do that again. You might be using a different simple editor,
like using Nano or G-Edit or something like that. You might be looking at VIM
with the hopes that you can gain some advanced features from an editor like that.
Of course you may prefer to learn EMAX instead. That's perfectly fine. You should
choose the tool that best suits your needs. Both EMAX and VIM have quite steep learning curves
broad range of capabilities you gain from knowing either is considerable. Now I should say
probably go on that I'm not an expert in VIM. In fact I'm continuing to learn new VIM features
on a regular basis. However I've been using it for many years and would like to share
with you some of what I have learned. So why would you use VIM? Well with simpler editors you
can move around in a file. You can add, remove and change text and save the results. The editor
might have something like syntax highlighting some degree of knowledge of the programming language
you're typing. They might have spell check. You might have a whole list of quite nice features.
But with them another more advanced editor is you have this and a lot more. You can perform
global changes throughout a file. Processed many files at once, plug in to the editor to
changes behavior and so on. There's a language behind the scenes which you can use to build extensions
if you want to. So it means there's plenty of extensions available written in this language.
So let's look at using VIM. I'm doing this in the assumption that you haven't used it yet. If you
have you might want to skip this bit but you might find it useful anyway. So usually when you
come to a terminal and type the command VI at the command line this usually invokes VIM
on modern and existence. I haven't done a comprehensive survey on this but I believe it to be the case.
And VIM runs in VI compatible mode by default which usually results in many of the VIM enhancements
being unavailable. VIM uses a configuration file which is called .VIMRC in the home directory
and that's what's called on Linux. It's called different things on Windows. I'm not sure about
OSX and other operating systems, the VSDs but I'm not going to look at these in this series anyway.
VIM also has a GUI interface and you get to that usually by the command GVIM.
And that has its own configuration file which is in the home directory and it's called .GVIMRC.
Now if you don't have a .VIMRC file you can create one with touch, touch,
tell the slash .VIMRC. That just creates an empty one. Do that before you start using VIM
and it will stop it running in VI compatible mode even if you invoke it with VI.
You can start VIM on its own just by typing VIM but normally you would use it to edit a file so
you'd follow that invocation with the file name. The file doesn't need to exist so to create a new
file called say test file invoke VIM with the command VIM test file. So once it's running VIM
shows the contents of the file. All the lines on the screen where there's no content so it's just a
one line file then for example then they're all marked with a tilde character in the left margin.
If you're creating a brand new file then the first line and last line on the screen will be black
and the rest will contain the tilde on the left. Let's just so you can identify what's data and what's
not on a screen that might be bigger than the actual file. One of the key things about VIM is it's a
modal editor. It has modes. The mode you start in is called normal mode and in this mode you can
move around the lines of a file and perform actions but nothing you type at this point is actually
written into the file. In fact the keys that you press are actually editing commands. That's this
particular thing about VIM and this is one of the things that catches people out. If they just
fire up VIM without knowing this it's quite easy to get into a rare mess by hitting keystrokes
which are commands which do things possibly changing whatever file you've got and they get you
into a mess. So since in this case it's a new file there's not much you can do over an entering
text and to do this you'll need to switch into the next mode which is called insert mode. You do
this by pressing the i key, lowercase i and you see the message dash dash insert dash dash will
appear at the bottom line of the screen. That warns you that you're in insert mode. Now type
some text and press the enter key at the end of each line. You might notice that in insert mode
you can press the arrow keys and move back to text you've already typed. This was not a feature
of the original VI. It may be later I'm not sure I've not used VI for a long time but it was a
thing I always used to fall over when I was a VI user but this is available in VIM so you can
actually go back and edit stuff you've already typed in. When you finished entering text you press the
escape key which I'm denoting in my notes as ESC in bold. That takes you out of insert mode.
So now you're back in normal mode and now things that you type are commands, key presses are commands
and not data to be entered into the file. So to move around in normal mode you can use the arrow keys
or you can use the home row keyboard keys K to move up, J to move down, H to move left and L to move
right. If you look at a keyboard you can see that actually makes quite a lot of sense because if
you're a proper typist which I'm not I should say then this will be right under where your fingers
would normally be and will be handy for you. So a lot of people say it's good to learn that. I must
admit I don't use them all that much even though I really should train myself to do it. I'm a sort of
hunt and peck old fashioned type typists. My kids are lightning fast in comparison to me I'm still
fumbling about looking to see where the keys are and stuff. Don't follow me my footsteps in this
particular case. Anyway this brings us to the last mode we'll look at in this particular
episode and this is command mode. To enter this mode you press the colon key while you're in normal
mode. This moves the cursor to the last line of the screen and that's what it was there for. I
remember I mentioned it before and the line begins then with a colon that tells you that you're in
command mode the cursor is there sitting after the colon and here you can enter another class of
command. It's a very powerful command which will let you do things right across the file, global
things to the file and so forth. We'll look at some of these later on in the series. This is how
you would save the file and exit VIM. So what you do is you go into command mode and use the
W command that writes the file that saves the file and then to exit from VIM use the Q command.
So you can actually join these two together by typing from normal mode colon WQ that writes the
file and exits. Now if you were to just use colon Q on its own having entered data into VIM this
would produce an error because VIM prevents you from throwing away your work before it closes.
So if you really meant to quit without saving you were trying to abort whatever you were doing
and this is a key piece of information in fact because getting into VIM without realizing what
you're doing, getting out colon Q. If you follow it with an exclamation mark that means I really want
to do this. So colon Q exclamation mark exit from VIM without saving. So I realize that's quite a
lot of information to pile into your ears. I assume you're not sitting out a keyboard, I don't
edit it doing this. So what I've done is to give you a summary of what I've covered so far. I'll
just just go through this very quickly but it's there as an aid memoir for a few to look at in the
notes and I'll finish after that. There's a bunch of resources and links in the notes point us to
Wikipedia pages for the editors I've mentioned. There's a number of books, some of which cost money,
some of which are free that would be well worth looking at. Personally I use learning the VI and
VIM editors' seventh edition which is the first one in my list. I've got an ebook version of that
and find that really quite useful but the others in the list are also pretty good. There's other
resources in the list which is pointed to the VIM community which is massive and very very active
indeed and there's also VIM tips wiki where if you if once you've got into VIM I won't need to
tell you anymore whenever you have a question going there will be the place to find information.
So let me finish by summarizing what I've said today. First of all VIM usually starts in normal
mode. The arrow keys or hjknl will navigate around in normal mode i enters insert mode
escape enters exits from insert mode in reverse in normal mode. Call on in normal mode enters command
mode. Call on w then in normal mode writes the file. Call on wq in normal mode writes and exits.
Call on q in normal mode exits but only if nothing was changed or added. Call on q exclamation mark
in normal mode exits regardless of any changes. Okay that's it.
You've been listening to Hacker Public Radio at Hacker Public Radio dot org. We are a community
podcast network that releases shows every weekday Monday through Friday. Today's show like all our
shows was contributed by an hbr listener like yourself. If you ever thought of recording a podcast
then click on our contributing to find out how easy it really is. Hacker Public Radio was found
by the digital dog pound and the infonomican computer club and is part of the binary revolution
at binrev.com. If you have comments on today's show please email the host directly leave a comment
on the website or record a follow-up episode yourself unless otherwise status. Today's show is
released on the creative comments, attribution, share a live 3.0 license.
