Linux 開發環境搭建

  本文多參考自網上資料,在此多謝這些資料的做者的辛勤勞動!php

  另外,本文所用 Linux 版本爲 CentOS 7.1。html

終端配置

安裝及配置 Zsh

  在默認狀況下,Linux 下的終端是 bash,但其跟 zsh 相比起來仍是有點差距。博文使用 zsh 的九個理由就講了九個使用 zsh 的理由。java

  安裝 zsh,直接利用 yum 便可:python

sudo yum install zsh

  而後能夠直接用 oh-my-zsh 來配置 zsh:linux

sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"

或者用 curl:ios

sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

  接下來能夠編輯一下 ~/.zshrc 來修改 zsh 顏色主題及安裝插件。我的的該文件爲:git

# Path to your oh-my-zsh installation.
  export ZSH=/home/xiehongfeng100/.oh-my-zsh

# Set name of the theme to load.
# Look in ~/.oh-my-zsh/themes/
# Themes Link: https://github.com/robbyrussell/oh-my-zsh/wiki/themes
# Extra Themes Link: https://github.com/robbyrussell/oh-my-zsh/wiki/External-themes
# Optionally, if you set this to "random", it'll load a random theme each
# time that oh-my-zsh is loaded.
# ZSH_THEME="robbyrussell"
# ZSH_THEME="agnoster"
# ZSH_THEME="gentoo"
# ZSH_THEME="dogenpunk"
# ZSH_THEME="bureau"
# ZSH_THEME="avit"
# ZSH_THEME="af-magic"
ZSH_THEME="bira"

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion. Case
# sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"

# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# The optional three formats: "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git git-flow history-substring-search github zsh-syntax-highlighting)

# User configuration

  export PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/xiehongfeng100/.local/bin:/home/xiehongfeng100/bin"
# export MANPATH="/usr/local/man:$MANPATH"

  export LD_LIBRARY_PATH="/usr/local/systemc-2.3.1/lib-linux64:$LD_LIBRARY_PATH"

source $ZSH/oh-my-zsh.sh

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='mvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# ssh
# export SSH_KEY_PATH="~/.ssh/dsa_id"

# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
~/.zshrc

  須要注意的是,可能須要設置一下環境變量。具體可參考上一博文  Linux 中環境變量設置github

  下圖是我的終端效果:shell

  

  注:終端背景顏色是經過終端的 Edit->Profile Preference->Colors,具體兩個步驟以下:json

  

  

  RGB數值大概分別爲37 42 44

 

  關於 zsh 詳細配置可參考 archlinux 上的一篇文章 Zsh

 

  爲了更方便,還可安裝 autojump 插件:

sudo yum install autojump
sudo yum install autojump-zsh

  它有一個很是實用的功能,及可以快速切換到已經打開過的目錄,以下例:

$ pwd
/home/seisman
$ cd Desktop
$ cd /opt
$ cd /usr/local

# 用j命令迅速從/usr/local跳轉到與des匹配的目錄,這裏只有Desktop能夠匹配
$ j des
$ pwd
/home/seisman/Desktop

安裝及配置 TMUX

  關於 TMUX 的概念,博文 Tmux - Linux從業者必備利器說的很詳細生動。而另外一博文tmux的使用方法和個性化配置對TMUX的配置說的很不錯。

  安裝 TMUX 直接用 yum 就能夠了:

sudo yum install tmux

  接下來就主要配置下 TMUX 就好了。新建文件 ~/.tmux.conf:

#設置前綴爲Ctrl + x
set -g prefix C-x
#解除Ctrl+b 與前綴的對應關係
unbind C-b

  效果圖以下:

  

 

  部分 TMUX 快捷鍵可參考博文 tmux 快捷鍵

  其餘命令的使用須要先激活 TMUX,默認快捷鍵是 Ctrl + b。由於我已經修改了這個快捷鍵,因此是 Ctrl + x。

  

  

  

VIM 的配置

安裝 Vundle

  Vundle 的安裝參照其主頁便可:

$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

  在 ~/.vimrc 中添加以下內容:

" Vundle Setting
set nocompatible
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" Let Vundle manage itself
Plugin 'gmarik/Vundle.vim'
" Plugins
Plugin 'Valloric/YouCompleteMe'
Plugin 'scrooloose/syntastic'
Plugin 'bling/vim-airline'
Plugin 'SirVer/ultisnips'
Plugin 'edsono/vim-matchit'
Plugin 'elzr/vim-json'
Plugin 'honza/vim-snippets'
Plugin 'justinmk/vim-sneak'
Plugin 'kien/ctrlp.vim'
Plugin 'ludovicchabant/vim-lawrencium'
Plugin 'majutsushi/tagbar'
Plugin 'mhinz/vim-signify'
Plugin 'plasticboy/vim-markdown'
Plugin 'scrooloose/nerdcommenter'
Plugin 'sjl/gundo.vim'
Plugin 'tpope/vim-sleuth'
Plugin 'tpope/vim-surround'
Plugin 'tpope/vim-fugitive'
Plugin 'tyru/open-browser.vim'
Plugin 'vim-scripts/a.vim'
" Plugin 'Raimondi/delimitMate'

