The following shortcuts are based on M$ Windows.
Navigation
<C-Up>
/<C-Down>
: move up ↑ / down ↓ without changing cursor position.<C-Alt-↑>
/<C-Alt-↓>
: open file explorer for the project root.
Searching / Replacing
As usual, it supports regular expressions as expected.
<C-F>
: search<C-R>
: replace<F3>
: jump to next matching word<S-F3>
: jump to previous matching word
Editing / Selection
Syntax-independent shortcuts:
<C-d>
: duplicate selected content, or the current line in case of empty selection.<Tab>
/<S-Tab>
: indent / unindent selected line(s), like<<
/>>
in Vim.<Alt-S-↑>
/<Alt-S-↓>
: move selected line(s) up ↑ / down ↓ to next attribute / method.<Alt-j>
: select next instance that matches the currect selection.<Alt-S-j>
: unselect current instance that matches the currect selection. i.e. reverse of<Alt-j>
.<C-C-↑>
/<C-C-↓>
: vertically aligned carets on multiple lines.- Press and release
<C>
once. - Press without releasing
<C>
. - Add caret(s) to neighbouring lines with
↑
and/or↓
.
- Press and release
Quick code navigation and generation
Syntax-dependent shortcuts:
<Alt-↑>
/<Alt-↓>
: move cursor up ↑ / down ↓ to next attribute / method.<C-S-↑>
/<C-S-↓>
: move selected attribute(s) and/or method(s) up ↑ / down ↓.<Alt-Ins>
: generate constructor, getter(s), setter(s), etc.<Alt-Enter>
: open popup window to resolve syntax error(s).<C-Enter>
: jump to definition.<S-F6>
: refactor current variable/method/etc.<S-F10>
: run program in the current configuration.<Alt-S-F10>
: run current class’smain
method, and update the current configuration.
Remarks: I don’t add the shortcuts for debugging mode since it requires the use of mouse for breakpoint insertion.
Emmets
Shortcuts for code generation.
main
:main
methodfori
: classicalfor
loop over an integer index.collection.iter
:for
loop overcollection
Other IDE Configurations
gitignore
In .gitignore
, I’m taught to use these three lines.
.idea/
out/
*.iml
Class import
In settings (<C-Alt-S>
), type “Java” to edit some settings in Editor → Code
Style → Java:
- Class count to use import with
*
: 1000 - Names count to use static import with
*
: 1000
In Packages to Use Import with *
, I’ve removed all packages.
shift Terminal focus
To disable escape from the Terminal by <Esc>
(imagine you’re using Vim for a
Git commit), you may carry out the following steps.
- In settings, choose Tools → Terminal on the left side bar.
- In the main area, under the Application Settings section, click Configure terminal keybindings.
- In the text field next to the magnifying glass, type “terminal”.
- On the right-hand side of the row Switch Focus To Editor, right-click
<Esc>
. - In the popup menu, select the last item Remove Escape.
Apache Log4J
Apache Log4J is a great debugging tool. Here’re the steps for its installation.
- Go to MVN Repository.
- Search “log4j”.
- Download Apache Log4j Core and Apache Log4j API.
- Choose JAR file.
- I’ve only tried version 2.17.2.
- In the top menu, click File → Project Structure (
<C-Alt-S-S>
). - In the left side menu in the window, click ‘+’ then Java.
- Select the two JAR files downloaded from MVN Repository.
- Create
src/log4j2.xml
. Other filename doesn’t work. It can be anywhere in the app’s classpath.
I was taught to copy and paste a Rolling File Appender similar to the one on How To Do In Java.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="basePath">C:/temp/logs</Property>
</Properties>
<Appenders>
<RollingFile name="fileLogger"
fileName="${basePath}/app.log"
filePattern="${basePath}/app-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<!-- Max 10 files will be created everyday -->
<DefaultRolloverStrategy max="10">
<Delete basePath="${basePathr}" maxDepth="10">
<!-- Delete all files older than 30 days -->
<IfLastModified age="30d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
<appender-ref ref="fileLogger" />
</Root>
</Loggers>
</Configuration>
Once it’s setup, it can be used by logger.error(String errorMessage)
.
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
public class Main {
private static final Logger logger = LogManager.getLogger(Main.class);
public static void main(final String... args)
{
logger.debug("Debug Message Logged !!!");
logger.info("Info Message Logged !!!");
logger.error("Error Message Logged !!!", new NullPointerException("NullError"));
}
}
- The
logger
isn’t a constant, since it’s state can be changed. - The
final
keyword forbids another instance ofLogger
from being affected withlogger
. - The
private
keyword forbids other classes from accessinglogger
. Logger
follows the singleton pattern.
The debug(String message)
is for development.