1. 程式人生 > >pycharm快捷鍵和常用設定

pycharm快捷鍵和常用設定

本部落格一直在同步更新中!

內容包含:pycharm學習技巧 Learning tips、PyCharm3.0預設快捷鍵(翻譯的)、pycharm常用設定、pycharm環境和路徑配置、Pycharm實用拓展功能:pycharm中清除已編譯.pyc中間檔案、python2轉python3最快方式
[pycharm版本控制和資料庫管理][PyCharm中的那些實用功能]

pycharm學習技巧 Learning tips

/pythoncharm/help/tip of the day:
A special variant of the Code Completion feature invoked by pressing Ctrl+Space twice allows you to complete the name of any class no matter if it was imported in the current file or not. If the class is not imported yet, the import statement is generated automatically.
You can quickly find all places where a particular class, method or variable is used in the whole project by positioning the caret at the symbol's name or at its usage in code and pressing Alt+Shift+F7 (Find Usages in the popup menu).
To navigate to the declaration of a class, method or variable used somewhere in the code, position the caret at the usage and press F12. You can also click the mouse on usages with the Ctrl key pressed to jump to declarations.
You can easily rename your local variables with automatic correction of all places where they are used.
To try it, place the caret at the variable you want to rename, and press Shift+F6 (Refactor | Rename). Type the new name in the popup window that appears, or select one of the suggested names, and press Enter.

...

切換
Use Alt+Up and Alt+Down keys to quickly move between methods in the editor.
Use Ctrl+Shift+F7 (Edit | Find | Highlight Usages in File) to quickly highlight usages of some variable in the current file.
選擇
You can easily make column selection by dragging your mouse pointer while keeping the Alt key pressed.
補全
Working in the interactive consoles, you don't need to memorise the command line syntax or available functions. Instead, you can use the familiar code completion Ctrl+Space. Moreover, from within the lookup list, you can press Ctrl+Q to view the item's documentation.
顯示
Use F3 and Shift+F3 keys to navigate through highlighted usages.
Press Escape to remove highlighting.
歷史
Ctrl+Shift+Backspace (Navigate | Last Edit Location) brings you back to the last place where you made changes in the code.
Pressing Ctrl+Shift+Backspace a few times moves you deeper into your changes history.
Ctrl+E (View | Recent Files) brings a popup list of the recently visited files. Choose the desired file and press Enter to open it.
Use Alt+Shift+C to quickly review your recent changes to the project.
剪下板
Use the Ctrl+Shift+V shortcut to choose and insert recent clipboard contents into the text.
If nothing is selected in the editor, and you press Ctrl+C, then the whole line at caret is copied to the clipboard.
run/debug
By pressing Alt+Shift+F10 you can access the Run/Debug dropdown on the main toolbar, without the need to use your mouse.

在PyCharm安裝目錄 /opt/pycharm-3.4.1/help目錄下可以找到ReferenceCard.pdf快捷鍵英文版說明 or 開啟pycharm > help > default keymap ref

PyCharm3.0預設快捷鍵(翻譯的)

PyCharm Default Keymap

1、編輯(Editing)

Ctrl + Space    基本的程式碼完成(類、方法、屬性)
Ctrl + Alt + Space  快速匯入任意類
Ctrl + Shift + Enter    語句完成
Ctrl + P    引數資訊(在方法中呼叫引數)

Ctrl + Q    快速檢視文件

F1   外部文件

Shift + F1    外部文件,進入web文件主頁

Ctrl + Shift + Z --> Redo 重做

Ctrl + 懸浮/單擊滑鼠左鍵    簡介/進入程式碼定義
Ctrl + F1    顯示錯誤描述或警告資訊
Alt + Insert    自動生成程式碼
Ctrl + O    重新方法
Ctrl + Alt + T    選中
Ctrl + /    行註釋/取消行註釋
Ctrl + Shift + /    塊註釋

