- MCP server with stdio transport for local use - Search episodes, transcripts, hosts, and series - 4,511 episodes with metadata and transcripts - Data loader with in-memory JSON storage 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
197 lines
18 KiB
Plaintext
197 lines
18 KiB
Plaintext
Episode: 1724
|
|
Title: HPR1724: Vim Hints 002
|
|
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr1724/hpr1724.mp3
|
|
Transcribed: 2025-10-18 08:16:44
|
|
|
|
---
|
|
|
|
This is HPR episode 1,724 entitled Vim Hints 2. It is hosted by Dave Morris' and is about 26 minutes
|
|
long. The summary is hints and tips for Vim users part 2. 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 everyone. This is Dave Morris with another HPR episode. Today I'm continuing with my series
|
|
about Vim. I'm calling it Vim Hints because what I'm hoping to do is to give you a sort of a
|
|
survival guide in the ways of Vim. I've entitled this episode Vim at my homework and it's about
|
|
how you avoid losing stuff when you're working with Vim. I appreciate that we haven't really
|
|
started looking at how to use Vim yet to do anything but I thought it would be good as part of
|
|
the survival guide toolkit to look at the security angle of it first of all so that when you do
|
|
get to actually use it you can do this with the knowledge that you've got within pretty secure.
|
|
One of the first things that you can do is to ensure that Vim is running in the mode where it
|
|
keeps the backup of what you're editing and to do that you need to set up a configuration file.
|
|
I mentioned this in the last episode. I think I mentioned that normally the configuration file is
|
|
called .VimRC and it's in the home directory. That's certainly one of the options that you have
|
|
and that shadows the way things used to be done under VI but there's an alternative which is to
|
|
create a directory, top-level directory only home directory called .Vim and then to create a file
|
|
called VimRC without the dot on the front in that directory. You choose which one you prefer.
|
|
This is for Linux of course. If you're on Windows it's called underscore VimRC and so on.
|
|
I'm not really going to go into this. I'm not a Windows user. I don't know about Windows. You can
|
|
find out more about it in the documentation if you need to do this. Anyway, I prefer to keep my
|
|
configuration file in the top-level directory just because I guess I'm stuck in my ways.
|
|
That's a way I've always done it. The very existence of this file makes the program that's
|
|
sometimes invoked as VI behave as if it's Vim but it's a good idea to switch on all of the Vim
|
|
features by explicitly setting the no compatible option. All you need to do to achieve this is to
|
|
put a line in the configuration file which consists of set, space, in or compatible, no compatible.
|
|
In other words switch off VI compatibility. You appreciate that Vim has come from VI and it will
|
|
emulate it if you wish it to. You can't really think why you'd want to but you can if you want to.
|
|
Having got that out of the way, the thing I was talking about earlier, the backup thing.
|
|
is enabled by on the next line putting set, space, backup. Now backup file is a copy of the file
|
|
that you're editing which is taken before you actually make any changes so you've got something
|
|
to fall back on. The backup file is saved in the same directory as file being edited by default.
|
|
Personally I think this is the best way of doing things but you can if you wish tell Vim to put
|
|
your backups in a different place and there's a command you can add to the configuration file.
|
|
These are on the show notes by the way which will enable you to do this. Set backup
|
|
B-A-C-K-U-P-D-I-R equals and then you follow that with a list of directories. You could if you wanted to
|
|
put a tilde meaning home slash dot backup so if you'd created a dot backup directory at the top
|
|
level then it would write your backup files in there. The example shows that being followed by
|
|
comma dot which means the current directory. If you haven't got backup directory that means
|
|
and comma slash TMP so if you don't have a home directory which might be the case if you're
|
|
running off a disk which is not writable then try and write the backup into the temp directory.
|
|
So I wouldn't personally suggest you do this just now. Just get everything to be written into
|
|
the same directory as the file itself. I would suggest is the best way of going ahead.
|
|
So the next topic in the sort of securing things section is to talk about undoing changes.
|
|
So you might want to know how to do this. You might do something silly like delete great block of
|
|
text from your file so or you might make a change in the wrong place or you might do something.
|
|
There's many things you can do. We'll come on to some of the some of the large and powerful
|
|
features of them but of course large and powerful means you can make some pretty horrendous errors.
|
|
So if you do make a mistake then you can undo the change. If you're in normal mode remember we
|
|
talked about normal mode the mode you normally start up in and the one that you return to by
|
|
pressing escape. When you're in that mode typing you will undo the last change. There's also a
|
|
redo so if you undead did something and realized no actually that's what I did want to do you can
|
|
hold down control key and press R normally call control R and it will redo the last change.
|
|
So Vim is actually keeping a record of these changes. It's keeping many of them. You can control
|
|
how many but we won't go into that just now and you can undo successive changes back in time
|
|
and control R redoes the the undun changes forward in time. It can get a lot more complicated than
|
|
this because if you think about it you can undo a thing make a change and then undo that and so
|
|
you end up with a sort of tree structure of things you've done things you could undo and things
|
|
you could redo. It can get quite complex and we'll maybe look at this in a later episode.
|
|
Personally I don't do I've not really bothered with this much myself so less it sort of seems
|
|
to be appropriate I'll maybe not get to that but we'll see we'll see. I'm going to play this by
|
|
year largely and if I get feedback asking for particular me to go into a particular directions
|
|
then I will do that. So one of the things you you can do with Vim and it's quite good to do this
|
|
I believe anyway is that you can retain your undo history between editing sessions. So there's
|
|
thing called an undo file which holds the change history and what's more you can put that in
|
|
a directory specifically for this purpose so it keeps an undo file in there in this directory
|
|
and I've given examples for how to do this you need to create the undo directory first
|
|
and in my particular case I've put it under the dot Vim directory I mentioned earlier
|
|
so you would put in your configuration file the line set space undo dir
|
|
i-u-n-d-o-d-i-r equals and then tilde slash dot Vim slash undo dir so that means I want my undo file
|
|
or files to be files plural to be put into this directory and then you'd follow that by enabling the
|
|
undo file option that is saving your undo history with the command set space undo file u-n-d-o-f-i-l-e
|
|
now the consequence of this is that when you start an editing session it's possible for you to type
|
|
you as your first first thing you do what that means is assuming you've been editing this file before
|
|
it means undo the thing I did last time I was editing it that can be a little bit surprising
|
|
I certainly surprised myself by doing this but it is if you know what you're doing
|
|
quiet a powerful feature so things like you might apply a change into a program test the program
|
|
find having saved it that is then find oh hell that's that was rubbish I don't want to do that
|
|
go back start editing the file again press u and undo that change and you're back to where you
|
|
were before you you did the thing that you wish you hadn't so the next topic under this particular
|
|
heading keeping your files and your work intact is quite large subject of file recovery now there's
|
|
a lot here I won't go into a huge amount of detail I just wanted to give you a sort of
|
|
taster and it's enough to survive with there's references here that you can follow up if you
|
|
want to dig deeper so as Vim is running it has its own recovery mechanism where it generates
|
|
a what's called a swap file this file is normally under under unix and Linux under BSD and Linux
|
|
I really mean or indeed macOS it the file is the file name is built from the name the file being
|
|
edited with the dot propended to it making it a hidden file and with the extension dot SWP
|
|
being stuck on the end so if you're editing the file test file the swap file would be called
|
|
dot test file dot SWP and it's in the same directory as the file you're editing you can
|
|
make Vim write swap files elsewhere and there's something to be said for this you could put it on
|
|
a different partition so that you're safer from problems and crashes you can also switch it off
|
|
that the whole swap file business but that's not smart not a smart thing to do I would say and I
|
|
would suggest leaving things in the default mode until you understand more about about this
|
|
and the swap file is updated that's keeping history of what's going on it's updated after typing
|
|
200 characters or when you've not typed in anything for four seconds when you finish editing
|
|
when you write your results or indeed quit out of it out of them then swap files deleted so it's
|
|
a thing that's present while you're editing and it's holding a recovery information about the
|
|
edits that you have done so the issue of recovery is really the the big issue here what recovery
|
|
is offering you is the ability to recover from something nasty happening like the power going
|
|
out or your machine crashing for whatever reason I have been known to open up a ssh session to
|
|
another machine and then shut it down forgetting that I've actually got an editor running
|
|
on it or indeed shut down the machine there's all manner of dark things that one can do to cause
|
|
problems if something happens to crash your vim session then your swap file will be left behind
|
|
and if you know that's the case you know that something's gone horribly wrong then you can
|
|
just simply invoke vim in its recovery mode to start the recovery process and you do that by
|
|
typing vim space minus r space and then the file name you will see the recovery process will
|
|
will be invoked you'll see a message of the sort that I've included in the show notes it says
|
|
using swap file in the name of the swap file original file gives you the name the original file
|
|
recovery completed tells you to check that everything's okay and so on and so forth and then you
|
|
can carry on with your editing session now the key issue is that it doesn't delete the swap file
|
|
the swap file is still there we'll come on to that in a minute now the vim minus r r thing is
|
|
is fine personally I don't use it very often I tend to just go and do the alternative thing which is
|
|
to go back to editing the file I was editing before and that's the point at which
|
|
vim will say oh there's a swap file here and we'll alert you now you'll see in the show notes
|
|
an example of the sort of message you'll get which starts with e325 colon attention
|
|
says I found a swap file it gives you the name of the swap file gives you details about when
|
|
when it was created what file you were editing and then there's a key very key piece of information
|
|
modified and in an example I've given you here yes you might see modified no that means you
|
|
hadn't changed anything then since you last saved your file so this case modified yes is the
|
|
critical one so it then warned you that you could see the same consequences you could see the
|
|
same situation if somebody else is editing the file well you indeed were opening it had opened
|
|
and we're editing the same file in another window or something that's a possibility that's another
|
|
that's one reason why they might be a swap file but the most likely thing is that the edit is
|
|
crashed and this is it's offering you the option of recovering so you'll get a a message saying
|
|
what do you want to do you want to open it read only edit it regardless recover delete it delete
|
|
the swap file that is quit abort that's assume that you have got a crashed edit session
|
|
you see them the message that there are modifications you want to do the recovery all you need to do
|
|
to press R and the recovery will go ahead and it's the you get the same message as you would have
|
|
got if you type VIN minus R and the file name now here's the thing about the swap file
|
|
it's not deleted it's used for the recovery purposes but it is not deleted in fact the edit session
|
|
creates a new swap file in fact the the new swap file will have the extension dot SWO instead of
|
|
SWP and then keeps on cycling through the last letter of this extension until it runs out
|
|
seems slightly off anyway it goes all the way back to a so if you if you imagine you've got the
|
|
situation where you have just recovered and you have said okay recover you've recovered the
|
|
changes you've now got your file back to the state it was with unsafe changes in it so you would
|
|
type colon wq as we talked about in the last episode which writes the output and quits the edit now
|
|
if you do this you will have exited everything will be safe and fine but there will still be
|
|
a swap file left from the crash session if you then go back a bit later on and edit the same file again
|
|
you will find that VIN comes back with the e325 message saying I found a swap file but it will it will
|
|
warn you that the file that it that you're editing is newer than the swap file in other words
|
|
something weird has happened as far as VIN is concerned and the file has changed now all
|
|
that's happened is the swap file has not been deleted so you can at that point at the prompt
|
|
that I mentioned earlier use d to delete the file the only thing about this is that it can be
|
|
confusing I find it confusing sometimes you've done the recovery you think everything's fine you've
|
|
done further editing you've saved your file you've gone off done something else you come back
|
|
right I'm going to edit my file again you edit it and it comes back and says I found a swap file
|
|
and you think in what oh oh that's that old swap file from before right I'll just delete it that
|
|
seems a bit counterintuitive to do it that way so I've offered you two other ways of dealing with
|
|
this you realize that this is VIN being very very very cautious and not deleting the recovery file
|
|
all on its own what you could have done is you could have quit saved your file and quit out of
|
|
the editor and then knowing that there was a swap file you could have removed it yourself
|
|
in the example I've shown you in the show notes I'm assuming that you file in question is called
|
|
test file 2 therefore there's a swap file called dot test file 2 dot SWP where you could simply
|
|
RM that file get rid of it having recovered it but that's also messy the third possibility is
|
|
you recover you write the changed file out to make everything secure then you don't quit from them
|
|
you type the command colon e what colon e does is it tells me to re-edit the file you're already
|
|
working with in other words it it starts again as if the file has just seen the file and in doing so
|
|
it checks to see if there's a swap file left around and it says oh yes there's a swap file over
|
|
there it gives you the warning that there's a there's a recovery file but it's that the file
|
|
that you're editing is newer than the recovery file so you can then type the D command and come out
|
|
and delete the swap file and then quit the edit and it did continue to do the editing if you want to
|
|
personally I think option 3 is the more logical thing to do but you will need to choose
|
|
the route through this that you prefer for yourself but I'm raising this because it is one of
|
|
the slightly weird aspects of them that catches people out when they're new to using it
|
|
in the show notes I want to talk about the case where there are no changes in the swap file
|
|
instead of it saying modified yes you get the message modified no so the swap file is not needed
|
|
you don't need to recover anything because there was nothing to recover so you can just press D
|
|
at the prompt open read only edit anyway etc and you can then continue editing the file as normal
|
|
and you know at that point that the swap file has been deleted and everything is fine and dandy
|
|
so hopefully the show notes will help you to get your head around this really I made
|
|
sort of introduced this quite early and it might be confusing me doing it that way but I thought
|
|
if you had this as a thing to refer to later on as you use VIM that you will find it
|
|
helpful to refer back so I've added the comment that they make in the VIM manual which is
|
|
don't panic the recovery thing is a little bit weird but it's pretty robust and we'll
|
|
get you out of nasty situations where your machine has crashed or something else nasty has happened
|
|
so I finish up my show notes with the summary of the changes you should make to the configuration
|
|
file I've just listed them all there together and reminding you that you in normal mode will
|
|
undo a change control R in normal mode will redo a change that you've previously undone
|
|
and don't forget that restarting VIM after a crash will invoke a recovery dialogue which we've
|
|
just been discussing so I hope that's useful and the next episode in this series we will start using
|
|
VIM for proper and for real hope you find that useful okay bye
|
|
you've been listening to HECKA Public Radio at HECKA Public Radio dot org we are a community podcast
|
|
network that release the shows every weekday Monday through Friday today's show like all our
|
|
shows was contributed by an HPR listener like yourself if you ever thought of recording a
|
|
podcast and click on our contributing to find out how easy it really is HECKA Public Radio was
|
|
founded by the digital dog pound and the infonomicon computer club and it's part of the binary revolution
|
|
at binwave.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 light 3.0 license
|