" Color schemes
Plugin 'tomasr/molokai'
Plugin 'flazz/vim-colorschemes'

call vundle#end()
filetype plugin indent on
Vundle Setting

  保存退出 vim 後從新進入 vim,執行 :BundelInstall。

  安裝過程以下圖(摘自 Vundle 主頁):

 

安裝 YouCompleteMe

  在上一步的基礎上,參照 YouCompleteMe 主頁,我的配置採用:

cd ~/.vim/bundle/YouCompleteMe
./install.py --clang-completer

  有時這樣子會提示 clang 沒有安裝(clang 已安裝),這時將次日語句改成:

./install.py --clang-completer --system-libclang

  另外,install.py 的參數是能夠經過

./install.py --help

來獲得的。

編輯 ~/.vimrc

  完整的 ~/.vimrc 文件以下:

" Vundle Setting
set nocompatible
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" Let Vundle manage itself
Plugin 'gmarik/Vundle.vim'
" Plugins
Plugin 'Valloric/YouCompleteMe'
Plugin 'scrooloose/syntastic'
Plugin 'bling/vim-airline'
Plugin 'SirVer/ultisnips'
Plugin 'edsono/vim-matchit'
Plugin 'elzr/vim-json'
Plugin 'honza/vim-snippets'
Plugin 'justinmk/vim-sneak'
Plugin 'kien/ctrlp.vim'
Plugin 'ludovicchabant/vim-lawrencium'
Plugin 'majutsushi/tagbar'
Plugin 'mhinz/vim-signify'
Plugin 'plasticboy/vim-markdown'
Plugin 'scrooloose/nerdcommenter'
Plugin 'sjl/gundo.vim'
Plugin 'tpope/vim-sleuth'
Plugin 'tpope/vim-surround'
Plugin 'tpope/vim-fugitive'
Plugin 'tyru/open-browser.vim'
Plugin 'vim-scripts/a.vim'
" Plugin 'Raimondi/delimitMate'

" Color schemes
Plugin 'tomasr/molokai'
Plugin 'flazz/vim-colorschemes'

call vundle#end()
filetype plugin indent on

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => YouCompleteMe  代碼自動補全  Begin
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Plugin 'Valloric/YouCompleteMe'
" youcompleteme  默認tab  s-tab 和自動補全衝突
" let g:ycm_key_list_select_completion=['<c-n>']
" let g:ycm_key_list_select_completion = ['<Down>']
" let g:ycm_key_list_previous_completion=['<c-p>']
" let g:ycm_key_list_previous_completion = ['<Up>']
" let g:ycm_confirm_extra_conf=0      " 關閉加載.ycm_extra_conf.py提示
let g:ycm_complete_in_comments = 1                "在註釋輸入中也能補全
let g:ycm_complete_in_strings = 1                "在字符串輸入中也能補全
let g:ycm_collect_identifiers_from_tags_files=1                 " 開啓 YCM 基於標籤引擎
let g:ycm_collect_identifiers_from_comments_and_strings = 1    "註釋和字符串中的文字也會被收入補全
let g:ycm_seed_identifiers_with_syntax=1            "語言關鍵字補全, 不過python關鍵字都很短,因此,須要的本身打開
let g:ycm_collect_identifiers_from_tags_files = 1
let g:ycm_min_num_of_chars_for_completion=2            " 從第2個鍵入字符就開始羅列匹配項
" 引入,能夠補全系統,以及python的第三方包 針對新老版本YCM作了兼容
" old version
if !empty(glob("~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py"))
    let g:ycm_global_ycm_extra_conf = "~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py"
endif
" new version
if !empty(glob("~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py"))
    let g:ycm_global_ycm_extra_conf = "~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py"
endif
"mapping
nmap <leader>gd :YcmDiags<CR>
nnoremap <leader>gl :YcmCompleter GoToDeclaration<CR>           " 跳轉到申明處
nnoremap <leader>gf :YcmCompleter GoToDefinition<CR>            " 跳轉到定義處
nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>
" 直接觸發自動補全
let g:ycm_key_invoke_completion = '<C-Space>'
" 黑名單,不啓用
let g:ycm_filetype_blacklist = {
        \ 'tagbar' : 1,
        \ 'gitcommit' : 1,
        \}
map <F9> :call SaveInputData()<CR>
func! SaveInputData()
    exec "tabnew"
    exec 'normal "+gP'
    exec "w! /tmp/input_data"