Ctrl + W    選中增加的程式碼塊
Ctrl + Shift + W    回到之前狀態
Ctrl + Shift + ]/[     選定程式碼塊結束、開始
Alt + Enter    快速修正
Ctrl + Alt + L     程式碼格式化

Ctrl + Alt + O    優化匯入
Ctrl + Alt + I    自動縮排
Tab / Shift + Tab  縮排、不縮排當前行
Ctrl+X/Shift+Delete    剪切當前行或選定的程式碼塊到剪貼簿
Ctrl+C/Ctrl+Insert    複製當前行或選定的程式碼塊到剪貼簿
Ctrl+V/Shift+Insert    從剪貼簿貼上
Ctrl + Shift + V    從最近的緩衝區貼上
Ctrl + D  複製選定的區域或行
Ctrl + Y    刪除選定的行
Ctrl + Shift + J  新增智慧線
Ctrl + Enter   智慧線切割
Shift + Enter    另起一行
Ctrl + Shift + U  在選定的區域或程式碼塊間切換
Ctrl + Delete   刪除到字元結束
Ctrl + Backspace   刪除到字元開始
Ctrl + Numpad+/-   展開/摺疊程式碼塊(當前位置的:函式,註釋等)
Ctrl + shift + Numpad+/-   展開/摺疊所有程式碼塊
Ctrl + F4   關閉執行的選項卡
 2、查詢/替換(Search/Replace)
F3   下一個
Shift + F3   前一個
Ctrl + R   替換
Ctrl + Shift + F  或者連續2次敲擊shift   全域性查詢
{可以在整個專案中查詢某個字串什麼的,如查詢某個函式名字串看之前是怎麼使用這個函式的}
Ctrl + Shift + R   全域性替換
 3、執行(Running)

Alt + Shift + F10   執行模式配置
Alt + Shift + F9    除錯模式配置
Shift + F10    執行
Shift + F9   除錯
Ctrl + Shift + F10   執行編輯器配置
Ctrl + Alt + R   執行manage.py任務
 4、除錯(Debugging)
F8   跳過
F7   進入
Shift + F8   退出
Alt + F9    運行遊標
Alt + F8    驗證表示式
Ctrl + Alt + F8   快速驗證表示式
F9    恢復程式
Ctrl + F8   斷點開關
Ctrl + Shift + F8   檢視斷點
 5、導航(Navigation)
Ctrl + N    跳轉到類
Ctrl + Shift + N    跳轉到符號

Alt + Right/Left    跳轉到下一個、前一個編輯的選項卡(程式碼檔案)

Alt + Up/Down跳轉到上一個、下一個方法

F12    回到先前的工具視窗
Esc    從工具視窗回到編輯視窗
Shift + Esc   隱藏執行的、最近執行的視窗
Ctrl + Shift + F4   關閉主動執行的選項卡
Ctrl + G    檢視當前行號、字元號
Ctrl + E   當前檔案彈出,開啟最近使用的檔案列表
Ctrl+Alt+Left/Right   後退、前進

Ctrl+Shift+Backspace    導航到最近編輯區域 {差不多就是返回上次編輯的位置}

Alt + F1   查詢當前檔案或標識
Ctrl+B / Ctrl+Click    跳轉到宣告
Ctrl + Alt + B    跳轉到實現
Ctrl + Shift + I檢視快速定義
Ctrl + Shift + B跳轉到型別宣告

Ctrl + U跳轉到父方法、父類

Ctrl + ]/[跳轉到程式碼塊結束、開始

Ctrl + F12彈出檔案結構
Ctrl + H型別層次結構
Ctrl + Shift + H方法層次結構
Ctrl + Alt + H呼叫層次結構
F2 / Shift + F2下一條、前一條高亮的錯誤
F4 / Ctrl + Enter編輯資源、檢視資源
Alt + Home顯示導航條F11書籤開關
Ctrl + Shift + F11書籤助記開關
Ctrl + #[0-9]跳轉到標識的書籤
Shift + F11顯示書籤
 6、搜尋相關(Usage Search)

Alt + F7/Ctrl + F7檔案中查詢用法
Ctrl + Shift + F7檔案中用法高亮顯示
Ctrl + Alt + F7顯示用法
 7、重構(Refactoring)

F5複製F6剪下
Alt + Delete安全刪除
Shift + F6重新命名
Ctrl + F6更改簽名
Ctrl + Alt + N內聯
Ctrl + Alt + M提取方法
Ctrl + Alt + V提取屬性
Ctrl + Alt + F提取欄位
Ctrl + Alt + C提取常量
Ctrl + Alt + P提取引數
 8、控制VCS/Local History
Ctrl + K提交專案
Ctrl + T更新專案
Alt + Shift + C檢視最近的變化
Alt + BackQuote(’)VCS快速彈出
 9、模版(Live Templates)

Ctrl + Alt + J當前行使用模版
Ctrl +J插入模版
 10、基本(General)
Alt + #[0-9]開啟相應的工具視窗
Ctrl + Alt + Y同步
Ctrl + Shift + F12最大化編輯開關
Alt + Shift + F新增到最喜歡
Alt + Shift + I根據配置檢查當前檔案
Ctrl + BackQuote(’)快速切換當前計劃
Ctrl + Alt + S 開啟設定頁
Ctrl + Shift + A查詢編輯器裡所有的動作

Ctrl + Tab在視窗間進行切換 

pycharm常用設定

lz提示一下,pycharm中的設定是可以匯入和匯出的,file>export settings可以儲存當前pycharm中的設定為jar檔案,重灌時可以直接import settings>jar檔案,就不用重複配置了。

file -> Setting ->Editor

1. 設定Python自動引入包,要先在 >general > autoimport -> python :show popup

     快捷鍵:Alt + Enter: 自動新增包

2. “程式碼自動完成”時間延時設定

  > Code Completion   -> Auto code completion in (ms):0  -> Autopopup in (ms):500

3. Pycharm中預設是不能用Ctrl+滾輪改變字型大小的,可以在〉Mouse中設定

4. 顯示“行號”與“空白字元”

  > Appearance  -> 勾選“Show line numbers”、“Show whitespaces”、“Show method separators”

5. 設定編輯器“顏色與字型”主題

  > Colors & Fonts -> Scheme name -> 選擇"monokai"“Darcula”

  說明:先選擇“monokai”,再“Save As”為"monokai-pipi",因為預設的主題是“只讀的”,一些字型大小顏色什麼的都不能修改,拷貝一份後方可修改!

  修改字型大小

> Colors & Fonts -> Font -> Size -> 設定為“14”

6. 設定縮排符為製表符“Tab”

  File -> Default Settings -> Code Style

  -> General -> 勾選“Use tab character”

  -> Python -> 勾選“Use tab character”

  -> 其他的語言程式碼同理設定

7. 去掉預設摺疊

  > Code Folding -> Collapse by default -> 全部去掉勾選 

8. pycharm預設是自動儲存的,習慣自己按ctrl + s  的可以進行如下設定:
    > General -> Synchronization -> Save files on frame deactivation  和 Save files automatically if application is idle for .. sec 的勾去掉

    > Editor Tabs -> Mark modified tabs with asterisk 打上勾

9.>file and code template>python scripts

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = '$Package_name'
__author__ = '$USER'
__mtime__ = '$DATE'
# code is far away from bugs with the god animal protecting
    I love animals. They taste delicious.
              ┏┓      ┏┓
            ┏┛┻━━━┛┻┓
            ┃      ☃      ┃
            ┃  ┳┛  ┗┳  ┃
            ┃      ┻      ┃
            ┗━┓      ┏━┛
                ┃      ┗━━━┓
                ┃  神獸保佑    ┣┓
                ┃ 永無BUG!   ┏┛
                ┗┓┓┏━┳┓┏┛
                  ┃┫┫  ┃┫┫
                  ┗┻┛  ┗┻┛
"""

10 python檔案預設編碼

File Encodings> IDE Encoding: UTF-8;Project Encoding: UTF-8;

11. 程式碼自動整理設定


這裡line breaks去掉√,否則bar, 和baz會分開在不同行,不好看。

File -> Settings -> appearance

1. 修改IDE快捷鍵方案

  > Keymap

1) execute selection in console : add keymap > ctrl + enter

  系統自帶了好幾種快捷鍵方案,下拉框中有如“defaul”,“Visual Studio”,在查詢Bug時非常有用,“NetBeans 6.5”,“Default for GNOME”等等可選項,

  因為“Eclipse”方案比較大眾,個人用的也比較多,最終選擇了“Eclipse”。 

  還是有幾個常用的快捷鍵跟Eclipse不一樣,為了能修改,還得先對Eclipse方案拷貝一份: 

  (1).程式碼提示功能,預設是【Ctrl+空格】,現改為跟Eclipse一樣,即【Alt+/】

  Main menu -> code -> Completion -> Basic -> 設定為“Alt+/”

  Main menu -> code -> Completion -> SmartType -> 設定為“Alt+Shift+/”

  不過“Alt+/”預設又被 

  Main menu -> code -> Completion -> Basic -> Cyclic Expand Word 佔用,先把它刪除再說吧(單擊右鍵刪除)!

  (2).關閉當前文件,預設是【Ctrl+F4】,現改為跟Eclipse一樣,即【Ctrl+W】

  Main menu -> Window -> Active Tool Window -> Close Active Tab -> 設定為 “Ctrl+F4”;

  Main menu -> Window -> Editor -> Close -> 設定為 “Ctrl+W”;

2.設定IDE面板主題

 > Theme -> 選擇“Alloy.IDEA Theme”

  或者在setting中搜索theme可以改變主題,所有配色統一改變

File > settings > build.excution

每次開啟python控制檯時自動執行程式碼

> console > pyconsole

import sys
# print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS])
import  os
print('current workdirectory : ', os.getcwd() )
import  numpy as  np
import  scipy as sp
import  matplotlib as mpl

如果安裝了ipython,則在pyconsole中使用更強大的ipython

> console

選中use ipython if available

這樣每次開啟pyconsole就會開啟ipython

Note: 在virtualenv中安裝ipython: (ubuntu_env) pika:/media/pika/files/mine/python_workspace/ubuntu_env$pip install ipython

File > settings > Languages & Frameworks

如果在專案設定中開啟了django支援,開啟python console時會自動變成開啟django console,當然如果不想這樣就關閉專案對django的支援:


如果開啟支援就會在 settings > build.excution > console下多顯示一個django console:


Django console設定如下

import sys
print('Python %s on %s' % (sys.version, sys.platform))
import django
print('Django %s' % django.get_version())
sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS])
if 'setup' in dir(django): django.setup()
import django_manage_shell; django_manage_shell.run(PROJECT_ROOT)

File > settings > Project : initial project

project dependencies > LDA > project depends on these projects > 選擇sim_cluster就可以在LDA中呼叫sim_cluster中的包

pycharm環境和路徑配置

python直譯器路徑

python專案直譯器路徑

用於配置python專案執行的python路徑

比如,有的專案是執行的是系統python2.7下的環境;有的是3.4;有的專案使用的是virtualenv的python環境[python虛擬環境配置 - pycharm中的專案配置]

在pycharm > file > settings > project:pythonworkspace > project interpreter > 選擇對應專案 > project interpreter中指定python直譯器

pycharm中執行configuration有一個選項add content roots to pythonpath

選中後sys.path中會多一整個專案project的路徑/media/pika/files/mine/python_workspace,裡面的目錄就被當成包使用,這樣就可以通過from SocialNetworks.SocialNetworks引入不是python包的目錄中的檔案了。

不過最好使用sys.path.append(os.path.join(os.path.split(os.path.realpath(__file__))[0],"../.."))來新增,這樣在pycharm外也可以執行不出錯 。

pycharm中進行python包管理

pycharm中的專案中可以包含package、目錄(目錄名可以有空格)、等等

目錄的某個包中的某個py檔案要呼叫另一個py檔案中的函式,首先要將目錄設定為source root,這樣才能從包中至上至上正確引入函式,否則怎麼引入都出錯:

SystemError: Parent module '' not loaded, cannot perform relative import

Note:目錄 > 右鍵 > make directory as > source root

python指令碼解釋路徑

ctrl + shift + f10 / f10 執行python指令碼時

當前工作目錄cwd為run/debug configurations 中的working directory

可在edit configurations > project or defaults中配置

console執行路徑和當前工作目錄

python console中執行時

cwd為File > settings > build.excution > console > pyconsole中的working directory

並可在其中配置

pycharm配置os.environ環境

pycharm中os.environ不能讀取到terminal中的系統環境變數

pycharm中os.environ不能讀取.bashrc引數

使用pycharm,無論在python console還是在module中使用os.environ返回的dict中都沒有~/.bashrc中的設定的變數,但是有/etc/profile中的變數配置。然而在terminal中使用python,os.environ卻可以獲取~/.bashrc的內容。

解決方法1:

在~/.bashrc中設定的系統環境只能在terminal shell下執行spark程式才有效,因為.bashrc is only read for interactive shells.

如果要在當前使用者整個系統中都有效(包括pycharm等等IDE),就應該將系統環境變數設定在~/.profile檔案中。如果是設定所有使用者整個系統,修改/etc/profile或者/etc/environment吧。

解決方法2:在程式碼中設定,這樣不管環境有沒有問題了

# spark environment settings
import sys, os
os.environ['SPARK_HOME'] = conf.get(SECTION, 'SPARK_HOME')
sys.path.append(os.path.join(conf.get(SECTION, 'SPARK_HOME'), 'python'))
os.environ["PYSPARK_PYTHON"] = conf.get(SECTION, 'PYSPARK_PYTHON')
os.environ['SPARK_LOCAL_IP'] = conf.get(SECTION, 'SPARK_LOCAL_IP')
os.environ['JAVA_HOME'] = conf.get(SECTION, 'JAVA_HOME')
os.environ['PYTHONPATH'] = '$SPARK_HOME/python/lib/py4j-0.10.3-src.zip:$PYTHONPATH'

pycharm配置第三方庫程式碼自動提示

Pycharm實用拓展功能

pycharm中清除已編譯.pyc中間檔案

選中你的workspace > 右鍵 > clean python compiled files

還可以自己寫一個清除程式碼

pycharm設定外部工具

[]針對當前pycharm中開啟的py檔案對應的目錄刪除其中所有的pyc檔案。如果是直接執行(而不是在下面的tools中執行),則刪除E:\mine\python_workspace\WebSite目錄下的pyc檔案。

將上面的刪除程式碼改成外部工具

PyCharm > settings > tools > external tools > +新增

Name: DelPyc

program: $PyInterpreterDirectory$/python Python安裝路徑

Parameters: $ProjectFileDir$/Oth/Utility/DelPyc.py $FileDir$

Work directory: $FileDir$

Note:Parameters後面的 $FileDir$引數是說,DelPyc是針對當前pycharm中開啟的py檔案對應的目錄刪除其中所有的pyc檔案。

之後可以通過下面的方式直接執行


Note:再新增一個Tools名為DelPycIn

program: Python安裝路徑,e.g.     D:\python3.4.2\python.exe

Parameters: E:\mine\python_workspace\Utility\DelPyc.py

Work directory 使用變數 $FileDir$

引數中沒有$FileDir$,這樣就可以直接刪除常用目錄r'E:\mine\python_workspace\WebSite'了,兩個一起用更方便

程式碼質量

當你在打字的時候,PyCharm會檢查你的程式碼是否符合PEP8。它會讓你知道,你是否有太多的空格或空行等等。如果你願意,你可以配置PyCharm執行pylint作為外部工具。

python2轉python3最快方式

/usr/bin/2to3 -wn $FileDir$

這樣在pycharm中開啟某個檔案,右鍵external tools > py2topy3就可以瞬間將當前檔案所在目錄下的所有py2轉換成py3,是不是很機智!

其它

from:http://blog.csdn.net/pipisorry/article/details/39909057