vim


Ctrl+w   switch between windows (e.g. nerd tree)

di"   delete inside quotation mark
expand all folds:    zR
save readonly:   :w!
Save a without permissions    :w !sudo tee %
Auto indent:       v+selectText+=
popup menu of words (in insert mode) Ctrl+p
Goto line 42         :42

:set number  //to show line numbers
:syn off  //to turn off syntax highlighting if makes it unreadable
:set ic   (ignorecase) <---> :set noic    (noignorecase)
:set hlsearch  highlight all search instances <---> :nohlsearch

:! ls     will execute ls command on your shell

Tabs

:tabnew name_of_file_for_vim
:tabdo %s/foo/bar/g             across all tabs

heighlight words

Search and hghlight
/next\|Abe     search separated with with \|
Just Highlight
:let m = matchadd('Todo','leading')
:let n = matchadd('ErrorMsg','ret') 
:let m = matchadd('Todo','searchTerm')

:let m = clearmatches()

Multi-Keyword highlight :help match
You could use the :match command
:match    word1/\c      for ignore case
:match ErrorMsg /word2/
:match PmenuSel /word3/
:match Visual /word4/
:match DiffChange /word5/

The first parameter of the command is the highlight-group (to see all available groups :highlight). The second parameter is a search pattern.


To learn vim use $ vimtutor


Vim Tutorial

Working with Files
Edit :e filename
Write :w filename
Quit :q
Quit without Save :q!
 Write & Quit
 :wq
 Write (read_only file)
 :w!
 Write & Quit (read_only file)
 :wq!
  

Navigating in file
 left down up right hjkl
 End of word e
 Begin of word
 b
 0'th character of line
 0 (or ^)
 End of line
 $
 Home (first line of screen)
 H
 Middle M
 Last (last line of screen)
 L
 Page up
 Ctrl+U
 Page down
 Ctrl+D
 Jump to line # n
 :n
 Scroll up
 Ctrl+e
 Scroll down
 Ctrl+y
 Beginning of file
 gg
 End of file
 G

Edit
 Insert i
 Append after cursor
 a
 Delete
 x
 Backspace X
 Delete line
 dd
 Yank (copy) line
 yy
 Paste before
 p
 Paste after
 P
 Indent
> OR <
 Default mode
 Escape Key
 Visual mode
 v
 ------ Yank (copy)
 y
 ------ Delete d
  
 Undo u
 Redo Ctrl+r

Search & Replace
 Search /pattern           \c makes it ignore case
 ----- next
 n
 ----- previous
 N
 Replace :%s/pattern/substitute/gic         i for ignore case    c for confirmation
            
               


Learn to speak vim – verbs, nouns, and modifiers!



Using vim is like talking to your editor in ‘verb modifier object’ sentences, turned into acronyms

  • learn some verbs: v (visual), c (change), d (delete), y (yank/copy). these are the most important. there are others
  • learn some objects: w (word), s (sentence) p (paragraph) there are others
  • learn some modifiers: i (inside), a (around), t (till..finds a character)

To move efficiently in vim, don’t try to do anything by pressing keys many times, instead speak to the editor in sentences

  • delete the current word: diw (delete inside word)
  • delete a string inside quotes: di” (delete inside quote)
  • copy this line: yt$ (yank till $ – end), or yy
  • change this line ct$ (change till $) or cc (are you seeing a pattern?)
  • visually select this paragraph: vap (visual around paragraph)

If you understand the verbs and objects you’re dealing with, you will soon realize that adding a new plugin and learning a new verb or noun exponentially increases your productivity, as you can now apply it in all the sentences you already know. It’s just like learning a language.

  • install surround.vim: vim-surround- you get a new noun, the ‘surround’ (s or S)
    • visually select a word and surround it with quotes: viwS”
    • change surround from quote to single quote: cs’”
  • install vim-textobj-rubyblock – you get a new noun, the ‘ruby block’ (r)
    • delete current ruby block: dir (delete inside ruby block)
    • visually select a ruby function: var (visual around ruby block)
    • visually select the innards of a function: vir (visual inside ruby block)
  • install tComment – new verb: “gc” (go comment)
    • comment the current ruby method: gcar (go comment around ruby)

Now go out and learn a new verb or noun every day!











Comments