endfunc
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => YouCompleteMe  代碼自動補全  End
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" set t_Co=256
" set background=dark
" colorschem solarized

" 設置配色方案
colorscheme torte
" colorscheme murphy
" colorscheme desert 

"字體 
"if (has("gui_running")) 
"   set guifont=Bitstream\ Vera\ Sans\ Mono\ 10 
"endif 

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 顯示相關  
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"set shortmess=atI   " 啓動的時候不顯示那個援助烏干達兒童的提示  
"winpos 5 5          " 設定窗口位置  
"set lines=40 columns=155    " 設定窗口大小  
set go=             " 不要圖形按鈕  
"color asmanian2     " 設置背景主題  
"set guifont=Courier_New:h10:cANSI   " 設置字體  
"syntax on           " 語法高亮  
autocmd InsertLeave * se nocul  " 用淺色高亮當前行  
autocmd InsertEnter * se cul    " 用淺色高亮當前行  
"set ruler           " 顯示標尺  
set showcmd         " 輸入的命令顯示出來,看的清楚些  
"set cmdheight=1     " 命令行(在狀態行下)的高度,設置爲1  
"set whichwrap+=<,>,h,l   " 容許backspace和光標鍵跨越行邊界(不建議)  
"set scrolloff=3     " 光標移動到buffer的頂部和底部時保持3行距離  
set novisualbell    " 不要閃爍(不明白)  
set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}   "狀態行顯示的內容  
set laststatus=1    " 啓動顯示狀態行(1),老是顯示狀態行(2)  
set foldenable      " 容許摺疊  
set foldmethod=manual   " 手動摺疊  
"set background=dark "背景使用黑色 
" 顯示中文幫助
if version >= 603
    set helplang=cn
    set encoding=utf-8
endif

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""新文件標題
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"新建.c,.h,.sh,.java文件,自動插入文件頭 
autocmd BufNewFile *.cpp,*.[ch],*.sh,*.java exec ":call SetTitle()" 
""定義函數SetTitle,自動插入文件頭 
func SetTitle() 
    "若是文件類型爲.sh文件 
    if &filetype == 'sh' 
    call setline(1,"\#########################################################################") 
    call append(line("."), "\# File Name: ".expand("%")) 
    call append(line(".")+1, "\# Author: xiehongfeng") 
    call append(line(".")+2, "\# mail: xiehongfeng100@qq.com") 
    call append(line(".")+3, "\# Created Time: ".strftime("%c")) 
    call append(line(".")+4, "\#########################################################################") 
    call append(line(".")+5, "\#!/bin/bash") 
    call append(line(".")+6, "") 
    else 
    call setline(1, "/*************************************************************************") 
    call append(line("."), "    > File Name: ".expand("%")) 
    call append(line(".")+1, "    > Author: xiehongfeng") 
    call append(line(".")+2, "    > Mail: xiehongfeng100@qq.com") 
    call append(line(".")+3, "    > Created Time: ".strftime("%c")) 
    call append(line(".")+4, " ************************************************************************/") 
    call append(line(".")+5, "")
    endif
    if &filetype == 'cpp'
    call append(line(".")+6, "#include <iostream>")
    call append(line(".")+7, "using namespace std;")
    call append(line(".")+8, "")
    endif
    if &filetype == 'c'
    call append(line(".")+6, "#include <stdio.h>")
    call append(line(".")+7, "")
    endif
    "    if &filetype == 'java'
    "        call append(line(".")+6,"public class ".expand("%"))
    "        call append(line(".")+7,"")
    "    endif
    "新建文件後,自動定位到文件末尾
    autocmd BufNewFile * normal G
endfunc 

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"鍵盤命令
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
nmap <leader>w :w!<cr>
nmap <leader>f :find<cr>
" 映射全選+複製 ctrl+a
map <C-A> ggVGY
map! <C-A> <Esc>ggVGY
map <F12> gg=G
" 選中狀態下 Ctrl+c 複製
vmap <C-c> "+y
"去空行  
nnoremap <F2> :g/^\s*$/d<CR> 
"比較文件  
nnoremap <C-F2> :vert diffsplit 
"新建標籤  
map <M-F2> :tabnew<CR>  
"列出當前目錄文件  
map <F3> :tabnew .<CR>  
"打開樹狀文件目錄  
map <C-F3> \be  
"C,C++ 按F5編譯運行
map <F5> :call CompileRunGcc()<CR>
func! CompileRunGcc()
    exec "w"
    if &filetype == 'c'
    exec "!g++ % -o %<"
    exec "! ./%<"
    elseif &filetype == 'cpp'
    exec "!g++ % -o %<"
    exec "! ./%<"
    elseif &filetype == 'java' 
    exec "!javac %" 
    exec "!java %<"
    elseif &filetype == 'sh'
    :!./%
    elseif &filetype == 'py'
    exec "!python %"
    exec "!python %<"
    endif
