Me Myself and My way

Posts tagged ‘vimrc’

การปรับแต่ง vim ใน Ubuntu 7.10

เนื่องจากใน Ubuntu 7.10 vim ได้ปิดการใช้งาน syntax hilight และ auto indent ไว้ทำให้เวลาเขียน shell script ด้วย vim ใน ubuntu จะไม่มีการ hilight สีและ tab ให้(แบบในห้อง lab 303) เราจึงต้องมีการแก้อะไรสักหน่อยเพื่อให้มันใช้งานได้

ในขั้นแรกเราก็ต้อง install vim version ที่ใหม่กว่าที่มากับ ubuntu ซะก่อนด้วยคำสั่ง

  1. sudo apt-get install vim

หรือจะใช้

  1. sudo apt-get install gvim

ก็ได้ ตัวหลังจะเป็น vim ในแบบ application ที่ใช้ gui ของ GNOME แต่การ install จะลง library ของ vim ที่ต้องใช้ทั้งหมดเหมือนกันพอลงเสร็จแล้วก็เข้าไปแก้ไขใน file /etc/vim/vimrc โดยจะต้องมี permission เป็น su ด้วยถึงจะแก้ไขไฟล์นี้ได้ ใน ubuntu ใช้คำสั่ง

  1. sudo vim /etc/vim/vimrc

หาบรรทัดที่เขียนว่า

  1. ” Vim5 and later versions support syntax highlighting. Uncommenting the next
  2. ” line enables syntax highlighting by default.
  3. ” syntax on

เอา comment (“) ของบรรทัดสุดท้าย (syntax on) ออก ให้เป็นแบบนี้

  1. ” Vim5 and later versions support syntax highlighting. Uncommenting the next
  2. ” line enables syntax highlighting by default.
  3. syntax on

จะเป็นการเปิดใช้งาน syntax hilight ของ vimจากนั้นหาบรรทัด

  1. ” Uncomment the following to have Vim load indentation rules according to the
  2. ” detected filetype. Per default Debian Vim only load filetype specific
  3. ” plugins.
  4. ” if has(“autocmd”)
  5. ”  filetype indent on
  6. ” endif

เอา comment ตั้งแต่ if จนถึง endif ออกจะเป็นการเปิดใช้งาน autoindent ของ vimตัวอย่างไฟล์ /etc/vim/vimrc ที่ใช้งานได้.. (ถ้าไม่คิดอะไรมากก็ก๊อปไปแปะทับได้เลย)

  1. ” All system-wide defaults are set in $VIMRUNTIME/debian.vim (usually just
  2. ” /usr/share/vim/vimcurrent/debian.vim) and sourced by the call to :runtime
  3. ” you can find below.  If you wish to change any of those settings, you should
  4. ” do it in this file (/etc/vim/vimrc), since debian.vim will be overwritten
  5. ” everytime an upgrade of the vim packages is performed.  It is recommended to
  6. ” make changes after sourcing debian.vim since it alters the value of the
  7. ” ‘compatible’ option.
  8. ” This line should not be removed as it ensures that various options are
  9. ” properly set to work with the Vim-related packages available in Debian.
  10. runtime! debian.vim
  11. ” Uncomment the next line to make Vim more Vi-compatible
  12. ” NOTE: debian.vim sets ‘nocompatible’.  Setting ‘compatible’ changes numerous
  13. ” options, so any other options should be set AFTER setting ‘compatible’.
  14. “set compatible
  15. ” Vim5 and later versions support syntax highlighting. Uncommenting the next
  16. ” line enables syntax highlighting by default.
  17. syntax on
  18. ” If using a dark background within the editing area and syntax highlighting
  19. ” turn on this option as well
  20. “set background=dark
  21. ” Uncomment the following to have Vim jump to the last position when
  22. ” reopening a file
  23. “if has(“autocmd”)
  24. ”  au BufReadPost * if line(“‘\””) > 0 && line(“‘\””) <= line(“$”)
  25. ”    \| exe “normal g’\”” | endif
  26. “endif
  27. ” Uncomment the following to have Vim load indentation rules according to the
  28. ” detected filetype. Per default Debian Vim only load filetype specific
  29. ” plugins.
  30. if has(“autocmd”)
  31.   filetype indent on
  32. endif
  33. ” The following are commented out as they cause vim to behave a lot
  34. ” differently from regular Vi. They are highly recommended though.
  35. “set showcmd            ” Show (partial) command in status line.
  36. “set showmatch          ” Show matching brackets.
  37. “set ignorecase         ” Do case insensitive matching
  38. “set smartcase          ” Do smart case matching
  39. “set incsearch          ” Incremental search
  40. “set autowrite          ” Automatically save before commands like :next and :make
  41. “set hidden             ” Hide buffers when they are abandoned
  42. “set mouse=a            ” Enable mouse usage (all modes) in terminals
  43. ” Source a global configuration file if available
  44. ” XXX Deprecated, please move your changes here in /etc/vim/vimrc
  45. if filereadable(“/etc/vim/vimrc.local”)
  46.   source /etc/vim/vimrc.local
  47. endif
นอกจากไปแก้ใน /etc/vim/vimrc แล้วยังทำได้อีกวิธีคือ สร้างไฟล์ .vimrc ไว้ใน home directory แล้วก็ใส่สองบรรทัดนี้ลงไป

  1. syntax on
  2. set autoindent

แต่วิธีนี้ยังไม่ได้ลอง แต่ดูแ้ล้วเป็นวิธีที่เค้านิยมใช้กันมากกว่าแก้ vimrc โดยตรงนะ

รายละเอียดเพิ่มเติมใน .vimrc ดูได้จาก http://www.csxix.com/?q=node/54