最近要看Unix進階編程這本書,本想用Ubuntu下的Code::Blocks,單用著頗為不爽,最後決定用簡單而使用的Vim,下面是編程環境的配置。
一:首先是安裝gcc和Vim
安裝gcc:
sudo apt-get build-depgcc或者sudo apt-get install build-essential
我的之前在配置某個環境的時候已經裝好了。
Tminal中輸入:gcc -v 顯示gcc的版本資訊,我是4.4.5
安裝Vim
apt-get install vim-full
配置vimrc
先修改檔案的許可權
1、使用cd / 轉到檔案系統中;
2、cd/etc/vim 切換到vim目錄內;
3、修改檔案許可權 : sudo chmod 777 vimrc ,會提示你輸入密碼,輸入密碼即可;
4、vim vimrc 開啟vim檔案,修改內容,我是直接粘網上的,修改前記得備份。
" All system-wide defaults are set in $VIMRUNTIME/debian.vim (usually just
" /usr/share/vim/vimcurrent/debian.vim) and sourced by the call to :runtime
" you can find below. If you wish to change any of those settings, you should
" do it in this file (/etc/vim/vimrc), since debian.vim will be overwritten
" everytime an upgrade of the vim packages is performed. It is recommended to
" make changes after sourcing debian.vim since it alters the value of the
" 'compatible' option.
" This line should not be removed as it ensures that various options are
" properly set to work with the Vim-related packages available in Debian.
runtime! debian.vim
" Uncomment the next line to make Vim more Vi-compatible
" NOTE: debian.vim sets 'nocompatible'. Setting 'compatible' changes numerous
" options, so any other options should be set AFTER setting 'compatible'.
"set compatible
" Vim5 and later versions support syntax highlighting. Uncommenting the
" following enables syntax highlighting by default.
if has("syntax")
syntax on
endif
" If using a dark background within the editing area and syntax highlighting
" turn on this option as well
"set background=dark
" Uncomment the following to have Vim jump to the last position when
" reopening a file
"if has("autocmd")
" au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
"endif
" Uncomment the following to have Vim load indentation rules and plugins
" according to the detected filetype.
"if has("autocmd")
" filetype plugin indent on
"endif
" The following are commented out as they cause vim to behave a lot
" differently from regular Vi. They are highly recommended though.
"set showcmd " Show (partial) command in status line.
"set showmatch " Show matching brackets.
"set ignorecase " Do case insensitive matching
"set smartcase " Do smart case matching
"set incsearch " Incremental search
"set autowrite " Automatically save before commands like :next and :make
"set hidden " Hide buffers when they are abandoned
"set mouse=a " Enable mouse usage (all modes)
set tabstop=4
set softtabstop=4
set shiftwidth=4
set autoindent
set cindent
set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s
set nu
if &term=="xterm"
set t_Co=8
set t_Sb=^[[4%dm
set t_Sf=^[[3%dm
endif
" Source a global configuration file if available
if filereadable("/etc/vim/vimrc.local")
source /etc/vim/vimrc.local
endif
""""""""""""""""""""""""""""""""""""""
" 文法著色與高亮設定
""""""""""""""""""""""""""""""""""""""
"開啟文法高亮
syntax enable
syntax on
"配色
"colo ruki
"colors ruki
"由於目前沒有找到ruki外掛程式,暫時取消該功能,找到該外掛程式後,再啟用該功能
"設定高亮搜尋
:set hlsearch
""""""""""""""""""""""""""""""""""""""
" 檔案設定
""""""""""""""""""""""""""""""""""""""
set encoding=utf-8
set fileencoding=chinese
set fileencodings=ucs-bom,utf-8,chinese
set ambiwidth=double
"檢測檔案的類型
filetype on
"預設無備份
:set nobackup
:set nowritebackup
""""""""""""""""""""""""""""""""""""""
" 滑鼠設定
""""""""""""""""""""""""""""""""""""""
"滑鼠支援
if has('mouse')
:set mouse=a
endif
"使滑鼠用起來象微軟 Windows,似乎正好解決 連續多行縮排問題、退格問題
:behave mswin
""""""""""""""""""""""""""""""""""""""
" 編輯器設定
""""""""""""""""""""""""""""""""""""""
"顯示行號
set number
"Tab 寬度
:set ts=4
"自動縮排
:set sw=4
"C/C++ 風格的自動縮排
:set cin
"設定 C/C++ 風格自動縮排的選項
:set cino=:0g0t0(sus
"開啟普通檔案類型的自動縮排
:set ai
"在編輯過程中,在右下角顯示光線標位置的狀態行
:set ruler
"顯示匹配括弧
:set showmatch
"在insert模式下能用刪除鍵進行刪除
:set backspace=indent,eol,start
"程式碼摺疊功能, 命令 za
:set foldmethod=syntax
:set foldlevel=100 "啟動vim時不要自動摺疊代碼
"設定字型
":set guifont=courier/ 9
"當按右鍵視窗的時候, 彈出捷徑功能表
:set mousemodel=popup
"自動換行
if (has("gui_running")) "圖形介面下的設定
"指定不折行。如果一行太長,超過螢幕寬度,則向右邊延伸到螢幕外面
:set nowrap
"添加水平捲軸。如果你指定了不折行,那為視窗添加一個水平捲軸就非常有必要了
:set guioptions+=b
else "字元介面下的設定
set wrap
endif
""""""""""""""""""""""""""""""""""""""
" 快速鍵設定
""""""""""""""""""""""""""""""""""""""
"<F1> 功能表列與工具列隱藏與顯示動態切換
set guioptions-=m
set guioptions-=T
map <silent> <F1> :if &guioptions =~# 'T' <Bar>
\set guioptions-=T <Bar>
\set guioptions-=m <bar>
\else <Bar>
\set guioptions+=T <Bar>
\set guioptions+=m <Bar>
\endif<CR>
"<F2>code_complete.vim外掛程式:函數自動完成
if !exists("g:completekey")
let g:completekey = "<F2>" "hotkey
endif
"<F3><F4>大小寫轉換
map <F3> gu
map <F4> gU
"目前的目錄產生tags文法檔案,用於自動完成,函數提示:code_complete.vim OmniCppComplete.vim ...
"map <F5> :!ctags -R --c-kinds=+p --fields=+S . <CR>
map <F5> :!ctags -R --c-kinds=+p --c++-kinds=+p --fields=+iaS --extra=+q . <CR>
"函數和變數列表
map <F6> :TlistToggle<CR>
"檔案瀏覽器
map <F7> :WMToggle<CR>
let g:winManagerWindowLayout = "FileExplorer"
"檔案樹狀列表
map <F8> :NERDTree<CR>
"映射複製、粘貼、剪貼ctrl+c ctrl+v ctrl+x
:map <C-V> "+pa<Esc>
:map! <C-V> <Esc>"+pa
:map <C-C> "+y
:map <C-X> "+x
" 映射全選 ctrl+a
:map <C-A> ggVG
:map! <C-A> <Esc>ggVG
" 多行縮排
:map <Tab> >
:map <S-Tab> <
""""""""""""""""""""""""""""""""""""""
" 外掛程式設定
""""""""""""""""""""""""""""""""""""""
"開啟OmniCppComplete.vim
set nocp
filetype plugin on
"2Html外掛程式,啟用XHtml css
:let html_number_lines=1
:let html_use_css=1
:let use_xhtml=1
"fencview.vim 外掛程式設定
let g:fencview_autodetect = 1 "開啟檔案時自動識別編碼
let g:fencview_checklines = 10 "檢查前後10行來判斷編碼
"autocomplpop.vim & supertab.vim 外掛程式設定
let g:AutoComplPop_IgnoreCaseOption=1
set ignorecase
""""""""""""""""""""""""""""""""""""""
" 其他設定
""""""""""""""""""""""""""""""""""""""
"去掉討厭的有關vi一致性模式,避免以前版本的一些bug和局限
:set nocompatible
5.儲存退出
接下來是在Vim中編寫一個Helloworld,然後用gcc編譯運行。
在應用程式中找到GVim,
1.敲如以下代碼:
#include <stdio.h>
int
main (void)
{
printf ("Hello,world");
return 0;
}
儲存為h.c;
(註:Vim的基本操作可以在http://www.chinavim.org/學習)
2.在Vim中敲入:!gcc -Wall c/h.c -o hello
簡單解釋一下,:!在Vim中是調用外部命令的意思,-Wall是顯示編譯器的提示,之後就是源檔案的位置,-o 之後接目標檔案的檔案名稱。
3.再運行./hello,結果(註:gcc編譯之後最後產生的檔案在命令的目前的目錄,而不是源檔案的目錄,-Wall最好加上);
4.打完收工。
以後就可以擺脫那些複雜的IDE了,還原編程本身。