分类: Vi/Vim Mac OS X2013-01-01 22:08 3371人阅读 评论(0) 收藏 举报
 

目录(?)[+]

 

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.
 
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文件并添加如下内容;
 
2,然后使.bashrc文件生效,在终端type命令,source .bashrc即可;
之后我们就可以使用最新版本的MacVim了。

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
 

 

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. 
 
 
 
 
下面就是对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.
 
 
 
知道了配置文件的位置,然后就开始动手配置吧。
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=* 可以查看可以选择的字体。
 

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 
 

 
 

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退出小窗口。

 
 
  1. " MacVim configuration settings
  2. " 2013-01-16 gerryyang
  3. " -------------
  4. " 基本设置
  5. " -------------
  6. " 启用vim高级特性(置于配置最前)
  7. se nocompatible
  8. " 设置文件编码
  9. set encoding=utf-8
  10. set fenc=cp936
  11. set fileencodings=ucs-bom,utf-8,cp936,gb2312,gb18030,big5
  12. " 显示行号
  13. se nu
  14. " 设置默认字体
  15. se guifont=Monaco:h14
  16. " 设置默认窗口大小
  17. se lines=30 columns=80
  18. " 设置当前行高亮, cursorline
  19. se cul
  20. " 将当前光标下的列高亮, cursorcolumn
  21. " 对于Pythoner比较有用
  22. "se cuc
  23. " 显示光标位置
  24. se ruler
  25. " 记录上次打开的位置
  26. if has("autocmd")
  27. autocmd BufRead *.txt set tw=78
  28. autocmd BufReadPost *
  29. \ if line("'\"") > 0 && line ("'\"") <= line("$") |
  30. \   exe "normal g'\"" |
  31. \ endif
  32. endif
  33. " -------------
  34. " 搜索设置
  35. " -------------
  36. " enable magic matching
  37. set magic
  38. " show matching bracets
  39. set showmatch
  40. " highlight search things
  41. set hlsearch
  42. " ignore case when searching
  43. set smartcase
  44. set ignorecase
  45. " incremental match when searching
  46. set incsearch
  47. " -------------
  48. " 代码设置
  49. " -------------
  50. " 设置语法高亮
  51. syntax enable
  52. syntax on
  53. " 显示缩进tab线
  54. se list lcs=tab:\|\
  55. " 设置代码折叠
  56. set foldenable
  57. set foldnestmax=1
  58. set foldmethod=syntax
  59. " -------------
  60. " 缩进设置
  61. " -------------
  62. " auto indent
  63. set autoindent
  64. " c indent
  65. set cindent
  66. " smart indet
  67. set smartindent
  68. " use hard tabs
  69. set tabstop=4
  70. " 将空格转换为tab, :%retab
  71. set noexpandtab
  72. set shiftwidth=4
  73. " 自动换行
  74. " break long lines
  75. set textwidth=1000
  76. " 设置某列高亮
  77. "se cc=+1
  78. " config C-indenting
  79. set cinoptions=:0,l1,t0,g0
  80. " enable filetype plugin
  81. filetype plugin indent on
  82. " use soft tabs for python
  83. autocmd Filetype python set et sta ts=4 sw=4
  84. " -------------
  85. " 快捷键设置
  86. " -------------
  87. " format codes
  88. map <F8> ggVG= <CR>
  89. " open multiple files in tabs
  90. map <F0> :tabedit <CR>
 
 
后续补充

