Lessons: 34Length: 2.5 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

5.4 Joining Lines

Sometimes, you'll find that the plugins you install override existing shortcuts. For example, if I go to Edit > Line > Join Lines, you'll notice that there is no shortcut assigned. But there is one when you first install it and that's Cmd or Ctrl+J. But if I try this, One, Two, Three. I select them, I hit Cmd+J. No, it doesn't work. So I need to figure out a new key binding for that. I'm gonna go to Sublime > Preferences > Key Bindings Default. And this will present me with all of the various key bindings that are automatically assigns by the editor. So I'm going to look for join_lines. There we are. And yep, it is set to super+j, but something must be overriding it. So rather than changing it here, remember, try not to edit the default settings. Because when the editor is updated, those will be overwritten. Instead, I'm going to copy this. Switch back into my user settings, which will not be modified. Now within here, I'll paste it in. So this is simply a JavaScript array. Within here, we have an object with a key for keys and then we setup what we want. In our case, I'm gonna go with Ctrl+J. Then when I press Ctrl+J, that's going to trigger the command, join_lines. We will get rid of the comma, because we only have one object in this array. And now if I try it again, I've selected these items. I press Ctrl+J and now it has joined them together. So if you're thinking, I'm not sure when I would use joins, there are actually a handful of clever ways that you could. Maybe you're creating a JavaScript array. I will create a new file for this, script.js. Normally, you would say var arr for array and then you would have to do a tedious process where you add all of these with end quotes, like so. Granted. It's not too bad, but it's still a pain in the button. Here's another way that you might do it using joins, you would put each of them on their own line. So one, two, three, four, five. I will select all of them, then press Shift+Cmd+L to create multiple cursors. So now each line has its own cursor. I'm gonna wrap the selected text on each line with a single quote just by pressing single quote. Then I'm going to move to the end of the line by pressing Cmd or Ctrl+right and we'll add a comma in. Next, we will hit Ctrl+J and that's going to join the lines together. And now I can escape out of the multiple cursors. Then at this point, I can remove the final comma. Select the entire line, wrap it within an array and then create our variable, like so. So granted, that seemed like a lot of steps. [LAUGH] But actually, you may find keyword may, that this can be faster. I've actually gotten to a point where I can do it fairly quickly. So another way I might do this is I would create the array and then I would do one, two, three, four, five, six, seven, eight, nine, ten. I'm gonna select all of them Shift+ Cmd+L surrounds them with a single quote. Add a comma at the end, Ctrl+J to join them together. Then back to the end of the line. Backspace to bring this up. Selected the whole thing, wrap it within an array and then get rid of that closing comma. So clearly, this would only be advantageous if you have big arrays. But it's still an option and it gets you into the idea of thinking about how you can use joins better. A more common application might be that you have some text. So I will past in some Lorem text right here. And then maybe here, you have some more Lorem. So then at this point, you wanna join these together. Well, all I have to do is place my cursor where I want the next lines to be joined and then press Ctrl+J and now those have moved up. Now what's cool about this is if this happens to be on a much lower line, it's the same thing. Just hit Ctrl+J a handful of times to automatically bring that up, which is much quicker than probably having to scroll down, then hit the Backspace until you get it to where you want. Here's one last example. Maybe I have a JavaScript object like this and we'll say, name equals Jeff, age is 27. Occupation: web dev. Maybe you have some spaces right here like that. Well, if you're up here, press Ctrl+J to bring each of those up. But here's another cool thing. What if you want this to be all on one line? Well, if press Ctrl+J again, that is going to bring name up. But if I do it again, now it's going to bring the next one up and the next one and the next one up. So maybe you have an array, like this. Once again, one, two, three and four. If you want to put all of this on its own line, just do Ctrl+J a handful of times and you're done. Pretty neat, right?

Back to the top