https://www.vim.org/

Add a \c in front of the keyword

1
/\ckeyword

Trace back previous history

Press : and then CTRL + f
Command that previouly used
Then can select the previous command by typing Enter key


Search for multiple words at a time

1
/foo\|bar

This will match both foo and bar at a time.


Change/Delete Word

There are 3 ways to change a word:

  • cw - This is only for your cursor is now in the first character position
  • caw - This won’t care where your cursor is now pointing to, but it will remove the space in front as well
  • ciw - This won’t care where your cursor is now pointing to, and it won’t remove the space in front

Example: var foo = "this is foobar", lets the cursor is in b in foobar word, when you issueing:

  • cw become var foo = "this is foo"
  • caw become var foo = "this is"
  • ciw become var foo = "this is "

NOTE: For delete word just change c to d


Show information about the editing file

1
CTRL + g

The output will be
"body_cust_pro_edit.cfm" 2660 lines --2%--


Replace with sub-match

For example: replace eval(somewordhere) to somewordhere
:%s/eval(\(\w\{1,}\))/\1/
NOTE

  • \( and \) is a sub-match which can be accessed by \1, \2…etc. Where \1 is the first sub-match.
  • \w is to match alphanumeric+underscore
  • {1,} is to indicate the min & max occurance for \w in this case, 1 is mean there must be at least one character and has no upper-bound

Another example: replace opener.frmFoo.elementName.value = value to window.opener.document.forms["frmFoo"].elements["elementName"].value = value
:%s/opener.frm\(\w\{1,\}\)\.\(\w\{1,\}\)/window.opener.document.forms["frm\1"].elements["\2"]/cgI
NOTE: The last three options are

  • c - will prompt you for each match see whether you want to replace
  • g - search globally, example: vim is vi improved and you search for vi, without g it will only replace the first occurance which is vim
  • I - case-sensitive match
Reference:

Case toggling

gu - switch to lower-case
gU - switch to UPPER-case
g~ - toggle between lower & UPPER case

Reference:

Change case by regex

Change FooBar to fOObAR

1
:%s/\(\w\)\(\w\{2\}\)\(\w\)\(\w\{2\}\)/\L\1\U\2\L\3\U\4/g

\L is change to lower-case (apply to all text after L)
\U is change to upper-case (apply to all text after U)

Reference:

Trace back search history

Press / and then CTRL + f
Search history
Then can select the previous command by typing Enter key


Count the number of occurance (pattern or word)

1
2
3
4
5
" Count the number of occurance of exactly 9-digit integer
:%s/\d\{9\}//gn

" Count the number of `foo`
:%s/foo//gn
Reference:

Remove the trailing space for each line

1
2
3
:%s/\(^ $\)\@!\&\s\+$//g
" Or
:%s/\(\S\zs\s\+$\)\|\(^\s\{2,}$\)//g
Reference:

Getting E576 error

1
2
3
$ vim foo.php
E576: viminfo: Missing '>' in line: on/foo.php^I368^I16
Press ENTER or type command to continue

When the error above occurred, just remove the ~/.viminfo will do

1
$ rm ~/.viminfo
Reference:

Show special character

1
2
:set list " to show the special character
:set nolist " to hide it back

Show special character

Reference:

Run a script directly

1
:!./run.sh
Reference:

Create a shortcut to run script

Ctrl+m will save the file then execute the /path/to/script.sh

1
map <C-m> :w<CR>:!/path/to/script.sh %<CR>

Replace with a new line character

1
:%s/,/\r/g

Replace all comma with new line

Reference:

Replace ^M with new line

1
:%s/^V^M/^V^M/g
Reference: