Editing Tips & Tricks
Learn some of the most useful Editing features of GoLand.
Editing code is a frequent activity that programmers do. However, editing is not only about typing the code but also navigation in a code-base.
If IDEs are good at one thing, is that they can help automate various tasks, including writing that tedious boilerplate code.
In this playlist we're going to have a look at many of the code editing features in GoLand, and how to effectively use them.
Implement an Interface
Implementing an interface is a common task that Go programmers need to do.
GoLand offers the option to quickly implement an interface using the Implement methods action.
^I (macOS) / Ctrl+I (Windows/Linux), and then select either to generate a new type or use an existing type from the current file.
Structure Tags
Quickly add tags to any fields from structures.
GoLand helps you add struct tags using custom live templates built into the IDE.
Adding tags to a structure can be done by starting to type either json or xml after the field type.
Custom Structure Tags
Add custom tags to any fields from structures.
GoLand has a few Live Templates builtin for struct tags, such as json or xml.
You can add your own structure field tag by going to Settings/Preferences | Editor | Live Templates and then select Go Struct Tags.
Then you can either customize an existing template, duplicate it, or create a new one.
Generate a Test for an Element
Automate test generation
GoLand offers the option to quickly generate a test.
Invoke with ⌘⇧T (macOS) / Ctrl+Shift+T (Windows/Linux), and select Test for ... from the popup.
You can choose to test a function/method, the whole file, or the whole package.
Expanding/Shrinking Selection
Use your keyboard to quickly select the portions of code that you need.
Use Extend Selection to quickly select parts of your code using semantic knowledge about it.
Selected too much, or using an already existing selection, and want to reduce the selection scope? Use the complimentary Shrink Selection feature.
Press ⌥↑ (macOS) / Ctrl+W (Windows/Linux), to Extend Selection.
Press ⌥↓ (macOS) / Ctrl+Shift+W (Windows/Linux) to Shrink Selection.
Edit Text in Multiple Places at Once
How to use multiple cursors to code faster in JetBrains IDEs.
Want to edit the same thing, in multiple places, at once?
Multiple carets to the rescue. Click the first occurrence, then invoke "Add Selection for Next Occurrence" ^G (macOS) / Alt+J (Windows/Linux)for each change. Type the replacement. Press ␛ when finished.
Or, all at once by invoking "Select All Occurrences" ⌘⌃G (macOS) / Ctrl+Alt+Shift+J ( Windows/Linux). Then, type the replacement as before.
Works in all languages and IDEs. Other tips:
- To add or remove multiple carets, you can also press and hold the ⌘ (macOS) / Alt ( Windows/Linux), then click to add a new caret. You can also make a multiple selection by holding Shift+Alt and dragging the caret.
- To remove all the carets, press ␛ (macOS) / Escape (Windows/Linux).
Select all Occurrences in a File
Quickly select all occurrences of a piece of text in the current file
Do you want to select all the occurrences of a piece of code?
Press ⌘⌃G (macOS) / Ctrl+Alt+Shift+J (Windows/Linux).
After the selection is complete, you can start editing all the fragments as if they were all the same one.
If you plan to use this feature to rename something, then you should have a look at our Rename refactoring, which is designed to work safely across multiple packages, not just at the file level, with full preview and undo support.
Navigate to File
Jump to a file without using the Project Tool Window, and your mouse
The Navigate to file feature allows us to search for a file by name, even if we know only part of it.
You to search for a file in the current project only, your source code only, or all places. This means you can search in other places like Go Modules dependencies and GOROOT.
How to use:
Press ⌘⇧O (macOS) / Ctrl+Shift+N (Windows/Linux) to invoke Navigate to file.
Pro tip:
As a hidden gem, try finding a directory by name.
Navigate Between Open Files
Don't use the mouse to quickly move between multiple files, use the switcher instead.
The Switcher allows you to quickly move between open files during your editing session.
Invoke the Switcher using ^⇥ (macOS) / Ctrl+Tab (Windows/Linux).
You can use it to quickly switch between the previous file and the current one using the shortcut.
If you want to browse the whole list, use the shortcut and hold Ctrl on Windows/Linux or Shift on macOS. You will be able to use ^⇥ (macOS) / Ctrl+Tab (Windows/Linux) again to move forward through the list of options, or press ^⇧⇥ (macOS) / Ctrl+Shift+Tab (Windows/Linux) and move backward.
Use Recent Files
Jump to any of the files you've been using recently in your JetBrains IDE.
The Problem
Have you ever been deep into a project, only to realize that you forgot to include something crucial in a file? We've all been there, so let's learn how to prevent this from happening to us again. While the tabs at the top of your screen are helpful, they can be a little bit overwhelming.
The Solution
Instead of searching for a certain tab, let’s use our IDE. One convenient way to find certain files is using the Recent File Window. It gives you a nice list of all the files you have visited recently. It is automatically sorted in the order you have visited them, which makes going back so much easier
Well, that is already a great improvement, but we can do even better! What if the file you are looking for is at the very bottom. Well sure we could press arrow down like a hundred times, or worse use the mouse, or we just type directly into the window, which lets you search for a particular file.
Wouldn’t it be nice if you could narrow it to only the files you edited, not all you visited? In the upper right corner, you see the “Show edited only” files. This will help you remove some clutter and hides the files you just visited without doing any changes, and you can even open a certain file in a split window to have a look at multiple files at the same by clicking on the icon on the right side or pressing Shift or Enter.
I got one more for you, let’s check it out! If we look to the left side of our Recent Files Window we see all the amazing tools we recently used. You want to jump to the Terminal? Just type in “T-E-R” and it will take you right there.
Development is hard! and having a good overview of edited files is difficult. Luckily our IDEs make navigation really convenient!!
Recent Locations
Jump back and forth between files using code snippets you've recently seen.
The Recent Locations pop-up allows you to search for a specific change or recently viewed piece of code.
Invoke it using ⌘⇧E (macOS) / Ctrl+Shift+E (Windows/Linux), and the list of recently viewed files will display not only the file name but also the code snippets around the area last viewed in the file.
Invoke the same shortcut again, and only the changed files will be visible.
The best part? Speed Typing works here: you can type in code to filter the results further.
Activate the Navigation Bar
Turn the navigation bar on when you need it and dismiss it when you're done.
The Problem
Getting into the flow means stripping down your UI distraction (turn off the Project tool window and Navigation Bar) then going keyboard-centric to avoid the mouse. But you still need to get to your files, and sometimes you'd prefer to browse instead of invoking "Recent Files" ⌘E (macOS) / Ctrl+E (Windows/Linux) or "Find File" ⌘⇧O (macOS) / Ctrl+Shift+N (Windows/Linux) action.
The Solution
In the latest version of JetBrains IDE's, the Navigation Bar is in the Status bar by default, but you don't want to use your mouse to get there, or perhaps you've already hidden it altogether as part of a lean UI.
You can invoke Navigate | Jump to Navigation Bar ⌘↑ (macOS) / Alt+Home (Windows/Linux) to temporarily re-activate the Navigation Bar. Once you use it, the window disappears.
But then, how do you make it disappear? Press ␛ (macOS) / Escape (Windows/Linux) to go back to where you were working.
How did we hide it in the Navigation Bar first place, or bring it back if wanted? From the menu, you can select View > Appearance > Navigation Bar which allows you to toggle between Top, In Status Bar and Don't Show.
Want a faster, keyboard-centric way to toggle? ⌘⇧A (macOS) / Ctrl+Shift+A (Windows/Linux) lets you search for such settings and even toggle inline. Type vi nav
to speed search, make sure the selection is on View: Show Navigation Bar, then press ⏎ (macOS) / Enter (Windows/Linux). The setting is toggled...no mouse usage needed.
If you want to know what else the Navigation Bar can do, check out Navigating Like a Pro.
File Structure Popup
View the file or whole package structure and navigate to any element.
The File Structure popup window allows you to get an overview of all the elements defined in a file or a package.
You can use it to navigate to any definition and invoke it from anywhere.
Use ⌘F12 (macOS) / Ctrl+F12 (Windows/Linux), and a window with all the elements defined in the current file will show up.
Press ⌘F12 (macOS) / Ctrl+F12 (Windows/Linux), again with the window still open, and it will show all the elements defined in the package of the current file.
Speed Typing can then help you to narrow down the list of elements to the one that you need.
Select In
Select the current file in the Project or Changes view and more
The Select in... action helps you select the currently selected file in a particular tool window.
It works in places such as the Project or the Changes view tool windows, and even in Explorer/Finder
Press ⌥F1 (macOS) / Alt+F1 (Windows/Linux).
Go To or From a Test
Navigate to and from a test subject like a method or function.
Use the Navigate | Test feature to quickly go to a test for an element.
Press ⌘⇧T (macOS) / Ctrl+Shift+T (Windows/Linux).
Press the same shortcut on a test, and you'll be able to jump back to the element that is the subject of the test.
Type Hierarchy
Discover what types implement an interface or what interfaces are implemented by a type.
The Type Hierarchy feature allows you to answer questions such as "What interfaces a type implements?" or "What the types are that implement an interface?"
Invoke it using ⌃H (macOS) / Ctrl+H (Windows/Linux).
You can dig deeper through the list of hierarchy to get as much information as you need.
Working with the Call Hierarchy
Analyze call trees for functions or methods
Call Hierarchy allows you to quickly see all the places where a function or method is used.
Press ⌥^H (macOS) / Ctrl+Alt+H (Windows/Linux), and the Call Hierarchy tool window will open up with all the places that the function is called inside.
This works for interface methods, type methods, methods, and calls.
You can then drill down into the call chain to see where those other functions are called.
Speed Typing is available to help you quickly navigate to the element you need.
Show /usages
Quickly discover the usages of the element your cursor is at
The Show usages feature allows you to quickly answer the question "Where's this element used?"
It will open a pop-up that you can quickly use to navigate/filter or otherwise dismiss with the results you are looking for.
Press ⌥⌘F7 (macOS) / Ctrl+Alt+F7 (Windows/Linux).
Compare With Clipboard
Compare text, files or directories and even use the clipboard contents.
GoLand has a built-in diff viewer for code, revisions, and even images.
Select any pair of files and press ⌘D (macOS) / Ctrl+D (Windows/Linux).
To quickly compare the editor with the clipboard, choose View | Compare with Clipboard.
If you have selected a single file, the IDE will prompt you to select another file to compare it to.
Paste from History
Paste from the clipboard even if it's not the last item you copied into it
The Paste from History feature you to use the (limited) history of everything you put in the clipboard.
Press ⌘⇧V (macOS) / Ctrl+Shift+V (Windows/Linux) to open pop-up window.
Switch to the Editor
Get the focus back to the editor regardless what is currently focused.
There are a lot of (tool) windows opening or appearing when using the IDE.
How can we quickly get back to the editor?
We can press Esc, and the focus will switch back to the editor.
The tool window you're switching from will preserve its visibility state in this case.
Hide all Tool Windows
When you want just your code you can hide all the tool windows in the IDE.
You can hide all the tool windows in the IDE and quickly switch to the editor via ⌘⇧F12 (macOS) / Ctrl+Shift+F12 (Windows/Linux)
Speed Typing
Quickly navigate to or filter any list of items in the IDE
Speed Typing is the ability to type anything in a list and have the results filtered.
You can then use the arrow keys to move through the list, or press ␛ (macOS) / Escape (Windows/Linux) to dismiss the filter.
How to Search Everywhere in Any JetBrains IDE
Search for anything in your JetBrains IDE across Classes, Files, Symbols and Actions.
The Problem
How do I find stuff, without memorizing a bunch of stuff? You’re editing something. You want to jump to something in your project. The IDE has so many options. What’s the fastest way?
You could browse through the file tree to find the file. Similarly, activate the navigation bar and wander around. Lots of other choices: Recent Files, Recent Locations, bookmarks, keeping a thousand tabs open.
The Solution
Press ⇧⇧ (macOS) / Shift+Shift (Windows/Linux) for Search Everywhere. The one IDE entry point to find everything. Type a string, make a selection, and jump to the target.
To dismiss the popup, press Escape. You can jump to a file by putting in the name of a file, not something in the file. You can type parts of a file path, to save some time. Those tabs show the kinds of things that can be searched. Hit tab to go Classes, then again to go to Files.
Mouseover on the tabs to show key bindings to open directly. “Symbols” is a favorite. Actions…the next tip. Want more power? Here’s three bonus tips:
- This checkbox controls how widely Search Everywhere goes looking. Press ⇧⇧ (macOS) / Shift+Shift (Windows/Linux) again to toggle it to checked, then again to unchecked.
- Use filter… to restrict results by certain criteria, per-tab.
- Click Open in Find Tool Window to get the full find treatment.
Yes, you can resize this window, as if it was a normal window. Next time you use Search Everywhere, it will remember the dimensions. Last point: the footer area has…tips! As you can see from these, lots of power in Search Everywhere.
Here’s one you didn’t see coming: Search Everywhere is also a desktop calculator.
Search Everywhere is the perfect first thing. Super-easy to learn, immediately useful, and gradually scales to more power.
Use Local History to Avoid Disaster
Use the IDE's built-in history facility to recover local changes when you haven't committed to VCS.
You’re in your repo. You add a file. Later, you make some edits, but then -- you change your mind. You want to revert but you never committed.
Use the IDE’s local history on a file… when you aren’t under version control, or you’re in between commits. Browse each IDE change, see diffs, recover from mistakes.
Delete a file by accident? Go to the folder’s local history, and recover that too!
The Problem
This is the "Oh my goodness you saved my sanity" tip.
Sometimes you are in a project that isn't yet under version control. Or, you've done a lot of work since your last commit. For example, a file is under version control, but you've been very busy.
You pause for a bit, have some inspiration, and hack away. You delete a paragraph, then ultimately finish the text. But now you want that paragraph back! Git can’t help, because you didn't commit before hacking away.
You could try “Undo-Undo-Undo-Copy-Redo-Redo-Paste.” But that’s barbaric. There has to be a better way.
The Solution
This is going be one of those things that people love about our IDEs.
Local History to the rescue. Our IDEs have a built-in facility that tracks revisions for all IDE-initiated changes -- whether you are using a VCS or not.
Right-click on a file and choose “Local History” then “Show History”. On the left, you get a list of changes detected by the IDE. When you select one, you get a diff – comparing to the current file contents – on the right.
As you go through the changes, you see each diff. Like our normal diff viewer, you can apply changes to recover portions.
Perhaps you want the entirety of the file at that point in the history. Right-click on that revision and choose revert.
If a certain revision is known to be in a certain state, right-click and apply a friendly label.
That covers changes to a file. But what if you deleted the file, and it wasn't under VCS? Go to that file’s folder and choose Local History. The deletion event is there, and you can restore the file.
As a wrapup, let’s see the same sequence -- without the mouse! First, invoke Search Everywhere and search for the Show Local History action.
In the Local History window, tab into the Old Changes panel. Cursor down to the edit. Tab to go into the changes.