天天看點

打造自己的 Python 編碼環境

美觀(視覺),聲音(聽覺),流暢度(觸覺),工作環境(嗅覺,味覺)等多個方面。後面有幾張配置後的圖檔感受一下,主要看字型和配色(有些人可能覺得比 較醜,個人喜歡黑紫,配色和字型有很多選擇,各有所好,求别噴 xd)。

打造自己的 Python 編碼環境
打造自己的 Python 編碼環境

準備階段:器

consolas:微軟下最好看的字型之一,個人認為很适合程式員,除開字型好看外,主要原因是它的數字“0”字加入了一斜撇,以友善與字母“o”分辨。這也是我和它結緣的主要緣由,當年在某次acm比賽中,就是因為肉眼無法分辨是0還是o,遺憾至今。

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即可。官方圖如下:

打造自己的 Python 編碼環境

// 修改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的字型,不然還是亂碼)。如下:

打造自己的 Python 編碼環境

iterm2配色:從這裡可 以擷取很多别人的配色主題,把整個項目git clone下來,然後在iterm2的perferences中的colors最下面的load presets中import git下來的terminal檔案,自己可以根據自己喜歡調整自己喜歡的顔色,我選的是fish tank,我自己微調了一些配色。如下:

打造自己的 Python 編碼環境

vim配置

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        " 最大數量重載可復原行數 

" 特殊檔案打開 

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>_ 

" 處理折疊行的左右移動 

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 

"字型的設定 

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 

" 縮進和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 

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 

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>" 

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. 

        let s:ctrlp_fallback = 'find %s -type f' 

    if exists("g:ctrlp_user_command") 

        unlet g:ctrlp_user_command 

    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> 

" 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 

"判斷前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() 

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 

filetype on 

總結

一個好的程式員必須善用各種工具提升自己的開發效率,和提升自己的幸福指數。另外,個人覺得vim還是值得自己重新一配的,我的配置過程基本都是在 讀spf13的配置,讀懂,然後選取,可是中間還是出了很多問題,比如因為我沒有用pymode插件,是以在用括syntastic的 local_list在退出或者分屏時的位置,都令我很蛋疼。而且在讀spf13配置時,會發現有很多很好的功能都沒有用上,比如paste模式,查找沖 突快捷鍵,螢幕移動,還有ctrlp-funky,vim-multiple-cursors這些很爽的插件。大家有好的配置可以推薦給我。

原文位址:http://developer.51cto.com/art/201602/505465.htm