Try   HackMD

終端機和 Vim 設定

教學影片: 快快樂樂學 Vim

自訂終端機命令列

每次打開終端機覺得命令列過長? 或者是顏色單調?
其實這些都是可以調整的!
我們可以到 ~/.bashrc 下設定 PS1 這個變數 (Ubuntu 在第 61 或 62 行)

  • 自訂命令列顯示字串

    • \u: 使用者帳號名稱
    • \h: 主機名稱
    • \w: 目前位置(完整路徑)
    • \W: 目前位置(只有目前目錄名稱)
    • [\t]: 顯示時間 (hh:mm:ss), 更多變化請見參考連結
  • 文字顏色
    文字的色彩是靠 ANSI escape code 來指定的
    \e[0;34m: 顏色指定開始控制碼
    \e[0m: 結束

    • 01: 0 是正常亮度,1 是高亮度
    • 3037: 30 + x 的值為前景色
    • 4047: 40 + x 的值為背景色
      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 →

記得重新登入或重開機,以載入上述設定,或執行 source 命令

$ source ~/.bashrc

改完後不但有美美的顏色,也不會顯示一長串的路徑。
要完整路徑就下 $pwd 吧 =w=

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

$ sudo apt-get install vim

幾乎是瞬間 Vim 就裝好啦!

Vim 設定

請在家目錄下,下命令 $ vim .vimrc

所有 Vim 的相關設定都在此更改

設定選項

以下列出部分設定,強大的 Vim 還有更多功能可供使用者自行訂定

  • set ai:
    • 自動縮排,啟用自動縮排以後,在貼上剪貼簿的資料時排版可能會亂掉,這時可以手動切換至貼上模式 :set paste 再進行貼上
  • syntax on: 開啟上色模式
  • set background=dark: 啟用暗色背景模式,字體色調變亮
  • set cursorline: 啟用行游標提示
  • set enc=utf8: 文字編碼加入 utf8
  • set hls: 螢光筆標記關鍵字
  • set number: 顯示行號
  • map <F4> : set nu!<BAR>set nonu?<CR>: 按下 F4 可以自由開關行號,複製程式碼時超方便!
  • set relativenumber: 顯示相對行號
  • set ic: 搜尋不分大小寫
  • set expandtab: 使用空白取代 Tab
  • set tabstop=4: 控制 Tab 時所需要的空白鍵 (Tab) 字元數,這個例子是用 4 個空白鍵取代 Tab
  • set shiftwidth=2: 自訂縮排所需的空白鍵位元數

參考資訊

Vim Plugin

網路上有許多工具可以讓工作更有效率!

要裝什麼樣的 plugins 就看使用者自己,請參考連結:

.vimrc 範例

" Specify a directory for plugins (for Neovim: ~/.local/share/nvim/plugged)                          
call plug#begin('~/.vim/plugged')

Plug 'Shougo/neocomplcache'
Plug 'scrooloose/nerdtree'
map <F5> :NERDTreeToggle<CR>

call plug#end()

let g:neocomplcache_enable_at_startup = 1 
let g:neocomplcache_enable_smart_case = 1 
inoremap <expr><TAB> pumvisible()?"\<C-n>" : "\<TAB>"

syntax on
set number
set cursorline
colorscheme default
set bg=dark
set tabstop=4
set expandtab
set shiftwidth=4
set ai
set hlsearch
set smartindent
map <F4> : set nu!<BAR>set nonu?<CR>

" autocomplete dropdown list colorscheme
hi Pmenu ctermfg=0 ctermbg=7 
hi PmenuSel ctermfg=7 ctermbg=4

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 →

管理多終端機視窗工具: byobu+ NERDTree

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 →

安裝: $ sudo apt-get install byobu

byobu

在終端機輸入 byobu,以下是快捷鍵:

  • F2 : 新增終端機分頁
  • F3,F4 : 在終端機之間切換
  • ctrl+F6 : 刪除終端機
  • shift+F2 : 新增水平終端機
  • ctrl+F2 : 新增垂直終端機
  • shift+方向鍵 : 切換