天天看點

Mac OS X使用之——新年第一天弘法寺許願,MacVim小試0 什麼是MacVim?1 Vim的三種模式2 光标的移動3 Vim graphical keyboard cheat sheet4 Vim個性化設定

2013-01-01 wcdj

新年第一天,和lydia一起去了仙湖植物園,在弘法寺許了新年心願,祝福愛我的人和我愛的人,新的一年夢想成真。

下文是對MacVim的使用總結,特别推薦一個印度男孩(Swaroop C H)寫的《byte of vim》的manual,正在閱讀中。

一些參考:

http://code.google.com/p/macvim/ https://github.com/b4winckler/macvim/wiki https://github.com/b4winckler/macvim/wiki/FAQ http://www.vim.org/docs.php

http://www.swaroopch.com/notes/Vim_en-Table_of_Contents/

http://www.swaroopch.com/notes/Vim/

http://files.swaroopch.com/vim/byte_of_vim_v051.pdf

http://www.truth.sk/vim/vimbook-OPL.pdf

https://www.packtpub.com/sites/default/files/0509-chapter-2-personalizing-vim.pdf

http://www.douban.com/group/vim/

http://www.vimer.cn

http://lilydjwg.is-programmer.com

https://groups.google.com/forum/?fromgroups#!forum/vim_mac (vim_mac常見問題)

0 什麼是MacVim?

MacVim is the text editor Vim for Mac OS X. MacVim brings you the full power of Vim 7.3 to Mac OS X. Mac OS X,Vim Installation If you use Mac OS X, then you already have the terminal version of Vim installed. Run the menu command Finder -> Applications -> Utilities -> Terminal. In the terminal, run the command vim and press enter, you should now see the Vim welcome screen.

Mac OS X使用之——新年第一天弘法寺許願,MacVim小試0 什麼是MacVim?1 Vim的三種模式2 光标的移動3 Vim graphical keyboard cheat sheet4 Vim個性化設定

