vi 編輯器提供簡單的字符串替換命令,在末行模式下可以使用替換命令,其命令格式如下:
[range]s/s1/s2/ [option]
[range] 表示檢索範圍,省略時表示當前行。下面是一些檢索範圍的例子。
1,10
表示從第 1 行到 10 行。%
表示整個文件,同1, $
。. ,$
從當前行到文件尾。
s 爲替換命令。
s1 要被替換的串,s2 爲替換的串。
option 表示選項:
/g
表示在全局文件中進行替換。/c
表示在每次替換之前需要用戶進行確認。省略時僅對每行第一個匹配串進行替換。
下面實例演示了字符串替換命令,將第 10~22 行的 printf 標識符替換爲 PRINT。
1) 在 vi 編輯器中編輯以下代碼:
int main(void)
{
struct stat buf; /*存儲文件狀態信息*/
if(stat("s1", &buf) == -1) {
perror ("fail to stat");
exit(1);
}
printf("permission : %d\n", buf.st_mode);
printf("inode number : %d\n", buf.st_ino);
printf("device number : %d\n", buf.st_dev);
printf("r-device number : %d\n", buf.st_rdev);
printf("link : %d\n", buf.st_nlink);
printf("uid : %d\n", buf.st_uid);
printf("gid : %d\n", buf.st_gid);
printf("file size : %d\n", buf.st_size);
printf("access time : %d\n", buf.st_atime);
printf("motify time : %d\n", buf.st_mtime);
printf("change time : %d\n", buf.st_ctime);
printf("buf size : %d\n", buf.st_blksize);
printf("block size : %d\n", buf.st_blocks);
return 0;
}
2) 在末行模式下輸入以下替換命令:
10,22s/printf/PRINT/g
屏幕顯示如下:
int main(void){ struct stat buf; /*存儲文件狀態信息*/ if(stat("s1", &buf) == -1) { perror ("fail to stat"); exit(1); } PRINTF("permission : %d\n", buf.st_mode); PRINTF("inode number : %d\n", buf.st_ino); PRINTF("device number : %d\n", buf.st_dev); PRINTF("r-device number : %d\n", buf.st_rdev); PRINTF("link : %d\n", buf.st_nlink); PRINTF("uid : %d\n", buf.st_uid); PRINTF("gid : %d\n", buf.st_gid); PRINTF("file size : %d\n", buf.st_size); PRINTF("access time : %d\n", buf.st_atime); PRINTF("motify time : %d\n", buf.st_mtime); PRINTF("change time : %d\n", buf.st_ctime); PRINTF("buf size : %d\n", buf.st_blksize); PRINTF("block size : %d\n", buf.st_blocks); return 0;}
vi 編輯器在執行替換命令指令時,可以進行簡單的模式匹配,其匹配模式如下:
\<word
替換以 word 開始的單詞的 word 部分。例如:
%s/\<abc/cde
表示將以 abc 開頭的單詞替換爲以 cde 開頭。
下例演示了這種替換操作,更改 stat 結構的變量名,將整個文件中的 buf 替換爲 stat_buf。
1) 在 vi 編輯器中編輯以下代碼:
int main(void){ struct stat buf; /*存儲文件狀態信息*/ if(stat("s1", &buf) == -1) { perror ("fail to stat"); exit(1); } printf("permission : %d\n", buf.st_mode); printf("inode number : %d\n", buf.st_ino); printf("device number : %d\n", buf.st_dev); printf("r-device number : %d\n", buf.st_rdev); printf("link : %d\n", buf.st_nlink); printf("uid : %d\n", buf.st_uid); printf("gid : %d\n", buf.st_gid); printf("file size : %d\n", buf.st_size); printf("access time : %d\n", buf.st_atime); printf("motify time : %d\n", buf.st_mtime); printf("change time : %d\n", buf.st_ctime); printf("buf size : %d\n", buf.st_blksize); printf("block size : %d\n", buf.st_blocks); return 0;}
2) 在末行模式下輸入命令:
%s/\<buf/stat_buf/g
屏幕顯示如下:
int main(void){ struct stat stat_buf; /*存儲文件狀態信息*/ if(stat("s1", &stat_buf) == -1) { perror ("fail to stat"); exit(1); } printf("permission : %d\n", stat_buf.st_mode); printf("inode number : %d\n", stat_buf.st_ino); printf("device number : %d\n", stat_buf.st_dev); printf("r-device number : %d\n", stat_buf.st_rdev); printf("link : %d\n", stat_buf.st_nlink); printf("uid : %d\n", stat_buf.st_uid); printf("gid : %d\n", stat_buf.st_gid); printf("file size : %d\n", stat_buf.st_size); printf("access time : %d\n", stat_buf.st_atime); printf("motify time : %d\n", stat_buf.st_mtime); printf("change time : %d\n", stat_buf.st_ctime); printf("stat_buf size : %d\n", stat_buf.st_blksize); printf("block size : %d\n", stat_buf.st_blocks); return 0;}