IceBorworntat's Blog

competitive coder

Vim

Sunday, January 3, 2021, 10:56 AM

วันนี้ผมมาแนะนำ Text Editor ที่สามารถใช้ในการเขียนโปรแกรมได้ครับ
Vim(Vi Improved) พัฒนามาจาก Vi ส่วนมากคนที่ใช้ Vim จะเป็น Developer เพราะ Developer มักจะใช้ Mac หรือ Linux ซึ่งสามารถติดตั้ง Vim ได้ใน Terminal และสามารถใช้ Vim ได้ใน Terminal วิธีการ install บน terminal ของ Linux

icy@iceborowrntat:~$ sudo apt-get install vim

วิธีการ install บน terminal บน Terminal ของ MacOs

icy@icymacbookpro:~$ brew install vim

ในส่วนของ Windows จะใช้เป็น gVim หน้าตาของ Vim ตอนเปิดครั้งแรก

  1
~                                                                                                                       
~                                                                                                                       
~                                                                                                                       
~                                                                                                                       
~                                                                                                                       
~                                                                                                                       
~                                                                                                                       
~                                                  VIM - Vi IMproved                                                    
~                                                                                                                       
~                                                   version 8.1.2269                                                    
~                                               by Bram Moolenaar et al.                                                
~                                       Modified by team+vim@tracker.debian.org                                         
~                                     Vim is open source and freely distributable                                       
~                                                                                                                       
~                                            Become a registered Vim user!                                              
~                                    type  :help register<Enter>   for information                                      
~                                                                                                                       
~                                    type  :q<Enter>               to exit                                              
~                                    type  :help<Enter>  or  <F1>  for on-line help                                     
~                                    type  :help version8<Enter>   for version info                                     
~                                                                                                                       
~                                                                                                                       
~                                                                                                                       
~                                                                                                                       
~                                                                                                                       
~                                                                                                                       
~                                                                                                                       
~                                                                                                                         
                                                                                                    0,0-1         All 

โหมดในการใช้ Vim
1. Command Mode 2. Insert Mode 3. Replace Mode 4. Visual Mode [Visual line / Visual]

Command Mode
ใช้สำหรับใส่คำสั่งต่างๆ เช่น :w(เซฟไฟล์) :q(ออกจากโปรแกรม) :y(yank หรือ คัดลอกข้อความ)
วิธีการเข้า Command Mode กดปุ่ม ESC

Insert Mode
ใช้สำหรับ ใส่ข้อมูลลงไปในไฟล์
วิธีการเข้า Insert Mode กดปุ่ม i ใน Command Mode

Replace Mode
ใช้สำหรับแทนที่ข้อความ
วิธีการเข้า Replace Mode กดปุ่ม Insert ใน Insert Mode

Visual Mode
แบ่งออกเป็น 2 โหมด
1.Visual line
ใช้สำหรับ เลือกทั้งบรรทัด
วิธีการเข้า Visual line กดปุ่ม Shift-V ใน Command Mode
2.Visual
ใช้สำหรับ เลืกเป็นตัวอักษร
วิธีการเข้า Visual กดปุ่ม Ctrl-V ใน Command Mode

ใน Vim สามารถตั้งค่าโดยการเข้าไปที่ $MYVIMRC ซึ่งสามารถเข้าไปแก้ไขได้โดย

:e $MYVIMRC

.vimrc ของผม

" Vim with all enhancements
source $VIMRUNTIME/vimrc_example.vim

" Use the internal diff if available.
" Otherwise use the special 'diffexpr' for Windows.
if &diffopt !~# 'internal'
  set diffexpr=MyDiff()
endif
function MyDiff()
  let opt = '-a --binary '
  if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
  if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
  let arg1 = v:fname_in
  if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
  let arg1 = substitute(arg1, '!', '\!', 'g')
  let arg2 = v:fname_new
  if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
  let arg2 = substitute(arg2, '!', '\!', 'g')
  let arg3 = v:fname_out
  if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
  let arg3 = substitute(arg3, '!', '\!', 'g')
  if $VIMRUNTIME =~ ' '
    if &sh =~ '\<cmd'
      if empty(&shellxquote)
        let l:shxq_sav = ''
        set shellxquote&
      endif
      let cmd = '"' . $VIMRUNTIME . '\diff"'
    else
      let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
    endif
  else
    let cmd = $VIMRUNTIME . '\diff'
  endif 
  let cmd = substitute(cmd, '!', '\!', 'g')
  silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3
  if exists('l:shxq_sav')
    let &shellxquote=l:shxq_sav
  endif
endfunction
set gfn=FixedSys:h12
set nu rnu
autocmd InsertEnter * set nornu
autocmd InsertLeave * set rnu
nnoremap <F9> :w <CR> :!g++ -Wall -std=c++17 % -o %:t:r <CR>
nnoremap <F10> :!%:t:r<CR>
nnoremap <F11> :!del %:t:r.exe<CR>
set belloff=all
inoremap {<CR> {<CR>}<ESC>O
set ts=4
set sw=4
set si
"set vb t_vb=
au GUIEnter * simalt ~x
au GUIENTER * tabnew
au GUIENTER * tabn
set nobackup
set noundofile
set noswapfile

IceBorworntat

author