Skip to main content

Command Line and Vim

什麼是command line?

一種操作電腦的方法

一般操作電腦使用的是GUI(Graphical User Inerface),透過圖形化介面我們可以直接點選、拖曳圖示完成操作。而CLI(Command Line Interface)就是透過輸入指令(純文字)的方式來進行相同的操作。

常用command line 指令

指令全名功能描述
pwdprint working directory列出現在所在位置
lslist segment列出所在位置所有檔案加上-a 會有更多信息
cdchange directory更改位置..回上一路徑(相對路徑)
~返回家目錄(相對路徑)
/ 從根目錄出發(絕對路徑)
touch建立檔案或更新檔案時間
mvmove移動檔案、重新命名mv 欲移動檔案 位置
rmremove移除檔案若要移除資料夾可用 rmdir (空資料夾)或是 rm -r (經確認後輸入y刪除所有內容)、或是 rm -rf(直接刪除所有內容,危險!)
mkdirmake directory新增資料夾
catconcentrate列印檔案內容or合併檔案可直接呈現檔案內容
grep在檔案內搜尋關鍵字grep 關鍵字 檔案
|pipe將左邊指令輸出當成右邊指令輸入
>將左邊內容指定給右邊(會覆蓋)cat 檔案1 檔案2 > 檔案3
>>將左邊內容指定給右邊(加在最後面)echo "append in the end" > 檔案
cpcopy複製一份copy 舊檔案 新檔名
echo呈現文字
manmanual指令說明
wget下載檔案
curl送出request
clear清除終端機顯示內容
fim開啟圖片檔自己安裝的,可能有別的可以用

Vim 文字編輯器

Vim(vi Improved)是從vi衍伸出來的文書編輯器。所有的Unix Like系統都會內建vi文書編輯器,且有許多預設按鍵用得好的話可以讓你撰寫地飛快XD雖然不像其他文字編輯器可以排版e.g.word。但就像Unix的哲學一樣:只做一件事,並做好它。

好玩的教學Interactive Vim tutorial (openvim.com)

基本操作

進入文字編輯:輸入 $vim <檔案名稱>(如果不存在會新建一個)

內部操作:

  • i 為插入(insert)
  • esc 結束插入
  • :w存檔
  • :q離開
  • :wq存檔並離開
  • :q!不存檔離開

詳細操作說明

Mode模式

  1. normal (Esc):to navigate and manipulate text.
  2. insert (i):write text
  3. visual (v)

Setup設定

  • 加上(取消)行號:在normal mode中,輸入: set number(or just "nu"):set nonumber

    *change forever :在根目錄中輸入$ vim ~/.vimrc ,新增一行 set number (or "nu").

  • 設定tab產生的間隔數:在根目錄中輸入$ vim ~/.vimrc ,新增一行set tabstop=4 .

Go to anywhere

指令動作
h,l,k,j←, →, ↑, ↓
wthe start of next word.
bthe start of the word.
ethe end of the word.
0reach the beginning of the line.
shift+$reach the end of the line.
gggo to the beginning of file.
shift+gthe end of the file.
  • 可以結合數字和指令e.g.
    • 2(shift + G) : go to line 2.
    • 3w : go to the begin of the next 3 words
    • "3igo" and then press Esc : "gogogo"

Find & Jump

  • 搜尋字母

    按f加上要搜尋的字母(或符號也可),並用;及,跳往下一個或上一個目標

  • 搜尋單字

    按/並輸入要搜尋的詞,用n和N跳往下一個或上一個目標

Add new line

  • o:插入文字至下一行
  • O:插入文字至前一行

Delete

  • x:從當下游標位置開始刪除(跟按鍵delete功能一樣)
  • D 或 dd :刪除整行

Replace

  • 在當下位置按r再輸入要替換的字

Cut&Paste

  • yy:剪下
  • P:貼上

Save

  • :w ( save )
  • :q (quit )
  • :q! (quit without save)
  • :wq (save and quit)

Other

  • :w ( save )
  • :q (quit )
  • :q! (quit without save)
  • :wq (save and quit)