endfunc
"C,C++的調試
map <F8> :call Rungdb()<CR>
func! Rungdb()
    exec "w"
    exec "!g++ % -g -o %<"
    exec "!gdb ./%<"
endfunc

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""實用設置
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 設置當文件被改動時自動載入
set autoread
" quickfix模式
autocmd FileType c,cpp map <buffer> <leader><space> :w<cr>:make<cr>
"代碼補全 
set completeopt=preview,menu 
"容許插件  
filetype plugin on
"共享剪貼板  
set clipboard+=unnamed 
"從不備份  
set nobackup
"make 運行
:set makeprg=g++\ -Wall\ \ %
"自動保存
set autowrite
set ruler                   " 打開狀態欄標尺
set cursorline              " 突出顯示當前行
set magic                   " 設置魔術
set guioptions-=T           " 隱藏工具欄
set guioptions-=m           " 隱藏菜單欄
"set statusline=\ %<%F[%1*%M%*%n%R%H]%=\ %y\ %0(%{&fileformat}\ %{&encoding}\ %c:%l/%L%)\
" 設置在狀態行顯示的信息
set foldcolumn=0
set foldmethod=indent 
set foldlevel=3 
set foldenable              " 開始摺疊
" 不要使用vi的鍵盤模式,而是vim本身的
set nocompatible
" 語法高亮
set syntax=on
" 去掉輸入錯誤的提示聲音
set noeb
" 在處理未保存或只讀文件的時候,彈出確認
set confirm
" 自動縮進
set autoindent
set cindent
" Tab鍵的寬度
set tabstop=4
" 統一縮進爲4
set softtabstop=4
set shiftwidth=4
" 不要用空格代替製表符
set noexpandtab
" 在行和段開始處使用製表符
set smarttab
" 顯示行號
set number
" 歷史記錄數
set history=1000
"禁止生成臨時文件
set nobackup
set noswapfile
"搜索忽略大小寫
set ignorecase
"搜索逐字符高亮
set hlsearch
set incsearch
"行內替換
set gdefault
"編碼設置
set enc=utf-8
set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
"語言設置
set langmenu=zh_CN.UTF-8
set helplang=cn
" 個人狀態行顯示的內容(包括文件類型和解碼)
"set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}
"set statusline=[%F]%y%r%m%*%=[Line:%l/%L,Column:%c][%p%%]
" 老是顯示狀態行
set laststatus=2
" 命令行(在狀態行下)的高度,默認爲1,這裏是2
set cmdheight=2
" 偵測文件類型
filetype on
" 載入文件類型插件
filetype plugin on
" 爲特定文件類型載入相關縮進文件
filetype indent on
" 保存全局變量
set viminfo+=!
" 帶有以下符號的單詞不要被換行分割
set iskeyword+=_,$,@,%,#,-
" 字符間插入的像素行數目
set linespace=0
" 加強模式中的命令行自動完成操做
set wildmenu
" 使回格鍵(backspace)正常處理indent, eol, start等
set backspace=2
" 容許backspace和光標鍵跨越行邊界
set whichwrap+=<,>,h,l
" 能夠在buffer的任何地方使用鼠標(相似office中在工做區雙擊鼠標定位)
set mouse=a
set selection=exclusive
set selectmode=mouse,key
" 經過使用: commands命令,告訴咱們文件的哪一行被改變過
set report=0
" 在被分割的窗口間顯示空白,便於閱讀
set fillchars=vert:\ ,stl:\ ,stlnc:\
" 高亮顯示匹配的括號
set showmatch
" 匹配括號高亮的時間(單位是十分之一秒)
set matchtime=1
" 光標移動到buffer的頂部和底部時保持3行距離
set scrolloff=3
" 爲C程序提供自動縮進
set smartindent
" 高亮顯示普通txt文件(須要txt.vim腳本)
au BufRead,BufNewFile *  setfiletype txt
"自動補全
:inoremap ( ()<ESC>i
:inoremap ) <c-r>=ClosePair(')')<CR>
:inoremap { {<CR>}<ESC>O
:inoremap } <c-r>=ClosePair('}')<CR>
:inoremap [ []<ESC>i
:inoremap ] <c-r>=ClosePair(']')<CR>
" :inoremap " ""<ESC>i
" :inoremap ' ''<ESC>i
function! ClosePair(char)
    if getline('.')[col('.') - 1] == a:char
    return "\<Right>"
    else
    return a:char
    endif
endfunction
filetype plugin indent on 
"打開文件類型檢測, 加了這句才能夠用智能補全
set completeopt=longest,menu
~/.vimrc

  VIM 最終效果以下: