 Even after all these years, I still find myself constantly amazed by some of the things that you can do in Emacs. I'm constantly learning new things, and some of these amazing things are things that are actually built into Emacs that I'm just now discovering, and some of them are third-party packages that I've installed and played around with. Today what I wanted to do, I wanted to share a couple of programs within Emacs that will blow your mind as far as being able to move text around, especially moving words, moving single characters, moving lines, moving blocks of text, all around the document. So let me switch over to my desktop and let me go ahead and launch Emacs, and I'll open a file to play around with. I'll just open up one of my recent files, like config.fish, which is of course the Fish Shields config file, and I'll zoom in. And the first thing I want to show you is this really cool built-in function in Emacs. If I do a meta-x, the name of the function is transpose-words, and this is really cool because what it does, it transposes the words around the point. So basically the point is worth a cursor point is, and transposing the words, it means transpose the word that's under the point with the next word essentially, or the word before it, depending on exactly where you have it. For example, if I'm on L, which is the very first character of the word line, and I do meta-t, alt-t, because that's the built-in key binding out of the box with Emacs for this transpose-words command, alt-t, you can see what happened. The word line and the word first swapped places, right? If I do U to undo, now if I was on any other character on the word line, other than the first, it will actually transpose line with the word after it, which is removed. So if I do meta-t on that, you can see it moves line forward, and it swaps line with the word after it, which was removes. So, and I believe it would work all the way until the blank space right behind line, that still, if I do meta-t, would swap line with the word removes U to undo here in evil mode. Now, what's really cool is I could keep transposing this word. So if I did meta-t to move line forward, right? So move the word line forward one, move it again, move it again, move it again. It keeps swapping with the next word right behind it, right? So you can actually move the word much further along than just, you know, the swapping it with the next word. Now, most of the time when you're doing a word swap like that, you're typically just transposing the word one time, especially this probably isn't something you'd use all the time with programming, for example. But for creative writing, you will often want to transpose the order of two words, you know, depending on maybe you want to rearrange the way you wrote something, you know, move the verb and, you know, the adverb and the preposition, you know, rearrange exactly the order of the way you stated things in a sentence. So this transpose words function, I love it. And I think going forward, I'm going to abuse the hell out of this function. I'm going to use it all the time. A matter of fact, I'm going to use it so much that when I'm on a computer that doesn't have Emacs installed, most Linux and Unix-like operating systems do have VIM installed out of the box. And I know VIM doesn't have this transpose words functionality because you have to jump through some steps to do this transpose word thing and a VIM document. Let me show you what I mean if I swap workspaces. Let me open a terminal. Let me zoom way in and let me do a VIM dot bash RC. Now my VIM is actually Neo VIM, but VIM Neo VIM is the same functionality. But let's imagine I want to transpose the word bash with the word after it config. Well, I would have to do five different key presses. I think like a minimum five key presses to do this. I would have to do DW to delete word. And then I would have to get behind the word that I wanted to transpose. In this case, I actually have to do something with the period. So I would have to go forward to the next word, so W to the next word, which in this case is the period, which is exactly where I want to be. But then I'd have to go back one. So H on the keyboard to go to the left, but then I'd have to paste. But then I'd end up with a space that I don't need. And then I had to add a space that I don't need. So actually, because of that period being where it was, that actually makes that makes that a little more complicated than it needed to be. But, you know, you're talking about five or six key presses typically to swap two words, right, to transpose two words that are next to each other in VIM. And of course, with punctuations, I hear with periods, but, you know, dashes and hyphens, what about, you know, carriage returns and things like that? It can get kind of complicated, right? So, you know, I started doing some searching. Can I get this transpose words functionality that I have in Emacs? Can I get that in VIM? And there is a page over on VIM.org about adding this exact functionality. What you could do is you could remap a key binding. Well, you could, you know, create a key binding for this in their example, their key binding GW, but you could, you know, make that anything. Let's imagine you're making it of a meta T, right? Alt T. And you can see the code to set is this massive string. That's the actual VIM code, right? Because what you want to do is you want to run the command underscore YIW, so the underscore is taking care of the blank spaces. YIW, yank inside word, and then colon S, colon S, colon is getting into command mode, S is a substitution. Then you've got a whole bunch of regex stuff, right, that it's doing here. CR, I think that has to do with the carriage returns. I don't play around with VIM script much, but, you know, this is what you'd have to do to add that transpose word functionality to like your VIM RC or, you know, your Neo VIM config. But what I found was here at the bottom of the page, they also had some recommended plugins. And one of them is actually called transpose words plugin. And it's essentially the same as the Emacs transpose word program. So what it does is meta T, right? Does exactly what it does in VIM as it would in Emacs. So you go to this page and you grab the trans word dot VIM package. And that's trans word with no O, there's no letter O in the word. So trans word without the O dot VIM, grab the latest one. One dot one is the version. Just download that file. And then all you need to do, I go back to this here and let me open up my graphical file manager. You need to drop that plugin in the plugins folder for VIM, or in my case, Neo VIM. So I'm going to go into my hometown folder dot config slash in VIM slash after slash plugin. And then in this folder, I dropped trans word dot VIM. And now that I have that there, if I get back into a terminal, let me zoom way in. And once again, I'll open my bash RC here in Neo VIM. And now let me go to the first letter of the word bash here in my bash config. And if I do meta T, you can see bash transposed itself. So it places with itself with the word before it in this case, which was the word my, because I was in the first letter of bash with the cursor point. Now, if I was at the end of the word, let's go to the blank space behind bash and meta T bash switches with config. If I was somewhere in the center of the word and did meta T, it actually switches with my. So it does look like the functionality is a little different with the trans word dot VIM plugin. Because if I'm in the middle of the word here in Emax and do meta T, it does switch with the next word. But in VIM, it looks like it's always going to switch with the word before it, unless your cursor is actually in the blank space after the word. So that is something to be aware of. But for the most part, the same functionality, just a little bit different. But now let me show you a third party Emax package that I discovered the other day. Let me go back to the browser here and over on GitHub. I found drag stuff dot E L. So drag stuff, as you can imagine, is you're grabbing words, a region, a block, whatever, a block of text, and you can move it around with ease because it has these built in key bindings. And what it uses is meta up, meta down, meta right, meta left. So the alt key and then one of the arrow keys. And as long as you've got whatever block of text highlighted, it just moves it over, you know, that amount of cursor points, essentially. Let me show you this in action. So if I get back into Emax, for example, and let's imagine this line here, maybe I want to move this line around. Well, I could do a visual line mode. So if I do shift V and I've got that line selected, now watch what happens when I do alt up, it moves the line up, right? It basically swaps that line with the line that was there. If I do alt up again, you see it moves it up ahead of that line, right? And if I do alt down, I can move it back down to where it was. So now let me try something with moving something left and right. Let's see, if I do a, let's do a visual block mode here and I select something. Maybe these three words here removes line the, which some things are still out of order for where I played around with the transpose word command earlier. But let's do meta and then the right arrow key. And if I do that, I'm not exactly sure what it did. If I keep hitting meta left, it's moving the word path. And if I do left, it'll move that. So I think that's a problem with visual block mode and evil mode. What if I just selected something though with the mouse? So let me select it with the mouse and do alt and then right. And then, okay, this is the functionality as it's supposed to exist. It moves it a cursor point at a time. And that's, that's neat. That is really, really cool. I could definitely see myself using dragstuff.el all the time as well. Now, let me go ahead and you to undo and see if I can actually get all of this back to how it read before I started this video. Yeah, first line removes the path, the way that line should exist. And actually, I should have showed you guys how to install dragstuff. So let me actually open Emacs once again and let me go to my config. And I have this dragstuff section in my config now. If I zoom in, basically it is just using the use package block that you typically would use to install packages in Emacs. So use dash package drag dash stuff. That's all you need to do. And then you actually need to make dragstuff a global mode, meaning enable dragstuff everywhere. So you have colon and yet. So set these before dragstuff is initialized. Turn the global mode on. That's what one means. It means turn the mode on. And then you also want to enable drag dash stuff dash define dash keys. So this sets those four default key bindings, which are meta up, meta down, meta left, meta right. That way you don't have to define those yourself. Now before I go, I need to think a few special people. I need to think the producers of this episode. And of course, I'm talking about Gabe, James, Matt, Paul, Steve, Wes, Armor Dragon commander, Ingrid, George Lee, Methos, Nate, Erion, Paul, Peace, Arch, and Vador, Realities for Lust, Red Prophet, Roland, Soul, Last Street, Tools, Devils of Origin, Two, and Ubuntu, and Willie. These guys, they're my high-steered patrons. Over on Patreon without these guys. This quick look at these really cool Emacs programs. It wouldn't have been possible. The show is also brought to you by each and every one of these fine ladies and gentlemen. All these names you're seeing on the screen right now. These are all my supporters over on Patreon because I don't have any corporate sponsors. I'm sponsored by you guys, the community. If you like my work and want to see more videos about Linux for an open-source software and Emacs, subscribe to DistroTube over on Patreon. These guys.