Vim tweaks

From lippmann wiki
Jump to: navigation, search

find replace confirmation

add a c to the find/replace command to get a comfirmation prompt at each replacement.

:%s/old/new/gc

viewports

I often split my vim session so I can compare textfiles when coding some basic php stuff. To split a page with vim, run :sp <filename> or :vsp <filename>.

sp will split the Vim window horizontally. The existing file text will be shown on the bottom or top half of the screen, and the newly opened file will be shown above or below this text.
vsp will split the Vim window vertically. The existing file text will be shown on the left or right half of the screen, and the newly opened file will be shown on the remaining part of the screen.

To switch between the views (also referred to as viewports) you use ctrl-w in combination with other keys:

Ctrl-w Ctrl-w moves between Vim viewports.

Ctrl-w j moves one viewport down.

Ctrl-w k moves one viewport up.

Ctrl-w h moves one viewport to the left.

Ctrl-w l moves one viewport to the right.

Ctrl-w = tells Vim to resize viewports to be of equal size.

Ctrl-w - reduce active viewport by one line.

Ctrl-w + increase active viewport by one line.

Ctrl-w q will close the active window.

Ctrl-w r will rotate windows to the right.

Ctrl-w R will rotate windows to the left.

indentation

To avoid tab characters, and reduce the depth of functions or loops, I have vim interpret tabs as spaces, and when I press the tab button it adds 2 spaces instead of a tab character. In debian, for this all I need to do is create a file named /etc/vim/vimrc.local as root, and add below text:

set smartindent
set tabstop=2
set shiftwidth=2
set expandtab

Start/end each line with the same string

To prepend each line with a specific string, you can do below:

:%s!^!//!

For example:

:%s!^!/#/!

Puts a # at the beginning of each line in a document.

Conversely, to append a string to the end of each line, you can do below:

:%s/$//g

For example:

:%s/$/#/g

Which puts a # at the end of each string.