MacVim小试的更多相关文章

  1. Mac开发利器之程序员编辑器MacVim学习总结

    Emacs和Vim都是程序员专用编辑器,Emacs被称为神的编辑器,Vim则是编辑器之神.至于两者到底哪个更好用,网络上两大派系至今还争论不休.不过,相比之下,Emacs更加复杂,已经不能算是一个编辑 ...

  2. 小试ASP.NET MVC——一个邀请页面的实现

    上篇博客我们大体介绍了ASP.NET MVC以及如何去新建项目,这篇博客我们讲点干货.小试ASP.NET MVC,我们来写一个简单的邀请WEB. 先来建立一个Models,叫GuestResponse ...

  3. Brew安装MacVim

    brew install macvim --with-cscope --with-lua --with-python cscope lua python支持 附一些简单的brew命令 查看brew的帮 ...

  4. 启用vim-ruby的rails complete (macvim)

    macvim启用rails complete 用vim已经很久了, 一直用spf13也挺好的, 最近遇到要处理一个遗留项目, 数据库字段太多, 因此折腾了一下配置好vim的ruby-completio ...

  5. canvas游戏小试:画一个按方向键移动的圆点

    canvas游戏小试:画一个按方向键移动的圆点   自己对canvas,但又有一颗做游戏的心TT.然后记录一下对canvas的学习吧,用一个按方向键控制的小圆点来做练习.(编程时用了一些es6的语法) ...

  6. struts的hello world小试

    struts的hello world小试 前面jdk的安装和配置,tomcat的安装和配置以及java ide的安装和配置就不写了. 在项目中使用流程 创建一个Web项目 导如struts 2.0.1 ...

  7. StackExchange.Redis使用和封装小试

    StackExchange.Redis使用和封装小试 https://git.oschina.net/Ultralisk/ThorActor/blob/ThorActor/DBUtility/Redi ...

  8. -_-#【Mac】MacVim

    MacVim安装与配置 Mac开发利器之程序员编辑器MacVim学习总结 Git时代的VIM不完全使用教程 zencoding-vim This repository has moved to htt ...

  9. chrome扩展小试

    chorme 扩展 小试 官方文档 http://developer.chrome.com/extensions 非官方中文文档 http://docs.lmk123.com/getstarted.h ...

随机推荐

  1. Git 常用命令手记 及 Github协同流程(转)

    符号约定俗成:<xxx> 自定义内容xxx:[xxx] xxx为可选项:[<xxx>] 自定义内容xxx且为可选项. 说明/备注 命令 备注 保存更新 git add [-i] ...

  2. Drupal 7.31 SQL注射分析POC

    此漏洞昨日爆发 ,我们有时间去看看今天的代码. 于Drupal于,跑sql声明使用PDO型号,这是一般能够避免大部分的注射,由于使用占位符的sql语法语句是限制. 但是,这并不意味着绝对安全,. 在D ...

  3. HDU Today (图论)

    经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬 ...

  4. 微信公众号PHP简单开发流程

    原文:微信公众号PHP简单开发流程 微信公众号开发分傻瓜模式和开发者模式两种,前者不要考虑调用某些接口,只要根据后台提示傻瓜式操作即可,适用于非专业开发人员. 开发模式当然就是懂程序开发的人员使用的. ...

  5. SEO要领:8文章主持技巧(两)

    续篇:搜索引擎优化要领:8条辅助技巧(一) 四.检查你的robots.txt文件 与谷歌的蜘蛛通信的经常用法是使用robots.txt文件. 这是一个文本文件.同意你告诉搜索引擎,你的站点的网页上抓取 ...

  6. 远程数据client交换器

    不太繁忙的文本. 要被写入

  7. crawler_爬虫代理方案

    爬虫往往会遇到各种限制ip问题 理方案(爬虫) IP代理软件 优势标记: 是 自动切换IP 基本无开发成本标记: 黄色, 考虑切换IP时 ,网络瞬时异常 IP池,由商家维护 劣势标记: 非 部署 每个 ...

  8. mac_Mac item2常用快捷键

    整理使用 iTerm 2 过程中得常用快捷键,Mac 原来自带的终端工具 Terminal 不好用是出了名的,虽然最近几个版本苹果稍微做了些优化,功能上,可用性方面增强不少,无奈有个更好用的 Iter ...

  9. POJ 2586 Y2K Accounting Bug(枚举洪水问题)

    Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10674   Accepted: 53 ...

  10. 关于ios的autoLayout的一些简单介绍以及使用方法

    一.autoLayout的用途: 主要用于屏幕适配,尤其是出现了iphone6,plus之后. 二.怎么简单的用autoLayout呢? 点击左一,可以看到: 点击左二: 基本上要想autolayou ...