- Overview
- Transcript
5.4 Joining Lines
Likely, you already know that you can join lines; but do you know when you'd actually do so in a project? I'll show you a few options in this lesson.
1.Introduction1 lesson, 02:05
1.1Welcome02:05
2.Getting Started9 lessons, 35:20
2.1Installation and Base Settings04:54
2.2Services and Opening Sublime From the Terminal02:20
2.3Multiple Cursors and Incremental Search06:54
2.4The Command Palette04:13
2.5Instant File Changing03:19
2.6Symbols04:17
2.7Key Bindings02:33
2.8Installing Plugins Without Package Control02:54
2.9Package Control03:56
3.Snippets3 lessons, 14:40
3.1Your First Snippet09:04
3.2Adding Snippets Through Package Control02:32
3.3Easier Testing With Snippets03:04
4.Essential Plugins12 lessons, 46:58
4.1Zen Coding07:09
4.2Emmet06:52
4.3Cross-Browser CSS With Prefixr02:17
4.4Fetch Files With Ease 04:22
4.5Lightning Fast Folder and File Creation 02:12
4.6Sidebar Enhancements03:09
4.7Sublime Linter02:01
4.8Sexy Code Snippet Management With Gists07:50
4.9DocBlockr03:49
4.10Pretty Task Management02:42
4.11HTTP Requests Within Sublime02:29
4.12LiveReload02:06
5.Tips, Techniques and Modifications8 lessons, 49:09
5.1Regular Expressions in Sublime05:49
5.2Vintage Mode10:46
5.3Quicker Stylesheet References02:30
5.4Joining Lines04:40
5.5Sublime and Markdown with Marked03:10
5.6All About Projects 05:54
5.7Configuring and Mastering Split Windows07:19
5.8Custom Builds09:01
6.Closing1 lesson, 00:49
6.1Conclusion00:49
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?