Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม Bash

Powerline: ปรับปรุง Vim และ Shell ด้วย Custom Status Lines & Prompts

Powerline เป็น Statusline Plugin ที่ยอดเยี่ยมสำห Vim Editor พัฒนาด้วย Python และให้ Status Lines และ Prompts สำห Applications หลายอย่าง เช่น bash, zsh, tmux และอื่นๆ อีกมาก

มันเพิ่ม Appearance และ Functionality ของ Vim Editor โดยให้ Customizable และ Informative Status Line ที่ด้านล่างของ Editor Window รวมถึง Current Mode (เช่น Normal, Insert, Visual), File Name, Path, Line และ Column Numbers, Git Branch, Virtual Environment และอื่นๆ

Powerline: ปรับปรุง Vim และ Shell ด้วย Custom Status Lines & Prompts

คุณสมบัติ

  • เขียนด้วย Python ทำให้ Extensible และ Feature-rich
  • Code Base ที่ Stable และ Testable ทำงานดีกับ Python 2.6+ และ Python 3
  • รองรับ Prompts และ Statuslines ใน Linux Utilities และ Tools หลายตัว
  • มี Configurations และ Decorator Colors พัฒนาด้วย JSON
  • Fast และ Lightweight พร้อม Daemon Support ให้ Performance ที่ดีกว่าอีก

ในบทความนี้ ผมจะแสดงวิธีติดตั้ง Powerline และ Powerline Fonts และวิธีใช้ Bash และ Vim ภายใต้ RHEL-based และ Debian-based Distributions

วิธีติดตั้ง Powerline ใน Linux

เนื่องจาก Naming Conflict กับ Projects อื่นที่ไม่เกี่ยวข้อง Powerline Program พร้อมใช้บน PyPI (Python Package Index) ภายใต้ Package Name powerline-status

เพื่อติดตั้ง Packages จาก PyPI เราต้องการ Pip Package Management Tool สำหติดตั้ง Python Packages ดังนั้น ให้ติดตั้ง pip Tool ภายใต้ Linux Systems ก่อน

