4/4 WSL、Vim

今日要來學習 WSL 和一些基本 Bash 指令以及 Vim 文字編輯器。



一、WSL 簡介:

Windows Subsystem for Linux(WSL)是由 Microsoft 開發的功能,旨在讓開發人員能夠在 Windows 系統上運行 Linux 環境,而無需安裝虛擬機或者將電腦灌成雙系統。
WSL 的出現為同時使用 Windows 和 Linux 的開發人員提供順暢且具生產力的體驗,同時也為 Linux 初學者提供了一個輕鬆入門的機會。
更多 WSL 資訊可以參考 Microsoft Learn

二、WSL 安裝:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
快速安裝

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
注意事項
必須擁有 Windows 10 版本 2004 和更新版本或 Windows 11,才能使用此安裝方式。

  1. 搜尋「PowerShell」然後選取「以系統管理員身份執行」開啟。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  2. 輸入下方指令安裝 WSL,預設安裝 Ubuntu。

    ​​​​wsl --install
    

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  3. 將電腦重新啟動。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  4. 開啟 Ubuntu 後需要等待一段時間設定,設定完畢後,輸入使用者名稱和密碼,使用者名稱只能小寫。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
普通安裝

  1. 搜尋並開啟「開啟或關閉 Windows 功能」。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  2. 勾選「Windows 子系統 Linux 版」和「虛擬機器平台」選項然後按下確定。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  3. 按下「立即重新啟動」。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  4. 開啟 Microsoft Store 搜尋 Linux,這裡選擇 Ubuntu。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  5. 開啟 Ubuntu 後需要等待一段時間設定,設定完畢後,輸入使用者名稱和密碼,使用者名稱只能小寫。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

三、Bash 操作:

特殊字符

  • / 根目錄,Linux 所有文件和目錄的起始點。
  • ~ 家目錄,Linux 中的每個用戶都有一個家目錄,路徑 /home/username
  • . 當前目錄,比如在 /home/user 路徑中代表 /home/user
  • .. 父目錄,比如在 /home/user 路徑中代表 /home
  • sudo 臨時獲取 root 權限執行指令,需要使用者輸入密碼,比如 sudo apt-get update

常用指令

  • cd 切換目錄
    ​​​​cd / # 切換根目錄 ​​​​cd .. # 切換父目錄 ​​​​cd /home/user # 切換家目錄
  • ls 顯示目錄中的檔案
    ​​​​ls # 顯示當前目錄中的檔案 ​​​​ls -a # 顯示所有檔案,包括隱藏檔案 ​​​​ls /home/user # 顯示家目錄中的檔案
  • touch 創建檔案
    ​​​​touch main.txt # 創建 main.txt
  • mkdir 創建目錄
    ​​​​mkdir code # 創建 code 目錄
  • rm 刪除檔案/目錄
    ​​​​rm main.txt # 刪除 main.txt ​​​​rm -r code # 刪除 code 目錄,僅限空的目錄 ​​​​rm -rf code # 強制刪除整個 code 目錄
  • mv 移動檔案/目錄
    ​​​​mv main.txt run.txt # 重新命名 main.txt 為 run.txt ​​​​mv main.txt code/ # 移動 main.txt 到 code 目錄 ​​​​mv code/ app/ # 如果 app 目錄已經存在則移動 code 目錄,否則重新命名 code 目錄
  • cp 複製檔案/目錄
    ​​​​cp main.txt run.txt # 複製 main.txt 並命名為 run.txt ​​​​cp main.txt code/run.txt # 複製 main.txt 到 code 目錄並命名為 run.txt ​​​​cp -r code/ app/ # 複製 code 目錄中的所有檔案到 app 目錄
  • cat 顯示檔案內容
    ​​​​cat main.txt # 顯示 main.txt 內容
  • pwd 顯示當前路徑

四、Vim 教學:

Vim 是一個強大的文字編輯器,廣泛用於 Unix 和類 Unix 系統中。Vim 是 Vi 編輯器的改進版本,全名「Vi IMproved」。
Vim 具有學習曲線,初學者需要花費一段時間來熟悉操作方式和鍵盤快捷鍵。然而,一旦熟悉 Vim,它會成為一個非常強大和高效的文字編輯器,能夠極大地提高工作效率。

安裝指令

sudo apt-get install vim

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

基本概念

Vim 總共有三種模式,分別是命令模式(Command mode)、插入模式(Insert mode)、底線命令模式(Last line mode),剛啟動 Vim 會是命令模式。

  • 命令模式
    此模式下的鍵盤操作為輸入命令,任何模式按 Esc 都會回到此模式,不知道自己目前身處哪個模式的話,一直按 Esc 就對了。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • 插入模式
    此模式下的鍵盤操作為輸入字符,命令模式按下 i 進入此模式。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • 底線命令模式
    此模式下的鍵盤操作為輸入命令,命令模式下按 : 進入此模式,指令顯示在左下方最後一行。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

基本操作

想知道更多 Vim 操作指令可以點此或者上網查詢。

  • 開啟/創建檔案
    如果檔名沒有找到可以開啟則會創建新的。

    ​​​​vim main.txt
    
  • 儲存檔案
    底線命令模式輸入下方指令,即可儲存檔案。

    ​​​​:w
    
  • 離開 Vim
    底線命令模式輸入下方指令,即可離開 Vim。

    ​​​​:q
    

補充、Windows Terminal 安裝:

推薦安裝 Windows Terminal,不僅文字有顏色更好看,而且能夠同時開啟多個視窗以及各種 Shell,只要到 Microsoft Store 搜尋就能安裝。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

image