顯示具有 vim 標籤的文章。 顯示所有文章
顯示具有 vim 標籤的文章。 顯示所有文章

2015年11月23日 星期一

Vundle: Vim's plugin manager

之前安裝Vim的plugin都是透過Vim的官網下載,
這樣有個問題,就是如果plugin有更新,
你可能不知道,需要留心追蹤,
而如果安裝的plugin一多,
要一個一個追蹤與更新想必是一件相當累人的工作。
所以,Vim的plugin manager就應運而生,
有一個比較早期plugin manager名為pathogen
而網路上的評價比較好的則是Vundle
由於剛進入Vim plugin manager的世界,沒有包袱的我當然就是往Vundle邁進啦!
首先當然是安裝Vundle:
Debian有一個名為Vim-pathogen的套件,不過沒有Vunble,
但可以透過Vundle的GitHub上的文件一步一步來安裝,
首先,必須先安裝git:
Debian/Ubuntu:

apt-get install git


Fedora (22+):

dnf install git

接著下載並至於指定位置:

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



之後在~/.vimrc的開頭輸入:

set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required



再來,進入Vim並在normal mode中輸入:

:PluginInstall

或者,不進入Vim,直接在command-line下輸入:


vim +PluginInstall +qall

就會把Vundle的環境架好。

完成了Vundle的安裝與設置,接下來就是來安裝一些常用的plugins。
只要在上述~/.vimrccall vundle#begin()call vundle#end()之間,
插入像是:

Plugin 'scrooloose/nerdtree'

其中,引號內的字串表示:


此plugin在GitHub上的帳號/plugin的名稱

如果plugin沒有在GitHub上面,也可以有其他對應方式:

Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
Plugin 'L9'
" Git plugin not hosted on GitHub
Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Avoid a name conflict with L9
Plugin 'user/L9', {'name': 'newL9'}

之後進入Vim的normal mode輸入:


:PluginInstall

就會幫你把NerdTree這個好用的plugin給裝好了!

常用的Vundle指令除了:PluginInstall,還有:

:PluginList " 列出所有plugin :PluginInstall " 安裝plugin :PluginUpdate " 更新plugin :PluginClean " 清除未使用套件 :PluginSearch xxx " 搜尋plugin xxx

想要移除plugin,只要在~/.vimrc中移除該套件(Plugin xxx/xxx),

接著,在Vim normal mode下輸入:

:PluginClean

就可以移除該plugin了!

注意

最新版的的Vundle安裝plugin時是在~/.vimrc裡加入:

Plugin 'xxx/xxx'

而之前版本是使用:


Bundle 'xxx/xxx'

不過目前(2015/10)最新版可以向下相容。


參考資料:
Vundle
Arch Linux - 為 Vim 安裝套件管理工具 : Vundle ( Install Vundle for Vim on Arch Linux )

簡單自定Vim的顏色

雖然現在在terminal下用的顏色樣式是peachpuff
位置在:

/usr/share/vim/vim74/colors/peachpuff.vim
但如果未來想要自訂其他顏色樣式,
可以直接在normal mode或家目錄下的.vimrc輸入highlight這個指令:

highlight Comment ctermbg=DarkGray
上述的指令表示:「在terminal中所有註解(Comment)的背景變成黑灰色」。
ctermbg表示文字的背景,另有ctermfg表示文字的顏色。
除了改註解的顏色外,還有:

Comment, Constant, Normal, NonText, Special, Cursor...等等。
而顏色可以改成:

Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, Brown, 
DarkYellow, LightGray, LightGrey, Gray(Grey), Blue, LightBlue, Green, 
LightGreen, Cyan, LightCyan, Red, LightRed, Magenta, LightMagenta, 
Yellow, LightYellow, White...等等。
除了在terminal,在GUI(ex: GVim)中的顏色可以把ctermfg, ctermbg改成guifg, guibg,並且可以使用RGB的顏色標記來標記顏色:

