Difference between revisions of "Vim tweaks"

From lippmann wiki
Jump to: navigation, search
Line 40: Line 40:
 
  set expandtab
 
  set expandtab
  
=Start each line with the same string=
+
=Start/end each line with the same string=
 
To prepend each line with a specific string, you can do below:
 
To prepend each line with a specific string, you can do below:
  
Line 50: Line 50:
  
 
Puts a # at the beginning of each line in a document.
 
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.

Revision as of 17:04, 13 February 2015

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.