VIM - 编辑器之神

更新日期: 2020-04-27 阅读次数: 13905 字数: 1767 分类: Vim

Install

获取最新的 VIM 源码

$ cd /data/install/setupfiles/
$ hg clone https://vim.googlecode.com/hg/ vim
$ cd vim 
$ hg pull
$ hg update

编译、安装

在 Mac OS 下编译安装有一条原则,“永远不要覆盖 /usr/bin 下的文件”.
为了避免因卸载、覆盖自带的 VIM 带来不必要的麻烦,我们将 VIM 安装在 /opt/local 下。
因为 Mac OS X Mountain Lion 内置的 VIM 位于 /usr/bin/vim.
这样, 如果出现不兼容的问题,我们只需要删除 /opt/local 下的文件即可。
	$ which vim
	/usr/bin/vim
	$ vim --version
	VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Jun 20 2012 13:16:02)
	Compiled by root@apple.com
$ ./configure --prefix=/opt/local --with-features=huge --enable-pythoninterp=yes --enable-multibyte --with-vim-name=vim
$ make
$ sudo make install

修改系统环境变量 PATH

确认 ~/.bash_profile 中存在以下配置
	export PATH=/opt/local/bin:$PATH
使 /opt/local/bin 优先于 /usr/bin
重新加载 bash_profile,使配置生效 (即使上面的配置已存在,也要运行下面一行)
$ source ~/.bash_profile

确认安装成功

$ which vim
/opt/local/bin/vim
$ vim --version
VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Oct  8 2012 10:44:22)
MacOS X (unix) 版本
包含补丁: 1-684
编译者 zhongwei@Sun-Zhongweis-MacBook-Pro.local

果然间接修复了 Mac 自带 VIM 导致 VimWiki 链接地址不能自动隐藏的问题。

参考:

  • http://stackoverflow.com/questions/7211820/update-built-in-vim-on-mac-os-x

问题

若用上面的设置在 Ubuntu 12.04 上安装 VIM,查看 vimrc 时,会发现中文变成了乱码。

若采用 apt-get 安装则没有这个问题。

卸载

sudo make uninstall
  • http://stackoverflow.com/questions/1439950/whats-the-opposite-of-make-install-ie-how-do-you-uninstall-a-library-in-lin

Seven habits of effective text editing

http://www.moolenaar.net/habits.html

Pipe visually selected text into another file

选中需要处理的文本

:'<,'>w ~/Download/tmp.txt

若是想 pipe to a command

:'<,'>w !some_command

重新加载 vim 配置文件

:so ~/.vimrc

so is short for source

plugin layout in .vim directory

http://learnvimscriptthehardway.stevelosh.com/chapters/42.html

若要修改特定文件类型的默认 TAB 缩进,先将自带的配置文件从下面目录复制到自己的 目录,然后修改即可。

Mac OS 上 vim 的默认安装目录

/usr/share/vim/vim73/ftplugin

vi command pattern

(command)(number)(text object)

等同于

(number)(command)(text object)

第一种写法,读起来顺口; 但是有时候只能用第二种写法,例如:50i-

NERDTree

enable help doc

:helptags ~/.vim/doc

show help

:help NERDTree

use NERDTree

:NERDTree

refresh NERDTree:

  • r -> current directory
  • R -> root directory

taglist

http://www.vim.org/scripts/script.php?script_id=273 由于 taglist 依赖于 Exuberant Ctags,而 Mac OS 下的 ctags 是 UNIX ctags。 所以需要手动安装 Exuberant Ctags.

./configure && make && sudo make install

(Ubuntu: sudo apt-get install exuberant-ctags) 然后修改 .vimrc , 加入下面的配置

let Tlist_Ctags_Cmd="/usr/local/bin/ctags"

show help

:help :TlistToggle

use taglist

:TlistToggle

Paste registers in search or colon commands instead of using the clipboard

You can type Ctrl-R to insert a register when in insert mode, or when typing in the command or search line.

在 insert mode, 或者输入 vim 命令,搜索时,如果输入 会看到一个 " 提示,这时只需要输入对应的 register 标识,就能进行粘帖, 例如,a。 输入 " 对应 unnamed register.

在命令模式下,可以通过 来快速粘帖当前光标所在位置的单词。

include-search for c programming

Vim knows about include files, and can search them for a word you are looking for. The most common action is to lookup the prototype of a function. Position the cursor on the name of the function in your file and type [I: Vim will show a list of all matches for the function name in included files.

其他相关命令参见:help include-search

format json string in VIM

:%!python -m json.tool 

这里的 % 和查找替换时的作用一样,指选中 buffer 中的所有行。 所以,可以在写程序注释时,这样使用: 先输入一整行 json,为了快速输入,首次输入时,不进行排版

{"1": "http://...", "2": "http://...", "3": "http://..."}

然后使用 Ctrl-v 选中这行,输入

:!python -m json.tool 

注意: 需要将单引号都转换为双引号

:%s/\'/"/g

使用 :vimgrep + :cope 在 VIM 中遍历 grep 的搜索结果

例如我要遍历搜索所有包含 RequestHandler 的代码

新建一个 tab 页,否则搜索结果会覆盖掉当前窗口中的内容

:tabnew 

搜索, 当前窗口显示的是包含匹配的整个文件内容

:vimgrep RequestHandler **/*.py

显示所有匹配的列表

:cope

convert dos format to unix format

set ff=unix

不转换格式带来的问题, 例如将一个 python 文件加上可执行权限,执行时会报错:

env: python\r: No such file or directory

因为 dos format 是用 \r\n 做换行

反之亦然

set ff=dos

Auto format pasted code

我们从网上复制代码到 VIM 的时候,会变成逐行缩进的格式。那是因为每个换行都被 处理成了新的缩进。进入 paste-mode 就能避免这种问题。

:set paste

粘帖,之后退出 paste-mode.

:set nopaste

正则匹配中的特殊字符

http://vim.wikia.com/wiki/Search_and_replace

查找替换前进行确认 (confirm)

:%s/python/ipython/gc

Joining Two Lines

J

write with sudo

:w !sudo tee % > /dev/null

  • http://stackoverflow.com/questions/2600783/how-does-the-vim-write-with-sudo-trick-work

这个不太容易记住,可以自定义一个命令 Sudow 来执行. 在 .vimrc 里加入:

command Sudow execute "w !sudo tee % > /dev/null"

vim 中切换 panes

Ctrl-w h,j,k,l

移动到匹配的括号,以及C语言的注释 /* */,

以及 preprocessor conditional: #if, #ifdef, #else, #elif, #endif. TODO: 增加对模版语言及HTML标签的支持

%

数字自增列操作

实现一:

将一列数字全部自增一,例如原来是从 0 开始的,改为 1 开始 Ctrl-a 是光标处数字的自增,qa 录制一下, 每行执行一下即可。 若要生产一列自增数字 N 行, 从 1 开始 1 qa yy p Ctrl-a q @a 自减是 Ctrl-x

实现二(vimscript):

:for i in range(1, 10)|put =i|endfor

:for i in range(1, 10)|put ='current num is: '.i|endfor

Line complete

Ctrl-x Ctrl-l

更改 vim 的工作目录

cd <dir>

change word

  • cw: To the end of a word
  • c2b: Back 2 words
  • c$: To the end of line
  • c0: To the beginnging of line
  • c2w: change 2 words
  • cc: Change one line. (类似的:yy, dd)

switching to the previously edited buffer

Ctrl-6

在 VIM 中显示特殊符号 (例如,回车、TAB)

set list/set nolist
  • 回车会显示成 $
  • TAB 会显示成 ^I

vim with pyflakes "redefinition of unused" problem

加一个 assert 作为一个绕过的方法

    try:
        import json
        assert json  # silence pyflakes
    except ImportError:
        from django.utils import simplejson as json  # Python 2.4 fallback.

参考:

  • http://stackoverflow.com/questions/5033727/how-do-i-get-pyflakes-to-ignore-a-statement

指定文件类型

在新建一个没有文件名后缀的文件,或者一个没有文件名的 buffer 时, 是没有文件类型的,提示栏的 TYPE 为空。

如果我们需要使用特定文件类型对应插件的功能时,那么这里可以指定一个文件类型。 例如,我们想使用 vimwiki 插件的特性:

:set filetype=VIMWIKI

搜索时忽略大小写

例如我要搜索 Zhongwei 或者 zhongwei 或者 ZHONGWEI

/zhongwei\c    

将配置写在代码文件里

使用 modeline

  • https://wiki.python.org/moin/Vim

交换 vs 打开的两个 window 的位置

ctrl-w r (rotate)

git mergetool

+--------------------------------+
| LOCAL  |     BASE     | REMOTE |
+--------------------------------+
|             MERGED             |
+--------------------------------+

参考

  • http://vim.wikia.com/wiki/A_better_Vimdiff_Git_mergetool

与系统共享剪切板

参考 Mac OS X clipboard sharing

选取待复制的文本后,输入 "+y

在浏览器中打开当前链接

If you are using Vim 7.4 or later, in normal mode, put your cursor below the URL, then click gx, the URL will be opened in browser automatic.

参考 Open URL under cursor in Vim with browser

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式