#表示在GUI界面中所有「Constant」的顏色改為白色(#FFFFFF)
highlight Constant guifg=#FFFFFF
參考資料:
How to control/configure vim colors

2014年4月5日 星期六

Vim透過行號(line number)複製或刪除

在Normal mode下,輸入:

:23, 25 co 30

表示把23到25行之間(包括23和25行)的內容複製到第30行後,

也就是31行,

原本在31行的文字(或程式碼...)就會往後排序。

也可只輸入一個數字代表只複製一行:

:23 co 30



輸入:

:23, 25 del

表示刪除23到25行(包括23和25行)的內容,

原本在23至25行底下的內容會往上移。

2014年3月8日 星期六

Vim: 英文字母的大小寫轉換

以前轉換英文字母的大小寫都是透過人工的方式,

一不小心就會打錯字,

之後上網找方法,看到了Vim官網上的教學:

在Vim的normal mode下選取文字,並鍵入:

gu」:大寫轉成小寫,原本是小寫的維持小寫。

gU」:小寫轉成大寫,原本是大寫的維持大寫。

g~」:大寫的部份轉成小寫,小寫的部份轉成大寫。


參考資料:http://vim.wikia.com/wiki/Switching_case_of_characters

Vim: 多行插入空白

在用Vim Coding時常常會需要換行與縮排,

有時會遇到要縮排n行,

通常會透過「CTRL + v」來選取空白,

接著貼上於要縮排的區段的前面,

可是如果要縮排的對象是在每行開頭的位置,

就無法預先選取空白了。

如果遇到這樣的問題,可以不用一行一行的操作,

可以透過「CTRL + v」先選取要空白或Tab的部份,

接著鍵入「I」,

此時會跳至「CTRL + v」所選取的第一行,

輸入空白或Tab,接著隨意移動游標,

剛剛選取的每一行就會自動重複第一行的動作。


2014年1月27日 星期一

vim 折疊(fold)功能

以前就已經知道vim有折疊文字(程式碼)功能,不過那時候覺得還不需要,

也以為還要額外裝plugin,怕會很麻煩。

但如今開始會看一些比較長的程式碼,於是決定把它加入.vimrc

只要在.vimrc裡輸入:

set foldmethod=marker

就會以「{{{」來標示折疊的起始行,以「}}}」標示折疊的結束行,

如果沒有設定這一行的話,預設是不會顯示像「{{{」和「}}}」的縮排符號的,

而且一離開vim,fold就會取消,也就是下次開啟vim時fold就會不見。


另外,如果在.vimrc加入:

set foldlevel=0

可以讓開啟檔案時,預設將所有fold關閉。



接著來看fold的操作方式:


zo:開啟fold。
zc:關閉fold。
zm:全部縮起來(將 foldlevel-1)。
zM:全部縮起來(設 foldlevel=0)。
zf:製作出起始 fold 符號。
zF:製作出起始與結尾 fold 符號。
zi:啟用/關閉fold。
zO:打開所有fold。
zC:關閉所有fold。
zx:更新fold,即除了游標所在fold不關,其他都關掉。

其中zm和zM的差別目前還搞不清楚,搞清楚之後會再補上。

2013年12月6日 星期五

使用vim 內建的樣式(color scheme)

vim內建的樣式擺在:

/usr/share/vim/vim74/colors 

當中的數字"74"為vim的版本編號,

也就是7.4版,

請依照自己安裝的版本來作變更。

最後在.vimrc裡面加入:

:colorscheme your_choice

"your_choice"為你所選擇的樣式,

儲存完成後,樣式就會自動套用。

2013年11月13日 星期三

vim NERDTree 安裝(配置)

(2015/11/23:下列安裝方法是在認識Vundle之前,目前推薦使用Vundle)

首先到官方網站下載NERDTree:

http://www.vim.org/scripts/script.php?script_id=1658  
下載下來後解壓縮檔案


