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

2014年12月19日 星期五

Vi Cheat Sheet

雖然現在都是用vim 比較多,但AIX 上面還是只能用vi,還是記一下操作的設定好了
以下資料摘自於http://www.lagmonster.org/docs/vi.html

Modes
Vi has two modes insertion mode and command mode. The editor begins in command mode, where the cursor movement and text deletion and pasting occur. Insertion mode begins upon entering an insertion or change command. [ESC] returns the editor to command mode (where you can quit, for example by typing :q!). Most commands execute as soon as you type them except for "colon" commands which execute when you press the ruturn key.



Quitting
:x Exit, saving changes
:q Exit as long as there have been no changes
ZZ Exit and save changes if any have been made
:q! Exit and ignore any changes



Inserting Text
i Insert before cursor
I Insert before line
a Append after cursor
A Append after line
o Open a new line after current line
O Open a new line before current line
r Replace one character
R Replace many characters



Motion
h Move left
j Move down
k Move up
l Move right
w Move to next word
W Move to next blank delimited word
b Move to the beginning of the word
B Move to the beginning of blank delimted word
e Move to the end of the word
E Move to the end of Blank delimited word
( Move a sentence back
) Move a sentence forward
{ Move a paragraph back
} Move a paragraph forward
0 Move to the begining of the line
$ Move to the end of the line
1G Move to the first line of the file
G Move to the last line of the file
nG Move to nth line of the file
:n Move to nth line of the file
fc Move forward to c
Fc Move back to c
H Move to top of screen
M Move to middle of screen
L Move to botton of screen
% Move to associated ( ), { }, [ ]



Deleting Text
Almost all deletion commands are performed by typing d followed by a motion. For example, dw deletes a word. A few other deletes are:
x Delete character to the right of cursor
X Delete character to the left of cursor
D Delete to the end of the line
dd Delete current line
:d Delete current line



Yanking Text
Like deletion, almost all yank commands are performed by typing y followed by a motion. For example, y$ yanks to the end of the line. Two other yank commands are:
yy Yank the current line
:y Yank the current line



Changing text
The change command is a deletion command that leaves the editor in insert mode. It is performed by typing c followed by a motion. For wxample cw changes a word. A few other change commands are:
C Change to the end of the line
cc Change the whole line



Putting text
p Put after the position or after the line
P Put before the poition or before the line



Buffers
Named buffers may be specified before any deletion, change, yank or put command. The general prefix has the form "c where c is any lowercase character. for example, "adw deletes a word into buffer a. It may thereafter be put back into text with an appropriate "ap.