$ sudo apt install python3-pip [On Debian, Ubuntu and Mint]
$ sudo yum install python3-pip [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
$ sudo emerge -a dev-lang/pip [On Gentoo Linux]
$ sudo apk add py3-pip [On Alpine Linux]
$ sudo pacman -S python-pip [On Arch Linux]
$ sudo zypper install python3-pip [On OpenSUSE]

ตอนนี้ถึงเวลาติดตั้ง Powerline Latest Development Version จาก Git Repository สำหนี้ System ของคุณต้องมี Git Package ติดตั้งเพื่อ Fetch Packages จาก Git Repository

$ sudo apt install git [On Debian, Ubuntu and Mint]
$ sudo yum install git [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
$ sudo emerge -a git [On Gentoo Linux]
$ sudo apk add git [On Alpine Linux]
$ sudo pacman -S git [On Arch Linux]
$ sudo zypper install git [On OpenSUSE]

ต่อไป คุณสามารถติดตั้ง Powerline ด้วย pip Command ดังนี้

$ pip install powerline-status

หากได้ Error Message "externally-managed-environment" ซึ่งมักเกิดในบริบทของ Python Virtual Environments เมื่อใช้ Powerline หรือ Tools ที่เกี่ยวข้อง

เพื่อ Fix Error นี้ ให้ลบไฟล์ต่อไปนี้และรัน pip Install Command อีกครั้ง

$ sudo rm -rf /usr/lib/python3.x/EXTERNALLY-MANAGED

ทางเลือกอื่น คุณสามารถติดตั้ง Powerline โดยรัน Command เฉพาะ Package Manager ของคุณ ดังนี้

$ sudo apt install powerline [On Debian, Ubuntu and Mint]
$ sudo yum install powerline [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
$ sudo emerge -a sys-apps/powerline [On Gentoo Linux]
$ sudo apk add powerline [On Alpine Linux]
$ sudo pacman -S powerline [On Arch Linux]
$ sudo zypper install powerline [On OpenSUSE]

วิธีติดตั้ง Powerline Fonts ใน Linux

Powerline ใช้ Glyphs พิเศษเพื่อแสดง Special Arrow Effects และ Symbols สำห Developers สำหนี้ คุณต้องมี Symbol Font หรือ Patched Font ติดตั้งบน Systems

หากคุณติดตั้ง Powerline ผ่าน PIP คุณสามารถติดตั้ง Symbol Font และ Fontconfig File เวอร์ชันล่าสุดได้ดังนี้

$ wget https://github.com/powerline/powerline/raw/develop/font/PowerlineSymbols.otf
$ wget https://github.com/powerline/powerline/raw/develop/font/10-powerline-symbols.conf
$ sudo mv PowerlineSymbols.otf ~/.local/share/fonts/
$ sudo fc-cache -vf ~/.local/share/fonts/
$ sudo mv 10-powerline-symbols.conf ~/.config/fontconfig/conf.d/

หากคุณติดตั้ง Powerline ผ่าน Package Manager คุณสามารถติดตั้ง Latest Fonts ได้โดยรัน:

$ sudo apt install fonts-powerline [On Debian, Ubuntu and Mint]
$ sudo yum install powerline-fonts [On RHEL/CentOS/Fedora และ Rocky/AlmaLinux]
$ sudo emerge -a sys-apps/powerline-fonts [On Gentoo Linux]
$ sudo apk add powerline-fonts [On Alpine Linux]
$ sudo pacman -S powerline-fonts [On Arch Linux]
$ sudo zypper install powerline-fonts [On OpenSUSE]

เปิดใช้งาน Bash Color Prompts ด้วย Powerline

หากคุณติดตั้ง Powerline ผ่าน PIP คุณสามารถ Enable ได้โดย bash Shell และ vim Editor โดยเพิ่มบรรทัดต่อไปนี้ในไฟล์ ~/.bashrc

export TERM="screen-256color"

เพื่อ Enable Powerline ใน Bash Shell โดย Default คุณต้องเพิ่ม Snippet ต่อไปนี้ใน ~.bashrc

ก่อนอื่น หา Location ของ powerline ที่ติดตั้งด้วยคำสั่ง:

$ pip show powerline-status
Name: powerline-status
Version: 2.7
Summary: The ultimate statusline/prompt utility.
Home-page: https://github.com/powerline/powerline
Author: Kim Silkebaekken
Author-email: [email protected]
License: MIT
Location: /home/tecmint/.local/lib/python3.11/site-packages

เมื่อรู้ Actual Location ของ powerline แล้ว ต้องแทนที่ Location ในบรรทัดด้านล่างตามที่ System แนะนำ

powerline-daemon -q
POWERLINE_BASH_CONTINUATION=1
POWERLINE_BASH_SELECT=1
. /home/tecmint/.local/lib/python3.11/site-packages/powerline/bindings/bash/powerline.sh

หากคุณติดตั้ง Powerline ผ่าน Package Manager คุณสามารถ Enable ได้โดยรัน:

$ source /usr/share/powerline/bindings/bash/powerline.sh

ตอนนี้ลอง Log Out และ Log In กลับมา คุณจะเห็น Powerline Statusline ตามภาพด้านล่าง

Powerline: ปรับปรุง Vim และ Shell ด้วย Custom Status Lines & Prompts

ลองเปลี่ยนหรือ Switch ไป Directory ต่างๆ และจ้องดู " breadcrumb" Prompt เปลี่ยนเพื่อแสดง Current Location

คุณยังจะเห็น Pending Background Jobs และ หาก Powerline ติดตั้งบน Remote Linux Machine คุณจะสังเกตว่า Prompt เพิ่ม Hostname เมื่อ Connect ผ่าน SSH

เปิดใช้งาน Vim Color Prompts ด้วย Powerline

หาก Vim เป็น Editor โปรดของคุณ โชคดีที่มี Plugin ทรงพลังสำห Vim เช่นกัน เพื่อ Enable Plugin นี้ ให้เพิ่มบรรทัดเหล่านี้ใน ~/.vimrc

python3 from powerline.vim import setup as powerline_setup
python3 powerline_setup()
python3 del powerline_setup
set laststatus=2

ตอนนี้คุณสามารถ Launch Vim และเห็น Status Line ใหม่ที่สวยงาม:

Powerline: ปรับปรุง Vim และ Shell ด้วย Custom Status Lines & Prompts

สรุป

Powerline ช่วยตั้งค่า Statuslines และ Prompts ที่สีสันและสวยงามในหลาย Applications ดีสำห Coding Environments หวังว่าคู่มือนี้จะช่วยได้ และจำไว้ว่า Post Comment หากต้องการความช่วยเหลือหรือมี Ideas เพิ่มเติม