打造自己的 Python 編碼環境
前言
趁著放假,重新配置了一下自己的Mac的程式設計環境,畢竟新年新氣象嘛,主要是iTerm2、Zsh、Vim優化、Consolas字型、NoisyTyper,這些的相關配置。工欲利其事必先利其器,好的編碼環境可以提升我們的打碼的幸福感。好的編碼環境包括美觀(視覺),聲音(聽覺),流暢度(觸覺),工作環境(嗅覺,味覺)等多個方面。後面有幾張配置後的圖片感受一下,主要看字型和配色(有些人可能覺得比較醜,個人喜歡黑紫,配色和字型有很多選擇,各有所好,求別噴 XD)。
準備階段:器
iTerm2:是os系統中的一個替代終端。主要優點:螢幕分割(可以水平分割和垂直分割),選中即複製,快速查詢(command+f),配色字型自定義化,自定義快捷鍵等。
Consolas:微軟下最好看的字型之一,個人認為很適合程式設計師,除開字型好看外,主要原因是它的數字“0”字加入了一斜撇,以方便與字母“o”分辨。這也是我和它結緣的主要緣由,當年在某次acm比賽中,就是因為肉眼無法分辨是0還是o,遺憾至今。
Zsh:zsh和bash一樣是Shell的一種。相比bash:更高效、更好的自動補全、更好的檔名展開(萬用字元展開)、更好的陣列處理、可定製性高。mac下自帶了zsh,無需再安裝。
Vim:是一個類似於Vi的著名的功能強大、高度可定製的文字編輯器,在Vi的基礎上改進和增加了很多特性。和Emacs並列成為類Unix系統使用者最喜歡的編輯器。估計大家都很熟悉,不做累述。
NoisyTyper:一款提升你打字逼格的軟體,可以讓你打字的時候發出打字機的聲音,咔咔咔這樣。
上面幾個安裝都比較簡單,教程也比較多。這裡主要講一下mac下consolas字型的安裝。
$ brew install cabextract $ cd ~/Downloads $ mkdir consolas $ cd consolas $ curl -O http://download.microsoft.com/download/f/5/a/f5a3df76-d856-4a61-a6bd-722f52a5be26/PowerPointViewer.exe $ cabextract PowerPointViewer.exe $ cabextract ppviewer.cab $ open CONSOLA*.TTF
最後按下彈出視窗的安裝鍵就ok了。
打磨階段:利器
上面的工具準備好了後,就可以開始配置這些工具了。
Zsh配置
oh-my-zsh:一套強大的開源zsh配置檔案。安裝如下:
// 使用curl安裝 $ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" // 或者使用wget安裝 $ sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)" // 安裝後重新載入配置 $ source .zshrc
oh-my-zsh主題: oh-my-zsh有多款配套主題,點選前面的主題連結可以看到所有主題。這裡主要介紹下agnoster主題。修改zsh配置檔案(~/.zshrc)中的主題屬性為agnoster即可。官方圖如下:
安裝這個主題需要額外安裝powerline字型,不然會顯示亂碼。如果你想隱藏自己的使用者名稱資訊,需要在zsh配置檔案中設定預設使用者。具體安裝配置如下:
// 修改zsh配置檔案 $ vim ~/.zshrc ZSH_THEME="agnoster" //在.zshrc中修改ZSH_THEME DEFAULT_USER=username // 在.zshrc中新增或者修改預設使用者為自己,開啟終端後就不會顯示自己的使用者名稱資訊 $ source ~/.zshrc // 重新載入配置檔案 // poweline font 安裝 $ git clone https://github.com/powerline/fonts.git $ cd powerline $ ./install.sh
iTerm2配置
字型:接著要在iterm2的Perferences中Text中選擇常規字型 為consolas字型或者其他你喜歡的字型,非ASCII碼字型為powerline字型(一定要是名字中帶powerline的字型,不然還是亂碼)。如下:
iTerm2配色:從這裡可以獲取很多別人的配色主題,把整個專案git clone下來,然後在iterm2的Perferences中的Colors最下面的Load Presets中import git下來的terminal檔案,自己可以根據自己喜歡調整自己喜歡的顏色,我選的是Fish Tank,我自己微調了一些配色。如下:
Vim配置
之前一直用spf13,但是開啟大檔案太卡了,所以自己重新配置了一份。實測開啟8000+行程式碼的檔案,因為語法檢測,所以大概延時1s左右,之後操作很順暢。在配置過程中會遇到很多蛋疼的地方。需要比較系統的學習一下vim指令碼的語法和相關配置方法,vim網上很多配置好文,如《像 IDE 一樣使用 vim》,還有《vim 指令碼學習文件》,我就不累述了,配置檔案的註釋寫的比較詳細。我的配置主要針對python開發,另外需要額外安裝python包flake8。具體如下:
vimrc檔案,主要是vim基礎配置。(highlight.js不支援vim script,有點蛋疼)
" .vimrc " 相關vim指令碼文件 http://vimdoc.sourceforge.net " 相關好文 https://github.com/yangyangwithgnu/use_vim_as_ide " 基本配置 set nocompatible "不要vim模仿vi模式,建議設定,否則會有很多不相容的問題 set mouse=a " 啟動滑鼠所有模式,但是右鍵功能不可用, 可以保證滑鼠滾屏在當前螢幕內 set mousehide " 輸入檔案時隱藏滑鼠 set backspace=indent,eol,start " 退格鍵分別可刪除縮排,上一行結束,insert之前的字 set showmatch " 設定匹配模式 set nobackup " 不備份 set nowritebackup " 不寫入備份檔案 set noswapfile " 關閉交換檔案 set history=500 " history儲存長 set ruler " 顯示標尺 set showcmd " 顯示輸入命令 set incsearch " 搜尋時自動匹配 set hlsearch " 高亮搜尋項 set ignorecase " 無視大小寫 set smartcase " 如果有大寫就區別大小寫匹配 set laststatus=2 " 總是顯示狀態列 " set autowrite " 切換檔案自動儲存 set shortmess=atI" " 關閉歡迎頁面 set viewoptions=folds,options,cursor,unix,slash " Better Unix / Windows compatibility set virtualedit=onemore " 游標可以移到當行最後一個字元之後 set hidden " 切換檔案不儲存,隱藏 "set confirm " 退出前驗證 " set spell " 拼寫檢查 set linespace=0 " 行之間沒有多餘的空格 set wildmenu " 自動補全時的檔案選單 set wildmode=list:longest,full " 自動補全時,匹配最長子串,列出檔案 set whichwrap=b,s,h,l,<,>,[,] " 行尾可右移到下行,行首左移到上行,b:退格,s:空格,hl:左右,<>:n/v模式下的左右,[]:i/r模式下的左右 set scrolljump=5 " 游標離開螢幕範圍 set scrolloff=3 " 游標移動至少保留行數 " 格式 set nowrap " 取消自動折行 set autoindent " 自動縮排 set shiftwidth=4 " 縮排位寬=4個空格位 set expandtab " tab由空格表示 set tabstop=4 " tab=4空格 set softtabstop=4 " 回退可以刪除縮排 set nojoinspaces " 用J合併兩行用一個空格隔開 set splitright " 用vsplit新建視窗,讓新的放右邊 set splitbelow " 用split新建視窗,讓新的放下面 set pastetoggle=<F12> " 指定F12進入黏貼模式,可以正常複製縮排 set iskeyword-=. " 讓'.' 作為單詞分割符 set iskeyword-=# " 讓'#' 作為單詞分割符 set iskeyword-=- " 讓'-' 作為單詞分割符 set listchars=tab:›/ ,trail:•,extends:#,nbsp:. " 空格等無效字元顯示 set textwidth=80 " 內容寬度 set fileencodings=utf-8,gb18030,gbk,big5 " 檔案編碼 " 檔案儲存時處理首尾空格,^M字元 let g:keep_trailing_whitespace = 1 function! StripTrailingWhitespace() let _s=@/ let l = line(".") let c = col(".") %s//s/+$//e let @/=_s call cursor(l, c) endfunction autocmd FileType c,cpp,java,go,php,javascript,python,twig,xml,yml autocmd BufWritePre <buffer> if exists('g:keep_trailing_whitespace') | call StripTrailingWhitespace() | endif " 開啟新的buffer時,自動轉到對應檔案目錄 let g:autochdir = 1 if exists('g:autochdir') autocmd BufEnter * if bufname("") !~ "^/[A-Za-z0-9/]*://" | lcd %:p:h | endif endif " 恢復游標最後編輯位置 au BufWinLeave *.py,*.c,*.cpp,*.css,*.html,*.js,*php mkview au BufWinEnter *.py,*.c,*.cpp,*.css,*.html,*.js,*php silent loadview " 設定u的返回步數限制 if has('persistent_undo') set undofile " 開啟u回滾檔案記錄 set undolevels=1000 " 最大數量的改變回滾 set undoreload=10000 " 最大數量過載可回滾行數 endif " 特殊檔案開啟 autocmd BufNewFile,BufRead *.py,*.pyw set filetype=python autocmd BufNewFile,BufRead *.html.twig set filetype=html.twig autocmd FileType haskell setlocal expandtab shiftwidth=2 softtabstop=2 autocmd BufNewFile,BufRead *.coffee set filetype=coffee autocmd FileType haskell setlocal commentstring=--/ %s autocmd FileType haskell setlocal nospell " key 對映 let mapleader = ',' " 全域性leader設定 let maplocalleader = '_' " 本地leader設定 " 設定tag和window間快速跳轉 let g:easyWindows = 1 if exists('g:easyWindows') " 向上 map <C-J> <C-W>j<C-W>_ " 向下 map <C-K> <C-W>k<C-W>_ " 向右 map <C-L> <C-W>l<C-W>_ " 向左 map <C-H> <C-W>h<C-W>_ endif " 處理摺疊行的左右移動 noremap j gj noremap k gk " 沒sudo卻想儲存 cmap w!! w !sudo tee % >/dev/null " ,fc查詢衝突的地方 map <leader>fc //v^[</|=>]{7}( .*/|$)<CR> " ,ff 查詢游標後的單詞位置,列出選擇項 nmap <Leader>ff [I:let nr = input("Which one: ")<Bar>exe "normal " . nr ."[/t"<CR> " 螢幕左移和右移 map zl zL map zh zH " 對映vsp這些開啟新的buffer,預設目錄為當前目錄 cnoremap %% <C-R>=fnameescape(expand('%:h')).'/'<cr> map <leader>ew :e %% map <leader>es :sp %% map <leader>ev :vsp %% map <leader>et :tabe %% " 黏貼板 if has('clipboard') if has('unnamedplus') set clipboard=unnamedplus else set clipboard=unnamed endif endif "字型的設定 set guifont=Consolas,Bitstream_Vera_Sans_Mono:h9:cANSI " 設定gui字型 set gfw=幼圓:h10:cGB2312 " 語法高亮 syntax on " 匯入vim外掛管理檔案 if filereadable(expand("~/.vimrc.bundles")) source ~/.vimrc.bundles endif " 縮排和md檔案 filetype plugin indent on " 自動根據型別啟動對應外掛,縮排開啟 " 設定主題,UI " solarized 主題 set background=dark if filereadable(expand("~/.vim/bundle/vim-colors-solarized/colors/solarized.vim")) let g:solarized_termcolors=256 let g:solarized_termtrans=1 let g:solarized_contrast="normal" let g:solarized_visibility="normal" color solarized " Load a colorscheme endif set cursorline " 選中行高亮 autocmd BufEnter *.py set cc=81 " 開啟py檔案81行高亮 " 之前的高亮線太難看,重新制定顏色,這裡的black和iterm2顏色配置中的black一樣 hi CursorLine cterm=NONE ctermbg=black guibg=black hi CursorColumn cterm=NONE ctermbg=black guibg=black " highlight ColorColumn ctermbg=black guibg=black set number " 設定行號 hi LineNr ctermbg=black " Indent Guides 縮排列對齊線 let g:indent_guides_start_level = 2 let g:indent_guides_guide_size = 1 let g:indent_guides_enable_on_vim_startup = 1 " airline 就是狀態列的箭頭 let g:airline_theme="dark" let g:airline_powerline_fonts = 1 let g:airline#extensions#tabline#enabled = 1 let g:airline#extensions#tabline#buffer_nr_show = 1 " NerdTree配置 control+e開啟當前檔案目錄樹 map <C-e> :NERDTreeToggle<CR> map <leader>ee :NERDTreeToggle<CR> nmap <leader>nt :NERDTreeFind<CR> let NERDTreeShowBookmarks=1 let NERDTreeIgnore=['/.pyc', '/~$', '/.swo$', '/.swp$', '/.git', '/.hg', '/.svn', '/.bzr'] let NERDTreeChDirMode=0 let NERDTreeQuitOnOpen=1 let NERDTreeMouseMode=2 let NERDTreeShowHidden=1 let NERDTreeKeepTreeInNewTab=1 let g:nerdtree_tabs_open_on_gui_startup=0 " syntastic 配置 " 設定每次w儲存後語法檢查 function! ToggleErrors() Errors endfunction let g:syntastic_check_on_open=1 let g:syntastic_always_populate_loc_list = 1 let g:syntastic_auto_loc_list = 1 let g:syntastic_check_on_wq = 0 let syntastic_loc_list_height = 5 let g:syntastic_html_tidy_ignore_errors=[" proprietary attribute /"ng-"] autocmd WinEnter * if &buftype ==#'quickfix' && winnr('$') == 1 | quit |endif autocmd WinLeave * lclose " 自動補全tab觸發 function! InsertTabWrapper() let col = col('.') - 1 if !col || getline('.')[col - 1] !~ '/k' return "/<tab>" else return "/<c-p>" endif endfunction inoremap <Tab> <c-r>=InsertTabWrapper()<cr> inoremap <S-Tab> <c-n> " Ctrlp 和 Ctrlp-funky(,fu) if isdirectory(expand("~/.vim/bundle/ctrlp.vim/")) let g:ctrlp_working_path_mode = 'ra' nnoremap <silent> <D-t> :CtrlP<CR> nnoremap <silent> <D-r> :CtrlPMRU<CR> let g:ctrlp_custom_ignore = { / 'dir': '/.git$/|/.hg$/|/.svn$', / 'file': '/.exe$/|/.so$/|/.dll$/|/.pyc$' } if executable('ag') let s:ctrlp_fallback = 'ag %s --nocolor -l -g ""' elseif executable('ack-grep') let s:ctrlp_fallback = 'ack-grep %s --nocolor -f' elseif executable('ack') let s:ctrlp_fallback = 'ack %s --nocolor -f' " On Windows use "dir" as fallback command. else let s:ctrlp_fallback = 'find %s -type f' endif if exists("g:ctrlp_user_command") unlet g:ctrlp_user_command endif let g:ctrlp_user_command = { / 'types': { / 1: ['.git', 'cd %s && git ls-files . --cached --exclude-standard --others'], / 2: ['.hg', 'hg --cwd %s locate -I .'], / }, / 'fallback': s:ctrlp_fallback / } if isdirectory(expand("~/.vim/bundle/ctrlp-funky/")) " CtrlP extensions let g:ctrlp_extensions = ['funky'] "funky nnoremap <Leader>fu :CtrlPFunky<Cr> endif endif " complete autocmd Filetype * if &omnifunc == "" | setlocal omnifunc=syntaxcomplete#Complete | endif autocmd FileType css setlocal omnifunc=csscomplete#CompleteCSS autocmd FileType html,markdown setlocal omnifunc=htmlcomplete#CompleteTags autocmd FileType javascript setlocal omnifunc=javascriptcomplete#CompleteJS autocmd FileType python setlocal omnifunc=pythoncomplete#Complete "進行版權宣告的設定 "新增或更新頭 map <F4> :call TitleDet()<cr>'s autocmd BufNewFile *.py :call TitleDet() function AddTitle() call append(0,"#! /usr/bin/env python") call append(1,"# -*- coding: utf-8 -*-") call append(2,"# vim:fenc=utf-8 ") call append(3,"# Copyright © XYM") call append(4,"# Last modified: ".strftime("%Y-%m-%d %H:%M:%S")) call append(5,"") endf "更新最近修改時間和檔名 function UpdateTitle() normal m' execute '/# *Last modified:/s@:.*$@/=strftime(":/t%Y-%m-%d %H:%M")@' normal '' normal mk execute '/# *Filename:/s@:.*$@/=":/t/t".expand("%:t")@' execute "noh" normal 'k endfunction "判斷前10行程式碼裡面,是否有Last modified這個單詞, "如果沒有的話,代表沒有新增過作者資訊,需要新新增; "如果有的話,那麼只需要更新即可 function TitleDet() let n=1 "預設為新增 while n < 10 let line = getline(n) if line =~ '^/#/s*/S*Last/smodified:/S*.*$' call UpdateTitle() return endif let n = n + 1 endwhile call AddTitle() endfunction
vimrc.bundles檔案,主要是vim相關外掛
" .vimrc.bundles if &compatible set nocompatible end filetype off set rtp+=~/.vim/bundle/vundle call vundle#rc() Bundle "gmarik/vundle" " 星級越多越推薦, 5星為必選,4星強烈推薦,3星比較推薦 " 檔案模糊匹配查詢 推薦:**** Bundle "kien/ctrlp.vim" " 函式模糊匹配查詢(,fu) 推薦:**** Bundle 'tacahiroy/ctrlp-funky' " 檔案索引樹 推薦:***** Bundle "scrooloose/nerdtree" " 自動註釋 推薦:***** Bundle 'scrooloose/nerdcommenter' " 語法檢查 推薦:**** Bundle "scrooloose/syntastic" " 括號自動匹配 推薦:**** Bundle 'jiangmiao/auto-pairs' " 游標多行編輯 <C-n> <C-x> <C-p> 推薦:**** Bundle 'terryma/vim-multiple-cursors' " 配色主題 推薦:**** Bundle "altercation/vim-colors-solarized" " 縮排引導線 推薦:**** Bundle "nathanaelkane/vim-indent-guides" " 狀態列效果 推薦:*** Bundle "bling/vim-airline" " python 推薦:**** Bundle 'yssource/python.vim' Bundle 'python_match.vim' Bundle 'pythoncomplete' " js 推薦:**** Bundle 'elzr/vim-json' Bundle 'groenewege/vim-less' Bundle 'pangloss/vim-javascript' Bundle 'briancollins/vim-jst' Bundle 'kchmck/vim-coffee-script' " html 推薦:**** Bundle 'amirh/HTML-AutoCloseTag' Bundle 'hail2u/vim-css3-syntax' Bundle 'gorodinskiy/vim-coloresque' Bundle 'tpope/vim-haml' Bundle 'mattn/emmet-vim' if filereadable(expand("~/.vimrc.bundles.local")) source ~/.vimrc.bundles.local endif filetype on
總結
一個好的程式設計師必須善用各種工具提升自己的開發效率,和提升自己的幸福指數。另外,個人覺得vim還是值得自己重新一配的,我的配置過程基本都是在讀spf13的配置,讀懂,然後選取,可是中間還是出了很多問題,比如因為我沒有用pymode外掛,所以在用括syntastic的local_list在退出或者分屏時的位置,都令我很蛋疼。而且在讀spf13配置時,會發現有很多很好的功能都沒有用上,比如paste模式,查詢衝突快捷鍵,螢幕移動,還有ctrlp-funky,vim-multiple-cursors這些很爽的外掛。大家有好的配置可以推薦給我。
相關文章
- Typora+MinIO+Python程式碼打造舒適協作環境Python
- Python程式碼編輯器和整合開發環境有哪些?Python開發環境
- grpc-java原始碼環境編譯RPCJava原始碼編譯
- Docker定製自己的環境映象Docker
- python環境Python
- qgroundcontrol開發環境搭建原始碼編譯開發環境原始碼編譯
- Linux環境PostGIS原始碼編譯安裝Linux原始碼編譯
- openEuler arm 環境原始碼編譯mysql 8.0.37原始碼編譯MySql
- Ubuntu環境編譯OpenJDK11原始碼Ubuntu編譯JDK原始碼
- 使用Anaconda建立一個自己的環境
- 搭建自己的React+Typescript環境(二)ReactTypeScript
- 搭建自己的React+Typescript環境(一)ReactTypeScript
- 使用sublime text3搭建Python編輯環境Python
- 彙編環境配置
- win10使用vs2019從原始碼編譯OpenCV4.5+cuda10.2+cudnn8.0的C++環境和Python環境Win10原始碼編譯OpenCVDNNC++Python
- Python的開發環境Python開發環境
- 可移植的python環境Python
- Windows 環境下 Python 環境安裝WindowsPython
- 打造優雅高效的 PHP 開發環境PHP開發環境
- Nestjs最佳實踐教程:1編碼環境搭建JS
- 學習筆記-kali中配置python編寫環境筆記Python
- 配置python環境Python
- python環境搭建Python
- 編譯環境 Golang開發環境 vscode+git編譯Golang開發環境VSCodeGit
- 打造一個好用的 Windows 程式設計環境 2.程式設計環境Windows程式設計
- 用Python編寫自己的微型RedisPythonRedis
- 編寫你自己的Python模組Python
- Android編譯環境搭建Android編譯
- openform環境搭建-編譯ORM編譯
- [環境搭建] 透過原始碼編譯安裝 Redis原始碼編譯Redis
- python的虛擬環境virtualenvPython
- Anaconda虛擬環境配置Python庫與Spyder編譯器Python編譯
- vagrant + centos/7 搭建自己的本地開發環境CentOS開發環境
- 關於python Miniconda的指令碼安裝及 環境控制Python指令碼
- python批次將檔案編碼格式轉換為 UTF8帶標籤的格式,解決linux環境下中文編碼亂碼的問題PythonLinux
- python下多環境開發(虛擬環境)Python
- Python虛擬環境Python
- Python多環境管理Python
- virtualenv管理Python環境Python