Episode: 2348 Title: HPR2348: Vim Hints 005 Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr2348/hpr2348.mp3 Transcribed: 2025-10-19 01:34:22 --- This episode of HPR is brought to you by Ananasthost.com, get 15% discount on all shared hosting with the offer code HPR15, that's HPR15, better web hosting that's honest and fair at Ananasthost.com Hello everybody, welcome to Hacker Public Radio. My name is Dave Morris. Today I'm going to do show number five in the Vim Hint series. So I've started by saying, oops, where did half of 2015, all of 2016 and the first half of 2017 go? Well, it's been that long since we had the last episode of this series number four. What happened? Well, life got in the way and also motivation dwindled a bit. This particular series talking about Vim, although I think it's really important, is quite demanding. The said series that I did which had five episodes in it and required a lot of research. Useful, turned out, but a lot of research. Well, that was a walk in the park compared to this one, which is I've written down here a continental scale landscape. Vim is very, very, very, very complicated and feature rich. That's not a bad thing. The way to approach it is to learn enough to survive with, then learn a bit more that makes life a bit easier and keep on learning from then on. That's the way I see it. So as I've said, the original goal of the series was to try and introduce some of the useful features of Vim and make it manageable for every day use. And the hope was and still is that the series could get people started on their own journeys through the marvels of Vim. The other motivation for doing this was that there's a currently circulating meme, I guess what it is, which is summarized in Stack Overflow article, which I've referenced here, how to exit the Vim editor, which is trying to make out that off people using Vim, most people get lost and can't get out of it, could have something to do with the fact that Vim sometimes is the default editor that you're taking to by various things like Git. I think that's true, is it? Yeah, never actually checked that, but well, we dealt with how to get out of Vim in the first episode quite deliberately. And I think that that's not a difficult thing to do. And I think that that reveals that this meme is somewhat ridiculous, but you know how these things go, once they get started on the internet, they just keep on and on and on until everybody's thoroughly sick of them. So we've done four episodes of this series and the last run was in March 2015. I've listed links to all of the previous episodes, but I thought it'd be useful if we just whizzed through rapidly to summarize what was covered. Episode one, we looked at the very, very basic editing and talked about the modes of Vim, which is one of the features of Vim, normal insert and command modes we looked at. In episode two, we looked at Vim's backup mechanism where it keeps recovery information. We looked at how to undo and redo changes, because it's useful to be able to undo what you, you've a mistake you've made up to a number of levels. And we also looked in the context of the backup at how you recover in the event of a problem. I went to quite some length about this because it's one of the features that can trip you up. So it was useful to, I thought it would be useful to understand it better. You also started looking at the dot Vim RC config file, which is a vital component of Vim and its usability. In episode three, we began looking at movement, which you can do in normal mode. That's where you're not actually typing into Vim, but you're issuing commands to move around and change stuff and whatever. We added more to the configuration file. This is the sort of model I want to continue following various commands followed by some extras to add to the configuration file. You might find useful. Hopefully the number of changes the config file will diminish as time goes on, but it's at the moment it's actually increasing more than anything else. I hope you find that useful anyway. Then in episode four, we looked at more movement commands and we looked at searching. We began looking at things that make changes, so we could add stuff, insert stuff, delete stuff and change text in various ways. In the concept of doing these, we looked at movements and how you could do one of these actions in relation to a movement, delete up to the end of this word and that's our thing. Again, we added more things to the configuration file. So this time, I want to look at the subject of copying and pasting, which you can do in Vim. When we've seen a little bit of this in as much as when we've changed stuff and when we've deleted stuff, it's been in normal mode and we want to be able to do the missing bit, which is to copy things and then paste things or indeed delete things and paste things. We'll see in a minute. I made a reference to how you can find out the full gory details of this in the help system and there's also an online copy of the same documentation, which I've linked to. So first of all, how do you copy stuff in Vim? Well, there is a command based around the letter Y, where Y stands for Yank, which is another word for copy. So if you type two lowercase Ys in succession, so YY in normal mode, it copies lines. You will remember that we looked at DD, in both lowercase, in episode four, and if you proceed that by account, it will repeat the number of lines. So there's the same with the YY command. It will yank more than one line, which is the default if you don't give a count. So if you want to say you're on a line, you want to copy that line and the next two, you do three YY, both lowercase. There is a capital Y command, which is a synonym, which I can't say, for the two lowercase Ys, but it doesn't do the equivalent of what the capital C in capital D commands do, which is Yank from the current position to the end of the line. It copies the entire line. So that's a little bit of an inconsistency, but just one of those things you have to live with unfortunately. So if you want to yank less than a line, then you would use a single lowercase Y, and that operates on single character mode, but you need to follow it with the movement command, just like with the lowercase C for change, lowercase D that we saw in episode four. So I've given some examples of these operator plus movement combinations. So for example, Y, lowercase Y, lowercase W, will yank from the cursor to before the start of the next word. Remember that lowercase W jumps to the start of the next word, and lowercase Y, lowercase E, yanks from the cursor to the end of the next word. So if you're on a word already, it will yank to the end of it into the last letter of it. If you are at the end of the previous word or on a space between words or something like that, then it will go from there to the end of the next word. Lowercase Y dollar yanks from the cursor to the end of the line, which is similar to the Y, Y command except that Y, Y, just if you're in the middle of a line with Y, Y, then it copies everything on the line. Whereas Y, lowercase Y dollar copies from where you are to the end of the line. Lowercase Y zero yanks from before the cursor to the beginning of the line. So you remember the zero command takes you to the beginning of the line. That is column one. I say that because there's a circumflex which takes you to the first non-blank of a line. So you can actually do Y followed by circumflex. I put that in the list. But the point of doing this is to say that there are many combinations and I'm not going to list them all out. But you can deduce some of them from what we've already learned about movement commands. And also you can look at the documentation to get a more of a flavor. The last one in my list of examples was lowercase Y followed by a closed parenthesis. Remember closed parenthesis moves to the end of the sentence. So it will yank from that your cursor position to the end of the sentence. So that's it. You've copied all these things in all the different ways. And what can you do with them? Well you can paste them. Otherwise there's not much point. But this also applies to having deleted or changed text. Because when you delete text you can then paste it somewhere. So that would allow you to copy it but to move text around. Pasting it, the VIM documentation calls this action put. But I prefer to think of it as pasting. But you use the letter lowercase P and uppercase P letters. So the lowercase P command in normal mode, paste text after the cursor. And the uppercase P command puts or paste text before the cursor. In both cases the cursor is left on the last character of the pasted text. Both commands can be preceded by a count. And in that case you get multiple instances of the text. Now a number of VIM commands can be preceded by a lowercase G which subtly altars their effects. And we'll start using this lowercase G qualifier if that's the right term in this particular case. So if you use lowercase G lowercase P it behaves just like the lowercase P but it leaves the cursor after the pasted text. So that's useful for example if you've copied a word and you want to paste it twice. If you use just the lowercase P then you'll get the two instances colliding with one another. Of course you'll be left on the text itself. But if you use lowercase G lowercase P then it'll look more sensible. You'll get a gap between the two instances. Similarly with lowercase G uppercase P it behaves just like the uppercase P command but it leaves the cursor after the pasted text. Now I thought it was useful maybe at least to reference the fact that what's happening here is when you delete stuff or you change stuff or you copy stuff it's stored in a register. I didn't say as explicitly as I meant to that when you're doing a change command the c various variants of the c command then what happens is you effectively delete the text that your movement specifies and then you go into insert mode and you can type stuff so there's things that was deleted is stored away for pasting. The register that's used is a default one if you don't specify it but a number of other registers exist in them and can be chosen as the source or the destination of commands. Now this is quite a large and complex topic and I'm not going to cover it now because I think this episode is going to be complicated enough but I make reference to it for future shows. I'm not completely clear when I'm going to do it. Maybe in the next episode it might fit better there so the register capability is amazingly powerful but just to say it exists. So quite some examples of cutting and pasting and copying and all that stuff just three just to give you some idea. I hope that you will find that as you use the knowledge you've now gained as you use the the vim editor you will come to appreciate what you can do with these these various commands and we'll try and add more examples of all the various things as we go along. It might be worth actually reserving some time just to do some interesting examples at some stage. So here's the first one. It's a simple use of cut and paste and it's the sequence xp. These are two commands. The lower case x, the lower case p. What this does it swaps the character under the cursor with the one after it. So if you do what I do and type words and stuff with letters the wrong way around. So I type TEH rather than THE. If you were to position your cursor on the E of TEH and then type xp it would turn into THE. And the reason that works is the x deletes the E letter E turning the result into TH. And then it's stored in the in a register and you can paste it with p like a sp which places it to replace it after the cursor which is currently on the H and you get the E after the H. So xp is a great one just get that into muscle memory and you'll find just help using this a lot as you type things in in them in things even. The next one is lowercase d, lowercase w, lowercase w uppercase p. You can use that to swap words. It's not the best way but it does actually do it just to demonstrate. So d, lowercase d, lowercase w, deletes the current word, assuming the cursor's on the first character. So you're on the first character of the word, you type lowercase d, lowercase w, delete to the beginning of the next word. So it will eat up all of that word and the space that follows it. Then in that sequence d, w, p, the next w moves forward one word and then capital p pace the deleted word in front of it. So the effect would be to swap two words. I haven't done an example here but hopefully you'll be able to go into some text and then try it out with d, w, p and then type u to undo what you just did. This you really did want to swap two words. It's not the most robust and reliable I've doing this as I say but hopefully it makes the point that there are these capabilities. The last example, the sequence, lowercase y, lowercase w, capital p, this yanks the current word. Again it's assuming that the cursor's on the first character and then paces it in front of the cursor and thereby duplicates the word. So the y double you means yank up to the end of the word in the words to just before the next word and that gets you to that particular position and then if you use an uppercase p it places the word you just yanked before the cursor. Try them out and see how you go. Now in episode four we saw that then has this concept of text objects so we looked at sentences and paragraphs and how you move around with them. We've already mentioned one with a lowercase y closed parenthesis. There are more than the ones we've seen so far and we'll look at some more in relation to commands and again this is quite a detailed subject and I'm not going to dig all the way through this today and you can look in the the help under motion.txt and there's a link to the online documentation if you want to to look ahead and find out other things you can do. I will be covering them though in due course. So there's a concept of an inner object. We've seen commands like lowcase d, lowcase w and lowcase y, lowcase w which do things relating to a word. Yank from the cursor's position to before the beginning of the next word for example but there's another one which is lowcase y, lowcase i, lowcase w and what that means is yank inner word. The i means inner. At least it's a way to remember it and what it does is it copies from the start of the current word to the end of that word. It doesn't matter where in the word the cursor is positioned. So if them is actually identifying that you're in a word assuming you are and it will yank the entirety of that word from the start to the end. So similarly diw all lowercase deletes that entire word. The inner word thing means it doesn't include any non-word character after the word nor does it include any leading non-word character. So that business about movement we just use the w command it takes you across the inter word gap and but in this case diw does not include that inter word gap it's just the word itself. There are other objects can be used with this inner business. So di and some other movement command will work and you can do that with sentences and paragraphs but I'm not going to look at them all just now because I don't want to overload you but we'll come back to them next the next episode I think might be a smart move. Certainly be be looking at them and re looking at them probably as we go along. The other object uses the letter A which the documentation says is A for an object. I'm not sure I enjoy that because it doesn't make a lot of linguistic sense to me. The eye stands for inner object but anyway I like to think the A is signifying all so I have remembering it but the point of it is that it does a slightly different thing. So Y A W when they're all lowercase it copies the word including trailing white space if there isn't and if you are if the last word is the word you're on is followed by punctuation like full stop or something then it will include the leading white space as well instead I should say. So again it works regardless of whether cursor is positioned in the word it'll take that entire text object and you can do this with other text objects as well as I've said before like sentences and paragraphs but we're going to leave it till leave looking at these till later. So I've tried to make some examples here it's hard to do in an audio format or indeed in a textual format and the long notes here are attempting to help explain this. What I did was I typed out two rows of numbers which are meant to signify the column numbers within a file and then I followed that with a line. The line of text is hacker public radio is dedicated to sharing knowledge full stop and then I'm going to use that as some examples for some examples. So the first example is if the cursor is at column 10 which is the B of public if you then type diw there all lowercase it results in the deletion of the word public and leaves all the spaces so you end up with a result which says hacker followed by two spaces radio is dedicated to sharing knowledge so if we operate on the same original text with the cursor at column 10 as before instead typing diw all lowercase then it will delete the word public but it will include the trailing space so the result is hacker space radio is dedicated to sharing knowledge. Example three is assuming that the cursor is at column 48 which is the w of knowledge if we typed diw all lowercase then this results in the deletion of the word knowledge but leaves the leaving space and the terminating full stop so I've got an example of what the result of that would be. If however we had the cursor in the same position in column 48 and we typed diw all lowercase then it results in the deletion of the word knowledge and the leaving space so that it effectively terminates the sentence. So if you had a sentence that you didn't like very much and it's got extra words on the end you can position to whichever of the first words is of your the superfer's words and then type count diw and it will delete them all and but it won't you won't delete the full stop at the end of the sentence but it will remove any any spaces so it will turn this the sentence into the form that you would want and it will do it nice and easily. So I listed a few other objects that you might want to work with but I've not gone into a lot more detail about them. We've got y is all lowercase which means yank in a sentence so that will delete that will yank all of the sentence from the start of the sentence to up to and including the ending punctuation. So that particular sentence we were just dealing with would have just it would just copied the whole thing even if it was embedded in a paragraph. Y is all lowercase yanks a sentence including trailing white spaces so if that sentence is inside a paragraph somewhere then it will copy out the the trailing spaces as well so you could paste it into another paragraph as long as you positioned it appropriately on the first character of a sentence you wanted to proceed with it. Y-i-p all lowercase yank in a paragraph and that will go from the start of the paragraph to before the terminating blank line remember the paragraphs are separated by blank lines and Y-a-p all lowercase would yank a paragraph including the trailing blank line so it would it will copy the whole paragraph including the line after it. Okay that's enough of that I think just one more little one more small or shorter or simple subject before I go on to look at the configuration file that's looking at joining lines now there are times when you've got two lines adjacent to one another i.e. one after the other and you want to join them together and you can do that by in normal mode simply typing the uppercase j command remember that lowercase j is is a cursor movement command so it's uppercase j for join it can the uppercase j command can be preceded by a count and then it will join multiple lines is a simple example in a moment the uppercase j command places a space between the joined lines what it's actually doing is removing the end of line characters between lines and replacing them with spaces there are a few side effects here there are options you can have switched on which will add double spaces after the end of sentences of course that's the sort of typing convention that some people use and if that sort of feature is enabled then the uppercase j command will will use that option will honor it was what I was grouping to say now there's a lowercase j can proceed the uppercase j which remember as a way of enhancing the if functionality of a command and it joins lines but it doesn't add any spaces so i've quite an example here which shows the two the effect of the two things we've got an imaginary file which contains the words hacker public and radio each on a separate line if we position the cursor on the first line and typed three uppercase j then it will happily turn it into hacker space public space radio if you did the same with that file but used three lowercase g uppercase j you would get hacker public radio all there's one word no spaces so traditionally you can have a tradition after four episodes we've been structuring these things by introducing various commands then going to look at configuration file additions well i'm going to do the same here in episode four the file we've got fairly long and i'm not going to reiterate the whole file we've got so far i'm just going to be adding to it but as an inclusion with this episode there's a file called example underscore vimrc underscore five with the five reflects it relates to episode five and you can download that and check it out and it's got everything we've done before and also what we're going to add today so the first thing we're going to add to the config file is the thing that enables syntax highlighting now vim syntax highlighting is built in capability though it can be enhanced we'll look at that much later on how you can enhance these things it already cared us for a whole range of of languages and formats and so forth but it's good to switch it on and the option you want is simply syntax space on you need to put that in your configuration file and you will get syntax highlighting if you've got a if you're doing this on a terminal that has got color capabilities and you'd be hard pressed to use one that doesn't i would imagine then you will get different coloring depending on what it is you're you're editing now if you're if you are typing stuff maybe you're typing lists of things or you're in a programming language or whatever indentation could well be relevant to what you're doing so if you've indented a line while you're typing and then you start a new line it will you can make vim automatically move the add an indentation so we say to that new line to copy the one above so that's really useful it's a really useful feature and you can switch it on with the option set space auto indent and with these these some set commands they usually have an abbreviation set can be abbreviated to sc and auto indent to ai so you can type sc space ai remember if you remember back to previous episodes i'm telling you this but suggest that you use the full form in your configuration files because it makes it look easy to remember what it means you can switch it off with set space no auto indent or the short form would be s-e space no ai n-o-a-i you do that from the command line if you needed to for example this brings us on to wrapping wrapping of lines that is if you're typing away into insert mode by default in a brand new vim you will just keep typing until you hit a new line press the entity or whatever but you can make vim do an automatic wrap to the next line by adding a line break and it will do this at a particular point which you have defined and it will do it when the defined line width has reached and the current word is completed so it doesn't split words which would be very very very irritating thing about this automatic wrapping though is that if you have a line which is already as long as whatever width you've set and you go and edit it and add a whole bunch of words and stuff and text in the middle of it making it exceed that width vim will not then wrap it because it only does it as you're typing we will look at how to re-implement the wrapping at a later stage we're not going to cover that today but it will um well only do that when you typing stuff basically at the end of the line so you define the maximum width of text that that width which triggers the wrapping with the the command set space text width equals and then a number so for example one I prefer personally is set space text width equals 78 you can abbreviate it text width is abbreviate to TW and you can switch it off with setting text width to zero and is off by default now when you're typing stuff in insert mode and you press the tab key then vim will add a tab character and that causes it to move according to the appropriate tab stop just like it comes from the old typewriter days where you had physical tabs and your typewriter and the carriage moved to those when you hit the tab tab key now this takes us into an area of whether you actually want to have tab characters in your file or whether you'd sooner have things to spaces in there to indent stuff and it's it's a bit of a contentious area for various reasons which I won't go into today in fact I'd like to spend some time looking at this subject in a later episode because that gives you all the factors making your decision on which way you want to go with this and tell you the choices I've made and so we'll do that later there is a way in which you can ask vim to replace tab characters with spaces and you can also ask vim to make its increments of of indenting four columns as opposed to the eight columns that you would get with a tab the two configuration options that control these things are called expand tab and shift width expand tab forces all tab characters would be replaced by the appropriate number of spaces in insert mode you just turn it on or off set set expand tab will switch it on and set no expand tab will switch it off and you can abbreviate these to ET to go on and no ET to go off if you're editing a file that's already been typed in which contains tab characters the set this setting will not affect those characters it's only ones which are around being typed in that are affected you can go through the file and change all the tab characters to appropriate numbers of spaces with a command mode command colon retab but we'll cover that in more detail when we look at this subject in more more depth so controlling the number of spaces to use for auto indenting is achieved with the shift width option you follow it with the with the number so you might type set space shift width equals four the auto indent step is four spaces that can be abbreviated to SW equals four as I've shown there's quite a lot to be said about this whole subject but we're going to leave it for now and we'll be when we do look at it in more detail we'll be looking at the tab stop and soft tab stop commands and how you can use those there is something that we you need to have some indication of as I've referred to it here when you're in insert mode and you're typing typing stuff in you will find that them will copy you've got auto indent on them will copy the indentation of your previous line and you might want to undo that well you can't just type the the backspace key to to go backwards but you could also use control d to achieve the same thing which skips back whatever the shift width number is of columns if you want to add further shift width numbers of characters then control t will add further indent so I'll come on to these in more detail in a in a subsequent episode and show you some of the other ways of achieving similar things but we'll we'll reserve that for probably the next episode when we talk about tabs and spaces in more detail the last element to add to the configuration file in episode four we looked at searching and we used the options ink search and hl search ink search caused it caused the search target to be moved to as you type to start to type the search into the editor and hl search highlighted all of the targets as you once you set the search in motion now if you're in the middle of doing an edit and you search for a thing I don't know if you search for the word then or something and you find it and you do whatever you wanted to do with it all of these search targets are going to remain highlighted for the rest of session till you switch it off switch them off and I said that it's possible to use the command mode command colon no hl n ohl which is turn off the highlight but it gets a little tedious to do this because it it can be you can get a lot of highlighting with certain searches and you'll see that it's relevant to a lot of other things we'll be doing in the future so what I suggest you do is to add something to your configuration file which will make it easier to switch it off and one of the the commonest things to do is to set things up so that if you type control l control l is a is a sequence which which is used to refresh the screen it's done in a lot of screen-based apps do this control l is generic refresh the screen well what I'm suggesting here is a mechanism which will not only refresh the screen but it will also turn off all the highlights this is using a feature we've not looked at at all yet so it's one of these things you can just copy and paste into your config file and we've come on to how it works what it does at a later stage essentially what's happening in brief is that the the control l sequence the control l character it would be is being redefined remapped so that when it's issued not it does two things it issues this no hl command and it also does what control l normally does so you'll see the line here I won't read it out but you just type that in exactly as it is or you can come and paste it out of the example file that I've provided okay so that's everything now the remaining bits in the in the notes are a summary of all that we've covered today which I'm not going to go over but it's just there is an aid memoir if you find it useful there's the various things we're going to add to the config file I've included the set hl search in here as well because I think it's nice to have the the definition of control l immediately after the set hl search of course the two things are related but that's just me you'll see the same in the in the example file that I've given you so that's it and I've included these notes in ePub form in case that's useful we'll try and get another episode out before too long certainly not nearly two years gap between this one and the neck okay but I just know you've been listening to hecka public radio at hecka 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 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 binwreff.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 stated today's show is released on the creative comments attribution share freedom licenses