If you want to use a graphical version of Vim, download the latest version of the Cocoa-based MacVim project(http://code.google.com/p/macvim/). Double-click the file, it will be unarchived and a directory called MacVimxxx will be created. Open the directory, and copy the MacVim app to your Applications directory.

For more details MacVim differences, including how to run MacVim form the terminal see the macvim reference: 1. Click on Finder -> Applications -> MacVIm 2. Type :help macvim and press the Enter key. 

在Mac上配置MacVim為後續寫代碼做準備。 首先需要将系統預設的Vi/Vim替換為我們下載下傳的MacVim,方法如下: 1,在home目錄下,建立.bashrc檔案并添加如下内容;

Mac OS X使用之——新年第一天弘法寺許願,MacVim小試0 什麼是MacVim?1 Vim的三種模式2 光标的移動3 Vim graphical keyboard cheat sheet4 Vim個性化設定

2,然後使.bashrc檔案生效,在終端type指令,source .bashrc即可; 之後我們就可以使用最新版本的MacVim了。

Mac OS X使用之——新年第一天弘法寺許願,MacVim小試0 什麼是MacVim?1 Vim的三種模式2 光标的移動3 Vim graphical keyboard cheat sheet4 Vim個性化設定

1 Vim的三種模式

There are three basic modes in Vim - normal, insert and visual. [1] Normal mode is where you can run commands. This is the default mode in which Vim starts up. [2] Insert mode is where you insert i.e. write the text. [3] Visual mode is where you visually select a bunch of text so that you can run a command/operation only on that part of the text. 

Command          Action i                         insert text just before the cursor I                         insert text at the start of the line a                        append text just after the cursor A                        append text at the end of the line o                        open a new line below O                       open a new line above s                        substitute the current character S                        substitute the current line r                         replace the current character R                        replace continuous characters

Mac OS X使用之——新年第一天弘法寺許願,MacVim小試0 什麼是MacVim?1 Vim的三種模式2 光标的移動3 Vim graphical keyboard cheat sheet4 Vim個性化設定

2 光标的移動

在Vim中完全使用快捷鍵來實作光标的移動,下面總結常用的shortcut keys: [1] 兩個位置的切換 `` [2] 多個光标位置切換,Jump around You want to jump back to the previous location? Press ctrl-o to jump forward to the next location? Press ctrl-i [3] 行首和行末 ^ key moves to the start of the line $ key moves to the end of the line [4] 跳轉到某行、行首、行末 If you know the absolute line number that you want to jump to, say line 50, press 50G and Vim will jump to the 50th line. If no number is specified, G will take you to the last line of the file. How do you get to the top of the file? Simple, press 1G. (或者gg)

[5] 上頁和下頁 ctrl-b which means move one screen 'b'ackward ctrl-f which means move one screen 'f'orward

[6] 視窗内跳轉,上、中、下 What if you wanted to the middle of the text that  is currently being shown in the window? Press H to jump as 'h'igh as possible (first line of the window) Press M to jump to the 'm'iddle of the window Press L to jump as 'l'ow as possible (last line being displayed) [7] Word, Sentences, Paragraphs的移動 w, move to the next 'w'ord e, move to the 'e'nd of the next word b, move one word 'b'ackward ), move to the next sentence (, move to the previous sentence }, move to the next paragraph {, move to the previous paragraph [8] 設定自己的mark,友善跳轉(my favorite) Use ma to create a mark named 'a'. Move the cursor to wherever you want. Press 'a (i.e. single quote followed by the name of the mark) , Vim jumps (back) to the line where that mark was located. You can use any alphabet (a-zA-Z) to name a mark which means you can have up to 26*2=52 named marks for each file. 

3 Vim graphical keyboard cheat sheet

http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html Summary: Notice that our efficiency in using Vim is directly proportional to the efficiency of using the keyboard. 

Mac OS X使用之——新年第一天弘法寺許願,MacVim小試0 什麼是MacVim?1 Vim的三種模式2 光标的移動3 Vim graphical keyboard cheat sheet4 Vim個性化設定

下面就是對MacVim進行一些個性化的設定了。

4 Vim個性化設定

1,如何對安裝好的MacVim進行配置?

How do I save configuration settings? Type :h vimrc-intro for more information on Vim configuration files. 

You probably got tired of typing commands that  you use very often. To start Vim with all your favorite option settings and mappings, you write them in what is called the vimdc file. Vim executes the commands in this file when it starts up. the :version command mentions the name of the "user vimrc file" Vim looks for.

Mac OS X使用之——新年第一天弘法寺許願,MacVim小試0 什麼是MacVim?1 Vim的三種模式2 光标的移動3 Vim graphical keyboard cheat sheet4 Vim個性化設定
Mac OS X使用之——新年第一天弘法寺許願,MacVim小試0 什麼是MacVim?1 Vim的三種模式2 光标的移動3 Vim graphical keyboard cheat sheet4 Vim個性化設定

知道了配置檔案的位置,然後就開始動手配置吧。 touch ~/.vimrc vim ~/.vimrc 例如,(注釋行使用雙引号标示) " 顯示行号 se nu

2,修改MacVim的字型,選擇一種自己喜歡的字型,下面使用Mac的Monaco等寬字型(Monospaced Font)

How can I change the default font? Add the line set guifont=Monaco:h14 to your .vimrc to set Monaco as the default font with a size of 14. 通過指令,:se guifont=* 可以檢視可以選擇的字型。

Mac OS X使用之——新年第一天弘法寺許願,MacVim小試0 什麼是MacVim?1 Vim的三種模式2 光标的移動3 Vim graphical keyboard cheat sheet4 Vim個性化設定

3,設定預設MacVim打開時預設視窗的大小

How can I set the default size of new windows? Add the line set lines=40 to your .vimrc to make 40 the default number of rows. se lines=30 columns=80

4,設定搜尋高亮

Searching Options——Highlighting The following command causes Vim to highlight any strings found matching search pattern: :se hlsearch To turn off search highlighting, use: :se nohlsearch 

5,代碼對齊功能

方案1:(代碼多層次時,看起來會比較亂) 若用tab縮進,可以:set list lcs=tab:\|\ (此處是個空格); 若用空格縮進,可以使用 https://github.com/Yggdroot/indentLine 這個插件;

方案2:(感覺線條比較粗) vim-indent-guides,Vim 7.2+ Indent Guides is a plugin for visually displaying indent levels in Vim.

https://github.com/nathanaelkane/vim-indent-guides 方案3:(對于Python的強制對齊會比較友善) 設定某列高亮,此功能非常适合Pythoner,Vim 7.3+ se cc=80 方案4:(這個Plugin非常不錯,節省人肉勞動,正在使用中) Vim 插件  Tabular  允許你在 Vim 中按等号、冒号、表格等來對齊文本,對于經常寫代碼的朋友來說,有 Tabular 将會非常友善。

http://linuxtoy.org/archives/tabular.html https://github.com/godlygeek/tabular http://vimcasts.org/episodes/aligning-text-with-tabular-vim/ http://www.vim.org/scripts/script.php?script_id=3464

Can't install vim Tabular Plugin

IIRC, Tabular comes with an after directory which contains a plugin in its own directory, an autoload directory, a doc directory & a plugin directory.

So just copy the contents of those directories to their counterparts in $HOME/.vim/ (making any directory that does not already exist) & you're good to go.

使用方法:

Visual模式,vi} :Tab/=

或者,指定要格式化的範圍,:100,200 Tab/=

或者,再省事點兒直接映射為快捷鍵:

" Tabular codes, '='

map <F1> :Tab/= <CR>

" Tabular codes, ',' (http://vimdoc.sourceforge.net/htmldoc/pattern.html#/\zs)

map <F2> :Tab/,\zs <CR>

方案5:(還沒有使用,暫時記錄下) Align : Help folks to align text, eqns, declarations, tables, etc  

http://www.vim.org/scripts/script.php?script_id=294 

