一个计算机技术爱好者与学习者

0%

IDEA常用快捷键和配置

1. 前言

工欲善其事,必先利其器。IDEA作为当今最流行的IDE之一,有很多快捷键和配置可以帮助我们更好地开发。本文记录一下自己常用的一些快捷键和配置,备忘。

2. 快捷键

2.1. 查看快捷键

1、菜单栏,IntelliJ IDEA,Preferences…。
2、Keymap,Main menu,Refactor,Extract,Introduce Variable…。

2.2. 自动补全函数返回值

windows: ctrl+alt+V,回车
macos: option+command+V,回车

2.3. 函数跳转

command+单击,command+],command+[

2.4. 代码格式化

macos: option+command+L

3. 配置

3.1. idea配置maven

1、打开idea,File,Settings,搜索“maven”。
2、Maven home directory选择D:\Server\apache-maven-3.3.9
3、User settings file选择D:\Server\apache-maven-3.3.9\conf\settings.xml

3.2. idea配置tomcat

1、打开idea,View,勾选Toolbar,显示工具栏。

2、单击工具栏上的下三角(Select Run/Debug Configuration),Edit Configurations。

3、单击打开窗口的“+”号,Add New Configuration,Tomcat Server,Local。

4、Name修改为Tomcat8.5,JRE选择C:\Program Files\Java\jdk1.8.0_111\jre

5、勾选Show this page,单击Fix,选择project_name:war exploded,OK。

6、单击工具栏的绿色三角,启动tomcat。

3.3. 自动导包

打开idea,File,Settings,Editor,General,Auto Import,界面上能勾选的全部勾选。

3.4. 自动换行

1、编码时自动换行
Preferences > Editor > Code Style,勾选wrap on typing
在编码时,如果超出最大行宽,则自动换行。

2、代码格式化时自动换行
Preferences > Editor > Code Style > Python,勾选 Ensure right margin is not exceeded
在使用快捷键手动格式化代码时,自动换行。

3.5. 批量替换换行符

参考Configuring Line Separators

为新文件配置换行符:

  1. Press ⌘, to open IDE settings and select Editor | Code Style.
  2. To configure line separators for new projects, go to File | New Projects Setup | Settings/Preferences for New Projects | Editor | Code Style.
  3. The line separator style applied to the current file is indicated in the status bar.

存量文件替换换行符:

  1. Select a file or directory in the Project tool window ⌘1.
    Note that if a directory is selected, the line ending style applies to all nested files recursively.

  2. From the main menu, choose File | File Properties | Line Separators, and then select a line ending style from the list.

3.6. python代码检查

pylint 是一个能够检查Python编码质量、编码规范的工具。它分析 Python 代码中的错误,查找不符合代码风格标准(Pylint 默认使用的代码风格是 PEP 8)和有潜在问题的代码。

参考文档:

pylint安装配置方法如下:

1、安装pylint
pip install pylint

2、常用命令

1
2
3
4
5
6
7
8
9
10
pylint --version
pylint --help
# 查看pylint安装路径
which pylint
# 生成配置文件
pylint --persistent=n --generate-rcfile > .pylintrc
# 默认使用当前目录下的.pylintrc配置文件
pylint test.py
# 指定配置文件
pylint --rcfile=.pylintrc test.py

C(convention):规范,违反了编码风格标准
W(warning):警告,某些python特定问题
E(error):错误,可能是代码中的错误
R(refactor):重构,代码比较糟糕
F(fatal error):致命错误

3、idea配置pylint
Preferences… > Tools > External Tools,点击 + 号添加

  • Name:pylint
  • Program:/Users/haojin01/Library/Python/2.7/bin/pylint
  • Arguments:--rcfile=$ProjectFileDir$/.pylintrc $FilePath$
  • Working directory:$FileDir$

4、使用
Tools > External Tools > pylint

3.7. python代码自动规范化

black 是一个官方的 Python 代码格式化工具,git地址 psf/black

black安装配置方法如下:

1、安装black
pip3 install black

2、常用命令

1
2
3
4
5
black --help
black --diff test.py
black test.py
black --line-length 120 test.py
black --config pyproject.toml test.py

3、官方 pyproject.toml 示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Example configuration for Black.

# NOTE: you have to use single-quoted strings in TOML for regular expressions.
# It's the equivalent of r-strings in Python. Multiline strings are treated as
# verbose regular expressions by Black. Use [ ] to denote a significant space
# character.

[tool.black]
line-length = 88
target-version = ['py36', 'py37', 'py38']
include = '\.pyi?$'
extend-exclude = '''
/(
# The following are specific to Black, you probably don't want those.
| blib2to3
| tests/data
| profiling
)/
'''


# Build system information below.
# NOTE: You don't need this in your own Black configuration.

[build-system]
requires = ["setuptools>=41.0", "setuptools-scm", "wheel"]
build-backend = "setuptools.build_meta"

[tool.pytest.ini_options]
# Option below requires `tests/optional.py`
optional-tests = [
"no_python2: run when `python2` extra NOT installed",
"no_blackd: run when `d` extra NOT installed",
"no_jupyter: run when `jupyter` extra NOT installed",
]

4、自动换行无效问题

1
2
black --line-length 120 test.py
black --config pyproject.toml test.py

以上两个命令,都无效,不能实现自动换行,很奇怪,应该是个bug,先不管它。

5、idea配置black
Preferences… > Tools > External Tools,点击 + 号添加

  • Name:black
  • Program:/usr/local/bin/black
  • Arguments:--config $ProjectFileDir$/pyproject.toml $FilePath$
  • Working directory:$FileDir$

6、使用
Tools > External Tools > black

3.8. 设置Python注释模板

1、打开注释模板配置
Preferences > Editor > Code Style > File and Code Templates > Python Script

2、填入注释内容

1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
################################################################################
#
# Copyright (c) 2021 Baidu.com, Inc. All Rights Reserved
#
################################################################################
"""
Authors: voidking
Date: ${DATE}
"""

3、新建Python文件,文件头部就会自动出现注释了。

3.9. 新建文件自动git add

1、打开注释模板配置
Preferences > Version Control > Confirmation ,选择Add silently。

2、新建文件,文件会被自动git add跟踪。