vim command

Created:

Updated:

Here I summarized vim commands that are used frequently for development.

  • How to move cursor to the location in history

    command Description
    CTRL + o move to cursor to previous location in history
    CTRL + I move to cursor to next location in history



  • How to use vim registers
    • ex) “ayy, “ap, :reg



  • How to search for “a or b” in the current file. ‘\c’ means ignore case.
    1
    
     :vimgrep /\ca\|b/ <CTRL-R><SHIFT-%>
    


  • How to search two words(“a” and “b”) in either order in the current file. ‘\c’ means ignore case.
    1
    
     :vimgrep /\c.*a\&.*b/ <CTRL-R><SHIFT-%>
    
    • reference
      • https://vim.fandom.com/wiki/Search_patterns


  • How to show each buffer in a tab on gvim.
    1
    
     :tab ball
    


  • How to switch between tabs on gvim.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
      In normal mode
      gt  //next tab
      gT  //Previous tab
      3gt //go to 3rd tab
    
      In command mode
      :tabfirst>
      :tabnext
      :tabprev
      :tablast
      :tabnext
      :tabm
      :tabclose
      :tabnew
    


  • how to show trailing whitespace.(assuming you use :set hlsearch)
    1
    
    :/\s\+$
    
    • $ matches the end of a line or string.
    • \s matches whitespace character.
    • + matches the preceding pattern element one or more times.


  • how to delete any trailing whitespace at the end of each line.
    1
    
    :%s/\s\+$//g
    
    • reference
      • https://vim.fandom.com/wiki/Remove_unwanted_spaces


  • how to delete ^M in a file(^M is Ctrl + v + m)
    1
    2
    3
    
     :set ffs=unix
     :e!                   // reload
     :%s/^M//g             // ^M = ctrl + v + m
    


  • how to delete ^M for multiple files(^M is Ctrl + v + m)
    1
    
      $ find ./ -iname * -exec dos2unix {} \;
    


  • how to delete trailing whitespaces for multiple files
    1
    
      $ find . -type f -name '*.txt' -exec sed --in-place 's/[[:space:]]\+$//' {} \;
    


  • how to compare 2 directories in vim
    1
    
     :DirDiff folder_1 folder_2
    


  • how to compare 2 directory lists with vimdiff
    1
    
     $ vimdiff <(cd android-4.0.1_r1; find . | sort) <(cd android-4.0.2_r1; find . | sort)
    

Tags:

Categories:

Updated:

Leave a comment