Markers
Named markers may be set on any line in a file. Any lower case letter may be a marker name. Markers may also be used as limits for ranges.
mc Set marker c on this line
`c Go to beginning of marker c line.
'c Go to first non-blank character of marker c line.



Search for strings
/string Search forward for string
?string Search back for string
n Search for next instance of string
N Search for previous instance of string



Replace
The search and replace function is accomplished with the :s command. It is commonly used in combination with ranges or the :g command (below).
:s/pattern/string/flags Replace pattern with string according to flags.
g Flag - Replace all occurences of pattern
c Flag - Confirm replaces.
& Repeat last :s command



Regular Expressions
. (dot) Any single character except newline
* zero or more occurances of any character
[...] Any single character specified in the set
[^...] Any single character not specified in the set
^ Anchor - beginning of the line
$ Anchor - end of line
\< Anchor - begining of word
\> Anchor - end of word
\(...\) Grouping - usually used to group conditions
\n Contents of nth grouping

[...] - Set Examples
[A-Z] The SET from Capital A to Capital Z
[a-z] The SET from lowercase a to lowercase z
[0-9] The SET from 0 to 9 (All numerals)
[./=+] The SET containing . (dot), / (slash), =, and +
[-A-F] The SET from Capital A to Capital F and the dash (dashes must be specified first)
[0-9 A-Z] The SET containing all capital letters and digits and a space
[A-Z][a-zA-Z] In the first position, the SET from Capital A to Capital Z
In the second character position, the SET containing all letters

Regular Expression Examples
/Hello/ Matches if the line contains the value Hello
/^TEST$/ Matches if the line contains TEST by itself
/^[a-zA-Z]/ Matches if the line starts with any letter
/^[a-z].*/ Matches if the first character of the line is a-z and there is at least one more of any character following it
/2134$/ Matches if line ends with 2134
/\(21|35\)/ Matches is the line contains 21 or 35
Note the use of ( ) with the pipe symbol to specify the 'or' condition
/[0-9]*/ Matches if there are zero or more numbers in the line
/^[^#]/ Matches if the first character is not a # in the line
Notes:
1. Regular expressions are case sensitive
2. Regular expressions are to be used where pattern is specified


Counts
Nearly every command may be preceded by a number that specifies how many times it is to be performed. For example, 5dw will delete 5 words and 3fe will move the cursor forward to the 3rd occurence of the letter e. Even insertions may be repeated conveniently with thismethod, say to insert the same line 100 times.



Ranges
Ranges may precede most "colon" commands and cause them to be executed on a line or lines. For example :3,7d would delete lines 3-7. Ranges are commonly combined with the :s command to perform a replacement on several lines, as with :.,$s/pattern/string/g to make a replacement from the current line to the end of the file.
:n,m Range - Lines n-m
:. Range - Current line
:$ Range - Last line
:'c Range - Marker c
:% Range - All lines in file
:g/pattern/ Range - All lines that contain pattern



Files
:w file Write to file
:r file Read file in after line
:n Go to next file
:p Go to previos file
:e file Edit file
!!program Replace line with output from program



Other
~ Toggle upp and lower case
J Join lines
. Repeat last text-changing command
u Undo last change
U Undo all changes to line

2013年3月23日 星期六

vi 與 vim 的指令整理

vi 是 unix 家族下最功能強大的文字編輯器,讓用戶只要使用一個鍵盤就可以完成所有的編輯。而 vim 則是 vi 的加強版,甚至在 Windows 上也找得到 vim 的芳蹤。但 vi/vim 眾多的指令卻經常令初學者卻步,以下就是我所整理出來那些令人卻步的指令:

編輯模式

指令說明
*i在游標位置進入編輯模式
I在游標行的第一個非空白字元進入編輯模式
*a在游標位置後進入編輯模式
*A在游標行的最後一個字元進入編輯模式
*o向下新增一行,並進入編輯模式
O向上新增一行,並進入編輯模式
cc刪除游標行,並進入編輯模式
*[ESC]取消指令或退出編輯模式
游標移動
指令說明
*gg移到第一行
*G移到最後一行
*行數 → G移動到第 n 行
0移動到該行最前面
$移動到該行最後面
字數 → [Space]向右移動 n 個字元
*行數 → [Enter]向下移動 n 行

標記與複製

指令說明
*v開始字串標記
*V開始行標記
*v → [Ctrl]-V開始區塊標記
*d刪除標記的內容
*y複製標記的內容
*yy複製游標行
yG複製游標行到最後一行
y1G複製游標行到第一行
y$複製游標處到最後一個字元
y0複製游標處到第一個字元
*p在下一行貼上複製或刪除的內容
P在上一行貼上複製或刪除的內容
*[Ctrl]-R → 0在下一行貼上複製或刪除的內容,適用於編輯模式及指令行

搜尋與取代

指令說明
*/搜尋字串向下搜尋字串
**將游標移到字串上,直接按 “*” 也可以做向下搜尋
?搜尋字串向上搜尋字串
*:set ic搜尋時不分大小寫
*:set noic搜尋時要分大小寫
*n繼續下一個搜尋結果
*N繼續上一個搜尋結果
*:起始行,終止行s/搜尋字串/取代字串/gic從第 n 行到第 n 行取代字串 (後面的 g: 整行全部, i: 不分大小寫,c: 詢問)
*:1,$s/搜尋字串/取代字串/gic全部取代字串 (後面的 g: 整行全部, i: 不分大小寫,
c: 詢問)

刪除

指令說明
*dd刪除游標行
*dG刪除游標行到最後一行
d1G刪除游標行到第一行
*d$刪除游標處到最後一個字元
d0刪除游標處到第一個字元

檔案功能

指令說明
*:w存檔 (加 ! 表示強制存檔)
*:w 檔案名稱另存新檔
*:wq存檔並退出 vi
:e 檔案名稱編輯其它檔案
*:e!還原至檔案編修前的狀態
:r 檔案名稱讀入檔案內容,並加到游標行的後面
*:n切換到下一個開啟的檔案
*:N切換到上一個開啟的檔案
*:set nu顯示行號
*:set nonu取消行號顯示
*:files列出所有開啟的檔案

視窗分割

指令說明
*:new新增水平視窗
*:new 檔案名稱新增水平視窗,並在新增的視窗載入檔案
*:vnew新增垂直視窗
*:vnew 檔案名稱新增垂直視窗,並在新增的視窗載入檔案
*[Ctrl]-W → [方向鍵]切換視窗
*:only僅保留目前的視窗

其它

*指令說明
*J將游標行與下一行合併
*u還原指令
*[Ctrl]-R重做指令
*.重覆上一個指令
命令執行 linux 指令,並顯示執行結果
*:q退出 vi (加 ! 表示強制退出)

vim 的設定檔

通常我會編輯 /etc/vimrc,在檔案最後加入:
" 顯示列號
set number
" 語法高亮度顯示
syntax on
" 標記搜尋到的字串
set hlsearch
" 自動縮排
set autoindent
" 顯示說明
set ruler
" 顯示編輯狀態
set showmode
" 設定註解的顏色
highlight Comment ctermfg=cyan
" 設定搜尋到的字串顏色
highlight Search term=reverse ctermbg=4 ctermfg=7
" 設定 tab 鍵的字元數
set tabstop=4