Mac OS X使用之——新年第一天弘法寺許願,MacVim小試0 什麼是MacVim?1 Vim的三種模式2 光标的移動3 Vim graphical keyboard cheat sheet4 Vim個性化設定

6,文法高亮

What if you wanted to see colors for different parts of your code? Run :syntax on. If it doesn't recognize the language properly, use :set filetype=Wikipadia, for example.

7,設定代碼折疊

What if you wanted  different parts of your file to be folded so that you can concentrate on only one part at a time? Run :set foldmethod=indent assuming your file is properly indented. There are other methods of folding as well.

8,多tabs編輯

What is you wanted to open multiple files in tabs? Use :tabedit <file> to open multiple files in "tabs" (just like browser tabs), and use gt to switch between the tabs.

9, 自動補齊功能

You use some words frequently in your document and wish there was a way that it could be quickly filled in the next time you use the same word? While in insert mode, press ctrl-x ctrl-n to see the list of "completions" for the current word, based on all the words that you have used in the current document. Switch between possibilities with ctrl-next and ctrl-previous Alternatively, use :ab mas Maslow's hierarchy of needs to expand the abbreviation automatically when you type mas <space>.

10,按列選擇文本

You have some data where only the first 10 characters in each line are useful and the rest is no longer useful for you. How do you get only that data? Press ctrl-v, select the text and press y to copy the selected rows and columns of text.

11, 将文本的内容從小寫全部轉換為大寫

What if you received a document from someone which is in all caps, find it irritating and want to convert it to lower case? In Vim, run the following: : for i in range(0, line('$')) :     call set line(i, to lower(get line(i))) :endfor 更簡單的方法是: There is an even simpler method of selecting all the text (ggVG) and using the u operator to convert to lowercase. 

12,打開文檔中的檔案

What if you wanted to open a file whose name is in the current document and the cursor is placed on that name? Press gf (which means 'g'o to this 'f'ile)

13,多視圖編輯

What if you wanted to view two different parts of the same file simultaneously? Run :sp to 'split' the view

14,文本中尋找某個關鍵字

How do you search the file for the current word that the cursor is currently placed on? Press *

15,文本替換

How to do a find and replace only in lines 50-100? Run :50,100 s/old/new/g

16,設定配色方案

What if you wanted to choose a better color scheme for the display? Run :colorscheme desert to choose the 'desert' color scheme (my favorite)

17,建立快捷鍵

What if you wanted to map the keyboard shortcut ctrl-s to save the file? Run :mmap <c-s>:w<CR>. Note that <CR> means a 'c'arriage 'r'eturn, i.e., the enter key.

18,快速使用曆史指令

q:

q/

打開一個最近使用指令的小視窗;後一個為打開最近搜尋指令的小視窗在小視窗敲

RETURN

鍵會執行所在行的指令。使用

:q

退出小視窗。

" MacVim configuration settings
" 2013-01-16 gerryyang

" -------------
" 基本設定
" -------------

" 啟用vim進階特性(置于配置最前)
se nocompatible

" 設定檔案編碼
set encoding=utf-8
set fenc=cp936
set fileencodings=ucs-bom,utf-8,cp936,gb2312,gb18030,big5

" 顯示行号
se nu

" 設定預設字型
se guifont=Monaco:h14

" 設定預設視窗大小
se lines=30 columns=80

" 設定目前行高亮, cursorline
se cul

" 将目前光标下的列高亮, cursorcolumn
" 對于Pythoner比較有用
"se cuc

" 顯示光标位置
se ruler

" 記錄上次打開的位置
if has("autocmd")
autocmd BufRead *.txt set tw=78
autocmd BufReadPost *
\ if line("'\"") > 0 && line ("'\"") <= line("$") |
\   exe "normal g'\"" |
\ endif
endif


" -------------
" 搜尋設定
" -------------

" enable magic matching
set magic

" show matching bracets
set showmatch

" highlight search things
set hlsearch

" ignore case when searching
set smartcase
set ignorecase

" incremental match when searching
set incsearch

" -------------
" 代碼設定
" -------------

" 設定文法高亮
syntax enable
syntax on

" 顯示縮進tab線
se list lcs=tab:\|\ 

" 設定代碼折疊
set foldenable
set foldnestmax=1
set foldmethod=syntax

" -------------
" 縮進設定
" -------------

" auto indent
set autoindent

" c indent
set cindent

" smart indet
set smartindent

" use hard tabs
set tabstop=4
" 将空格轉換為tab, :%retab
set noexpandtab
set shiftwidth=4

" 自動換行
" break long lines
set textwidth=1000
" 設定某列高亮
"se cc=+1

" config C-indenting
set cinoptions=:0,l1,t0,g0

" enable filetype plugin
filetype plugin indent on
 
" use soft tabs for python
autocmd Filetype python set et sta ts=4 sw=4

" -------------
" 快捷鍵設定
" -------------

" format codes
map <F8> ggVG= <CR>

" open multiple files in tabs
map <F0> :tabedit <CR>

           

後續補充