Translation of foreign articles
Translation of foreign articles
Translation of foreign articles
1. Fast and automatic indentation
This shortcut is very useful when your code is not aligned.
Control + I / ⌃ + ICopy the code
It will automatically indent the line where the cursor is. If you select some code, or even an entire file, this shortcut adjusts the indentation of the selected part.
This is very helpful in keeping your code clean in a timely manner.
2. Modify in all scopes
Suppose you find an error in a method or variable name and you want to fix it. Of course, you don’t want to change them individually, because you know there’s Refactor for batch renaming, but sometimes Xcode’s Refactor can be a bit tricky. At this point, you can use the following shortcut keys to select all locations in the current file where the variable is used.
Command + control + E / ⌘ + ⌃ + e
This will select all the places where the variable is used, allowing you to easily change the variable name.
3. Find the next one
Now, suppose you don’t want to change variable names in all scopes, but just want to find the next place; Or you just want to rename it in a function, not the whole class, or something like that. There is a shortcut very similar.
Option + Control + e / ⌥ + ⌃ + e
When you select a string, press this shortcut and Xcode will select the next location where the string will appear. But that means that if some variable has the same name as a function, the next one you pick might not be what you expected. It’s not really the same variable, it’s just a string match.
4. Find the last one
Above we introduced “find next”, press one more key, it becomes “find previous”.
Shift + Option + Control + E / ⇧ + ⌥ + ⌃ + E
5. Move the entire line up or down
We might make some order adjustments to the code, of course using the classic “cut and paste”, but if we just want to move the code one line up or one line down, the following shortcuts will definitely help. Move up:
Option + Command + [/ ⌥ + ⌘ + [
Move down:
Option + command +] / ⌥ + ⌘ +]
Bonus tip! You can move multiple rows. If you select multiple rows and then use the previous shortcut, the rows will move as a whole.
6. Multi-line cursor (using mouse)
Sometimes you need to write the same thing in different parts of the file, and you get annoyed because you have to write it once and copy and paste it several times. All right, enough with that. You can write multiple lines simultaneously using one shortcut key.
Shift + Control + click / ⇧ + ⌃ + click
7. Multi-line cursor (using keyboard)
This shortcut is basically the same as the previous one, but instead of using the mouse to select the cursor position, we use the arrow to move the cursor up or down.
Shift + Control + Up or down /⇧ + ⌃ + ↑ or ↓
8. Quickly create an init function with multiple arguments
The above shortcut, one of my favorite uses, is to quickly create an initialization function, faster than any of the previous methods.We can quickly create an initialization function by using a multi-line cursor in conjunction with other shortcuts, such as copy and paste or selecting the entire line. This is just one of several uses for this button.
8.1 Another way
There is also an editing feature that lets you easily generate a “Memberwise Initializer.” You can place your cursor over the class name and go to Editor > Refactor > Generate Memberwise Initializer. However, since this article is about shortcut keys, here is a tip: You can go to Preferences > Key Bindings, look up the corresponding command, and add shortcut keys. Here’s an example:
9. Return to the previous position of the cursor
Sometimes you need to deal with large files, and it may be difficult to find your way back after scrolling up to see something. With this shortcut, we can quickly jump back to our previous position as long as we don’t move the cursor away.
Option + Command + L / ⌥ + ⌘ + L
- Skip to a line
Related to the previous one, if we know the line number of the line we want to jump to, then using this shortcut we can jump directly to that line.
Command + L / ⌘ + L
These are the ten shortcuts and tricks I use every day to use Xcode effectively. They often come in handy. I hope they’ll be as useful to you. If you already know these shortcuts, or don’t, you can communicate with me, I would be happy. Feel free to share with me any other useful shortcuts you’ve used. Tip Ideally, you can use the same keyboard shortcuts for all of the techniques mentioned above. But depending on your operating system language Settings, some of them may be slightly different. You can go to Xcode > Preferences… > Key Bindings view Key combinations of specific shortcut keys. Bonus tip! Quickly open Preferences
Command +, / ⌘ +,