Vi&Vim应用

知らなかったVimの使い方

お勧めサイト

 
$ vim file +54    ファイルを開いて 54 行目へ移動 
:ls    バッファのリストを表示。まあ buffers 使ってたからそれでもいいんだけど。 
80|    指定カラムへ移動。この場合は 80 行目へ。 
Ctrl+y    Ctrl+e の逆スクロール。非常によく忘れて困る。 
zt, zz, zb    現在行を画面の一番上、真ん中、下にもってくる。 
CTRL-Z    vimをサスペンド。 :st でやってたけど、こっちの方がいいなー。 
:10,20d    10行目から20行目を削除。すぐに VisualMode に頼ってしまうので… 
g~~    現在行1行分で、大文字/小文字の切り替え。 
g~w    カーソル以降の単語の大文字/小文字の切り替え。 
<Ctrl-W>o    ウィンドウ分割してるときに、現在のウィンドウ以外を閉じる。 
Ctrl+c    コマンドラインとかインサートモードとかビジュアルモードから抜ける。そういえばそうだった… 
*    カーソル位置の単語を検索。たまに何か間違ってなってたけど、* だったのを知った… 
#    カーソル位置の単語を逆方向に検索。これは知らなかった。  

vim常用命令

 
u           撤销上一次的操作,Undo 
U           撤销对当前行的所有修改 
CTRL-R      Redo 

qa          开始录制宏, 保存在寄存器a中, 再按一次q结束录制 
@a          播放寄存器a中的宏 
@@          重复播放上一个宏 
99@a        播放寄存器a中的宏, 重复99次 

=a{         缩排当前语句块 
gg=G        缩排当前文件 

CTRL-]      跳转 
CTRL-T      跳转返回 
CTRL-O      跳转返回起点 

gd          局部变量(符号)定义处 
gD          全局变量(符号)定义处 
gf          调入光标处的文件 

/string     向后搜索字符串 
?string     向前搜索字符串 
n           重复上一次搜索 
N           反向重复上一次搜索 
:nohlsearch 取消加亮, 缩写:nohl 

hjkl        光标移动 

^F / PgDn   向后翻页 
^B / PgUp   向前翻页 

^           当前行首 
$           当前行尾 

0           移动到行首 
H           移动到屏幕上第一个可见行 
L           移动到屏幕上末一个可见行 
M           移动到屏幕中间的行 

+ / <cr>    移动到下一行首的非空字符 
-           移动到上一行 

{           上一个空行 
}           下一个空行 

J           连接两行,下一行上提 

