การ install plugin ให้กับ vim ด้วย Neobundle

คราวที่แล้ว เราลองหัดใช้ vim ให้สามารถพิมพ์ข้อความลงหรือโค้ดลงไปได้เท่านั้น แต่ว่ามันยังดูเรียบๆ ไม่น่าใช้เท่าไร

เราจึงต้อง install plugin เพิ่มเข้าไปให้ vim มีลูกเล่นมากขึ้น เช่น สีสันของ syntax แต่ละภาษาโปรแกรม, autocomplete, การค้นหา เป็นต้น

Neobundle เป็นตัวช่วยในการติดตั้ง plugin ให้กับ vim ตัวหนึ่งที่ง่าย เพียงแค่เราพิมพ์ NeoBundle plugin_name ลงในไฟล์ .vimrc แล้ว restart vim ขึ้นมาใหม่ก็จะ install plugin ให้เรียบร้อย

โปรแกรมที่จำเป็นสำหรับการ install คือ curl และgit ถ้าไม่มีก็ต้อง install curl และ git ก่อน โดย run คำสั่ง (สำหรับ ubuntu)

$ sudo apt-get install curl
$ sudo apt-get install git

การ install Neobundle สำหรับ linux และ osx เริ่มจาก run คำสั่ง

$ curl https://raw.githubusercontent.com/Shougo/neobundle.vim/master/bin/install.sh > install.sh
$ sh ./install.sh

จากนั้นก็สร้างไฟล์ .vimrc โดย run คำสั่งต่อไปนี้ที่ home path ของตนเอง

$ vi .vimrc

แล้วเข้าสู่ insert mode ด้วยการกด i จากนั้นก็ copy & paste ตัวอย่างของไฟล์ .vimrc ด้านล่างนี้ลงไปใน .vimrc

" Note: Skip initialization for vim-tiny or vim-small.
if 0 | endif
if has('vim_starting')
if &compatible
set nocompatible "Be iMproved
endif
" Required:
set runtimepath+=~/.vim/bundle/neobundle.vim/
endif
" Required:
call neobundle#begin(expand('~/.vim/bundle/'))
" Let NeoBundle manage NeoBundle
"Required:
NeoBundleFetch 'Shougo/neobundle.vim'
"****************************************************************"
"" Neobudle install packages
"****************************************************************"
"" Color
NeoBundle 'tomasr/molokai'
call neobundle#end()
" Required:
filetype plugin indent on
" If there are uninstalled bundles found on startup,
"this will conveniently prompt you to install them.
NeoBundleCheck

จากตัวอย่างด้านบนจะเห็นว่า ผมได้ลง plugin ชื่อ tomasr/molokai ซึ่งเป็น plugin ที่เพิ่มสีสันในการเขียนโค้ดนั่นเอง

รูปที่ 1 - หน้าต่างยืนยันการ install plugin

เมื่อปิดไฟล์ .vimrc แล้วเปิดขึ้นมาใหม่ มันจะให้เรายืนยันว่าจะ install plugin นี้นะ เราก็ตอบ y เพื่อ install plugin นี้

รูปที่ 2 - หน้าต่างแสดง script ในไฟล์ .vimrc

ในรูปที่ 2 นี้ จะเห็นว่ามีสีสันตั้งแต่ตอนเรา copy & paste ในตัวอย่างด้านบนแล้ว แต่ทำไมหลัง install plugin แล้วสียังไม่เปลี่ยนเป็นแบบ molokai นั่นก็เพราะว่า เรายังไม่ได้ตั้งค่าให้มันนั่นเอง เราเพิ่ม script ไปอีก ดังนี้

if !exists('g:not_finsh_neobundle') 
colorscheme molokai
endif
set t_Co=256
รูปที่ 3 - ไฟล์ .vimrc ได้ถูกตั้งค่าสีแบบ molokai

ลองปิดแล้วเปิดไฟล์ .vimrc อีกครั้ง จะเห็นว่าได้สีสันที่สวยงามมากยิ่งขึ้น ดังรูปที่ 3

เพียงเท่านี้เราก็สามารถลง plugin ตัวอื่นๆ ได้ง่ายขึ้น

Reference

https://github.com/Shougo/neobundle.vim

https://github.com/ak1103dev/MyVim

--

--