Tag Archives: vim

VIM skills

Merge multiple blank lines

:%s/^\_s\+\n/\r/g

Note: \_s match white space and blank lines

Grep by unicode character

# replace back slash(\) to front slash(/)
:%s#\%x5C#/#g

:help regexp for details about some specific syntax to select unicode characters with a regular expression in Vim.

|/\%d|  \%d \%d match specified decimal character (eg \%d123)
|/\%x|  \%x \%x match specified hex character (eg \%x2a)
|/\%o|  \%o \%o match specified octal character (eg \%o040)
|/\%u|  \%u \%u match specified multibyte character (eg \%u20ac)
|/\%U|  \%U \%U match specified large multibyte character (eg \%U12345678)

vimdiff ignore white space

To ignore white spaces while using vimdiff.

set diffopt+=iwhite

From the command line use:

vimdiff -c 'set diffopt+=iwhite' ...

To have vimdiff ignore whitespace while normal vim doesn’t, simply put this into your .vimrc:

if &diff
    " diff mode"
    set diffopt+=iwhite
endif

Vim get the current cursor char ascii code and replace it

In command mode press the keys ga (like get ascii) or :as / :ascii on the command line to find out the ascii code of the character where the cursor is. This displays the value of the current character in decimal, hex and octal. (Think “get ascii.”)

To do a search and replace with an hex code

:%s/\%x0d.*$//d

Being 0d the ascii character found with ga

Seeing ASCII/Unicode values in the status line

You can also add options to the statusline option to show the ASCII/Unicode value of the character under the cursor.

:set statusline=%<%f%h%m%r%=%b\ 0x%B\ \ %l,%c%V\ %P

It’s the %b\ 0x%B that does the trick. Note that you may need to :set laststatus=2 so that the status line is visible in with only one window showing.

See also at http://vim.wikia.com/

vim switch from vertical split to horizontal split

Vim mailing list says (re-formatted for better readability):

To change two vertically split
windows to horizonally split

Ctrl-W t Ctrl-W K

Horizontally to vertically:

Ctrl-W t Ctrl-W H

Explanations:

Ctrl-W t     makes the first (topleft) window current
Ctrl-W K     moves the current window to full-width at the very top
Ctrl-W H     moves the current window to full-height at far left

Note that the t is lowercase, and the K and H are uppercase.

Also, with only two windows, it seems like you can drop the Ctrl-W t part because if you’re already in one of only two windows, what’s the point of making it current?

http://vim.wikia.com/wiki/Switch_between_Vim_window_splits_easily
http://stackoverflow.com/questions/1269603/to-switch-from-vertical-split-to-horizontal-split-fast-in-vim

In VIM, take a look at the following to see different alternatives for what you might have done:

:help opening-window

For instance:

Ctrl-W s
Ctrl-W o
Ctrl-W v
Ctrl-W o
Ctrl-W s
...