ma          标记当前行位置, 记录在寄存器a中 
`a          返回寄存器a所标记的行(书签) 

.           重复上一个命令 

23G         定位到23行 
G           文件尾 

%           括号配对 

fx          在当前行向后查找下一个字符 
Fx          在当前行向前查找下一个字符 
;           重复前一个 f 或 F 命令. 

:s/from/to/ 查找和替换 
:s/from/to/g 查找和替换 
:%s/from/to/g 查找和替换 

:map <F2> dd 定义F2的功能 

i           进入插入模式,在当前字符前插入 
a           在当前字符后开始插入 
A           在行尾插入 
o           在当前行后插入一行,并进入插入模式 
O           在当前行前插入一行,并进入插入模式 

:w [name]   保存到文件 
:q          退出 
:q!         放弃保存退出 
:wq [name] 保存并退出 
:e name     编辑新文件 
:r name     把一个文件读入到当前中间中 
:e! name    放弃修改,编辑新文件 

rx          将当前光标处的字符替换成字符x 

x / Delete 删除光标处的字符 

dw          删除一个词 
dd          删除一行 
3dd         删除三行 
D           删除到行尾 
d$          删除到行尾 
dG          删除到文件尾 
dgg         删除到文件头 

yy          复制当前行到内部寄存器 
Y           复制到行尾 
yw          复制一个词 
y2w         复制两个词 
ye          复制一个词 
y$          复制到行尾 
p           粘贴内部寄存器内容到当前行 
"*yy        复制当前行到系统剪贴板 
"*p         粘贴系统剪贴板到当前行 

~           改变当前光标下字符的大小写 
*           搜索当前光标下的单词  

:set tabstop=4 
:set shiftwidth=4 
:set cindent 
:set foldmethod=indent 
:set foldcolumn=4 
:set foldlevel=3 

zf          创建折叠块 
zo          打开折叠块, zO 
zc          关闭折叠块, zC 
zr          打开多层折叠块 
zm          关闭多层折叠块 
zR          打开全部折叠块 
zM          关闭全部折叠块 
zd          删除 
zD 
zn          禁用折叠块 
zN          启用折叠块 
zi          在zn 和 zN之间来回切换 

:set nowrap 
:set lines=50 
:set columns=88 
:winpos 290 3 

:map <F8> =a{ 
:map <C-F8> gg=G 

:set nobackup 不产生备份文件  
:set expandtab 制表符换成空格 

:echo 10 + 5 * 2 

:set path+=/usr/*/include 指定头文件检索目录 
[I          标识符检索 
[D          检索#define定义的符号 

>>          增加缩进 
<<          减少缩进 
>a{         整块增加缩进, 包括{和} 
>af         整块增加缩进 

cw          删除当前词,并进入编辑状态 
ce          删除当前词,并进入编辑状态 
cc          擦掉当前行,并进入编辑状态 
c$          擦除到行尾,并进入编辑状态 

xp          交换两个字符的位置 

=a{         重新格式化当前块(C语言) 
gg=G        重新格式化当前文件(不太好用,有些情况不能处理) 
qa          开始录制宏,记录在变量a中,共有26个变量,按q停止录制 
@a          播放宏 
CTRL+]      跳转到当前光标所在单词对应的主题 
CTRL+T      回到前一个位置 
CTRL+O      回到前一个位置 
dw          删除一个词 
dd          删除一行 
3dd         删除三行 
/string     字符串搜索 
?string<cr> 同 "/", 向前查找 
n           重复上一个 "/" 或 "?" 命令 
^F or PgDn 向后翻页 
^B or PgUp 向前翻页 
$           光标移动到当前行尾 
nG          移动光标到第n行 
%           括号配对 
x or Delete 删除一个字符 
G           光标移动到文件最后一行 
i           进入插入模式,在当前字符前插入 
a           在当前字符后开始插入 
A           在行尾插入 
o           在当前行后插入一行,并进入插入模式 
O           在当前行前插入一行,并进入插入模式 
fx          在当前行向后查找下一个字符 
Fx          在当前行向前查找下一个字符 
;           重复前一个 f 或 F 命令. 
+ or <cr>   移动到下一行首的非空字符 
-           移动到上一行 
0           移动到行首 
H           移动到屏幕上第一个可见行 
L           移动到屏幕上末一个可见行 
M           移动到屏幕中间的行 
J           连接两行,下一行上提 
rx          将当前光标处的字符替换成字符 x. 
.           重复前一个命令 
'x          光标移动到标记x处 
mx          在当前行做个标记,x可以是任何小写字母 
/string     搜索 
?string     反向搜索 
n           重复上次搜索 
N           重复上次搜索,反方向 
:w [name]   保存到文件 
:q          退出 
:q!         放弃保存退出 
:wq [name] 保存并退出 
:e name     编辑新文件 
:r name     把一个文件读入到当前中间中 
:e! name    放弃修改,编辑新文件 
 
:g/str1/d      #删除匹配str1的行 
|删除每行第一个字符
:%s/^.//g    #删除每行第一个字符
 
:help encoding-values  #查看当前支持编码 

使用例

 
:g/^[^00]/d  #删除以非00开始的行 
 
:g/^$/d #删除空行 
 
:q 或 ZZ #退出当前窗口 
 
:colo darkblue #改变当前本色方案 
:set go=       #去掉窗口菜单等 
 
g/^#/d         #删除以#开始的行 
 
To get rid of those ^M use:  

:1,$ s/^M//gc  
or  
:% s/^M//gc    #按CTRL+vm打出^M符号 

_vimrc

"ORG環境設定 
set nocompatible 
"source $VIMRUNTIME/vimrc_example.vim 
source $VIMRUNTIME/mswin.vim 
behave mswin 
 
set diffexpr=MyDiff() 
function MyDiff() 
  let opt = '-a --binary ' 
  if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif 
  if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif 
  let arg1 = v:fname_in 
  if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif 
  let arg2 = v:fname_new 
  if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif 
  let arg3 = v:fname_out 
  if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif 
  let eq = '' 
  if $VIMRUNTIME =~ ' ' 
    if &sh =~ '\<cmd' 
      let cmd = '""' . $VIMRUNTIME . '\diff"' 
      let eq = '"' 
    else 
      let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"' 
    endif 
  else 
    let cmd = $VIMRUNTIME . '\diff' 
  endif 
  silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq 
endfunction 
 
"環境設定 
set nu 
set ic 
set ts=4 
set shiftwidth=4 
set nobackup 
set nu 
set lines=43 
set columns=100 
set linebreak 
set display=lastline 
set go=e 
behave mswin 
:colo darkblue 
:lang eng 
 
"----------------------------------------------------------------------------- 
" vim: set ts=4 sw=4 sts=0: 
"----------------------------------------------------------------------------- 
" 文字コード関連 
" 
if &encoding !=# 'utf-8' 
	set encoding=utf-8 
	set fileencoding=utf-8 
endif 
if has('iconv') 
	let s:enc_euc = 'euc-jp' 
	let s:enc_jis = 'iso-2022-jp' 
	" iconvがeucJP-msに対応しているかをチェック 
	if iconv("\x87\x64\x87\x6a", 'cp932', 'eucjp-ms') ==# "\xad\xc5\xad\xcb" 
		let s:enc_euc = 'eucjp-ms' 
		let s:enc_jis = 'iso-2022-jp-3' 
	" iconvがJISX0213に対応しているかをチェック 
	elseif iconv("\x87\x64\x87\x6a", 'cp932', 'euc-jisx0213') ==# "\xad\xc5\xad\xcb" 
		let s:enc_euc = 'euc-jisx0213' 
		let s:enc_jis = 'iso-2022-jp-3' 
	endif 
	" fileencodingsを構築 
	if &encoding ==# 'utf-8' 
		let s:fileencodings_default = &fileencodings 
		let &fileencodings = s:enc_jis .','. s:enc_euc .',cp932' 
		let &fileencodings = &fileencodings .','. s:fileencodings_default 
		unlet s:fileencodings_default 
	else 
		let &fileencodings = &fileencodings .','. s:enc_jis 
		set fileencodings+=utf-8,ucs-2le,ucs-2 
		if &encoding =~# '^\(euc-jp\|euc-jisx0213\|eucjp-ms\)$' 
			set fileencodings+=cp932 
			set fileencodings-=euc-jp 
			set fileencodings-=euc-jisx0213 
			set fileencodings-=eucjp-ms 
			let &encoding = s:enc_euc 
			let &fileencoding = s:enc_euc 
		else 
			let &fileencodings = &fileencodings .','. s:enc_euc 
		endif 
	endif 
	" 定数を処分 
	unlet s:enc_euc 
	unlet s:enc_jis 
endif 
" 日本語を含まない場合は fileencoding に encoding を使うようにする 
if has('autocmd') 
	function! AU_ReCheck_FENC() 
		if &fileencoding =~# 'iso-2022-jp' && search("[^\x01-\x7e]", 'n') == 0 
			let &fileencoding=&encoding 
		endif 
	endfunction 
	autocmd BufReadPost * call AU_ReCheck_FENC() 
endif 
" 改行コードの自動認識 
set fileformats=unix,dos,mac 
" □とか○の文字があってもカーソル位置がずれないようにする 
if exists('&ambiwidth') 
	set ambiwidth=double 
endif 
 
"----------------------------------------------------------------------------- 
" 編集関連 
" 
"オートインデントする 
set autoindent 
"バイナリ編集(xxd)モード(vim -b での起動、もしくは *.bin で発動します) 
augroup BinaryXXD 
	autocmd! 
	autocmd BufReadPre  *.bin let &binary =1 
	autocmd BufReadPost * if &binary | silent %!xxd -g 1 
	autocmd BufReadPost * set ft=xxd | endif 
	autocmd BufWritePre * if &binary | %!xxd -r | endif 
	autocmd BufWritePost * if &binary | silent %!xxd -g 1 
	autocmd BufWritePost * set nomod | endif 
augroup END 
 
"----------------------------------------------------------------------------- 
" 検索関連 
" 
"検索文字列が小文字の場合は大文字小文字を区別なく検索する 
set ignorecase 
"検索文字列に大文字が含まれている場合は区別して検索する 
set smartcase 
"検索時に最後まで行ったら最初に戻る 
set wrapscan 
"検索文字列入力時に順次対象文字列にヒットさせない 
set noincsearch 
 
"----------------------------------------------------------------------------- 
" 装飾関連 
" 
"シンタックスハイライトを有効にする 
if has("syntax") 
	syntax on 
endif 
"行番号を表示しない 
set number 
"タブの左側にカーソル表示 
set listchars=tab:\ \  
set list 
"タブ幅を設定する 
set tabstop=4 
set shiftwidth=4 
"入力中のコマンドをステータスに表示する 
set showcmd 
"括弧入力時の対応する括弧を表示 
set showmatch 
"検索結果文字列のハイライトを有効にする 
set hlsearch 
"ステータスラインを常に表示 
set laststatus=2 
"ステータスラインに文字コードと改行文字を表示する 
set statusline=%<%f\ %m%r%h%w%{'['.(&fenc!=''?&fenc:&enc).']['.&ff.']'}%=%l,%c%V%8P 
 
"--------------------------------------------------------------------------- 
" 印刷のフォント 
"--------------------------------------------------------------------------- 
" guifontと同じフォントで印刷 
:let &printfont=&guifont 
 
" 印刷用フォントを指定する 
":if has('win32') 
:set printfont=MS_Gothic:h12:cSHIFTJIS  " Windows 
":elseif has('mac') 
"  :set printfont=Osaka−等幅:h14          " Mac OSX 
":else 
                                          " その他の環境 
":endif

Macvim .vimrc

需要安装colorsamplepack插件

" 
" Maintainer:	Gavin Zhou <gavin.zhou@gmail.com> 
" Last change:	2010 Apr 13 
" 
" To use it, copy it to 
"     for Unix and OS/2:  ~/.vimrc 
"	      for Amiga:  s:.vimrc 
"  for MS-DOS and Win32:  $VIM\_vimrc 
"	    for OpenVMS:  sys$login:.vimrc 
 
" When started as "evim", evim.vim will already have done these settings. 
if v:progname =~? "evim" 
  finish 
endif 
 
" Use Vim settings, rather then Vi settings (much better!). 
" This must be first, because it changes other options as a side effect. 
" set encoding=utf-8 
set nocompatible 
"set guifont=Courier_New:h14:cANSI 
set guifont=Courier:h16 
"set guifontwide=STXihei:h16 
"set guifontwide=YouYuan:h16 
set nu    "show line number 
set tabstop=4 expandtab shiftwidth=4 
:colorscheme fruity 
":colorscheme guepardo 
set bsdir=buffer 
filetype on         "check file type 
set autochdir 
 
" allow backspacing over everything in insert mode 
set backspace=indent,eol,start 
 
set nobackup		" do not keep a backup file, use versions instead 
set history=1000	" keep 50 lines of command line history 
set ruler		    " show the cursor position all the time 
set showcmd		    " display incomplete commands 
set incsearch		" do incremental searching 
"set textwidth=80 
"set lines=32       " 40 line 
"set columns=80     " 120 chars 
set showmatch 
set statusline=%<%f\ %m%r%h%w%{'['.(&fenc!=''?&fenc:&enc).']['.&ff.']'}%=%l,%c%V%8P 
 
 
set nowritebackup 
set wrap 
set noerrorbells 
set novisualbell 
set t_vb= 
 
" For Win32 GUI: remove 't' flag from 'guioptions': no tearoff menu entries 
" let &guioptions = substitute(&guioptions, "t", "", "g") 
 
" Don't use Ex mode, use Q for formatting 
map Q gq 
 
" CTRL-U in insert mode deletes a lot.  Use CTRL-G u to first break undo, 
" so that you can undo CTRL-U after inserting a line break. 
inoremap <C-U> <C-G>u<C-U> 
 
" In many terminal emulators the mouse works just fine, thus enable it. 
if has('mouse') 
  set mouse=a 
endif 
 
" Switch syntax highlighting on, when the terminal has colors 
" Also switch on highlighting the last used search pattern. 
if &t_Co > 2 || has("gui_running") 
  syntax on 
  set hlsearch 
  set guioptions-=T 
endif 
 
" Only do this part when compiled with support for autocommands. 
if has("autocmd") 
 
  " Enable file type detection. 
  " Use the default filetype settings, so that mail gets 'tw' set to 72, 
  " 'cindent' is on in C files, etc. 
  " Also load indent files, to automatically do language-dependent indenting. 
  filetype plugin indent on 
 
  " Put these in an autocmd group, so that we can delete them easily. 
  augroup vimrcEx 
  au! 
 
  " For all text files set 'textwidth' to 78 characters. 
  autocmd FileType text setlocal textwidth=78 
 
  " When editing a file, always jump to the last known cursor position. 
  " Don't do it when the position is invalid or when inside an event handler 
  " (happens when dropping a file on gvim). 
  " Also don't do it when the mark is in the first line, that is the default 
  " position when opening a file. 
  autocmd BufReadPost * 
    \ if line("'\"") > 1 && line("'\"") <= line("$") | 
    \   exe "normal! g`\"" | 
    \ endif 
 
  augroup END 
 
else 
 
  set autoindent		" always set autoindenting on 
 
endif " has("autocmd") 
 
" Convenient command to see the difference between the current buffer and the 
" file it was loaded from, thus the changes you made. 
" Only define it when not defined already. 
if !exists(":DiffOrig") 
  command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis 
		  \ | wincmd p | diffthis 
endif 
 
"autocmd BufRead,BufNewFile *.rb map <F5> :% w !ruby<CR> 
"autocmd BufRead,BufNewFile *.py map <F5> :% w !python<CR>  
"autocmd FileType python setlocal omnifunc=pysmell#Complete 
nnoremap <silent> <F5> :NERDTree<CR> 
nmap <C-h> <Esc> :noh <cr> 
nmap <silent> <C-n> :bn <cr> 
nmap <silent> <C-p> :bp <cr>
/home1/yepnnet/public_html/wiki/data/pages/vi.txt · 最后更改: 2010/05/14 04:10 由 admin
到顶部
chimeric.de = chi`s home Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0