unzip nerdtree.zip

在~/.vim/建立plugin目錄(如果存在就不用建立了)

mkdir -p ~/.vim/plugin

將檔案 NERDTree.vim 放入剛建立的資料夾


mv plugin/NERD_tree.vim ~/.vim/plugin


開啟vim 後輸入:

:NERDTree

應該就可以看到NERDTree了!

-----------------------------------------------------

每次要輸入這麼多字很麻煩,所以可以設定快捷鍵

開啟vim 並編輯 .vimrc:

vim ~/.vimrc

 輸入:

 nmap <F5> <ESC>:NERDTree<CR>

讓按下<F5>時等於執行
 

:NERDTree



參考資料:
http://blog.longwin.com.tw/2009/02/vim-tree-explorer-nerdtree-plugin-2009/ 

2013年5月28日 星期二

不要讓Vim產生備份檔

不要讓Vim產生備份檔。 
ex: xxx.php~
 
set nobackup
set nowritebackup
set noswapfile

2013年4月28日 星期日

~/.vimrc

" vundle config
set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

Plugin 'scrooloose/nerdtree'
Plugin 'Lokaltog/vim-powerline'
"Plugin 'vim-airline/vim-airline'
"Plugin 'vim-airline/vim-airline-themes'
Plugin 'Yggdroot/indentLine'
Plugin 'kien/ctrlp.vim'
"Plugin 'Valloric/YouCompleteMe'

call vundle#end()            " required
filetype plugin indent on    " required

set hlsearch

" syntastic setting
set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*

let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0

" shown powerline in sigle buffer
set laststatus=2
set t_Co=256
let g:Powerline_colorscheme='solarized256'
"let g:Powerline_symbols = 'fancy'
"let g:airline_powerline_fonts = 1

" airline setting
"set laststatus=2
"set t_Co=256
"let g:airline_theme="bubblegum"
"let g:airline#extensions#tabline#enabled = 1

" tab
map <C-t>l <ESC>:tabnext<CR>
map <C-t>h <ESC>:tabprev<CR>
map <C-t>n <ESC>:tabnew<CR>:NERDTree<CR>
map <C-t>c <ESC>:tabclose<CR>
map <C-t>0 <ESC>:tabfirst<CR>
map <C-t>$ <ESC>:tablast<CR>
map <C-t>q <ESC>:qall<CR>

set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4

set autoindent
" C語言的縮排方法
"set cindent

set ruler
set showmode
set bg=dark
set nu

" terminal的title會設成filename
"set title

" 可以用 {{{ }}} 縮排 Folded
set foldmethod=marker
" 預設關閉
set foldlevel=0

" 編碼設定
set fileencoding=utf-8
set fileencodings=utf8,big5
set termencoding=utf-8
set enc=utf-8
set tenc=utf8

nmap <silent> <F5> <ESC>:NERDTree<CR>

" indent line 縮排對齊線plugin,預設關閉。
nmap <silent> <F6> <ESC>:IndentLinesToggle<CR>
let g:indentLine_enabled = 0

" 自動補齊括號
"inoremap ( ()<ESC>i
"inoremap [ []<ESC>i
"inoremap { {}<ESC>i

nnoremap <silent> <F8> :set paste<CR>
nnoremap <silent> <F9> :set nopaste<CR>

" vim color theme
colorscheme default
syntax on

" hightlight current line
set cursorline
hi CursorLine cterm=NONE ctermbg=234 ctermfg=NONE

" highlight TODO FIXME XXX with bold
hi Todo cterm=bold ctermbg=11

" highlight current line number
hi LineNr cterm=NONE ctermbg=234 ctermfg=245
"hi CursorLineNR cterm=NONE ctermbg=234 ctermfg=245

" 改變背景顏色顏色
"highlight Comment ctermbg=DarkGray
" 改變字體顏色
"highlight Comment ctermfg=DarkGray