René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Misc tips for VIM

Searching

Search and replace with lower case in VIM

In the command line, enter :%s/\(abc\)/\=tolower(submatch(1))/i

Searching for special characters

com! -nargs=* S exe "let @/='\\V".escape(<q-args>,' \/')."'<bar>norm! n"
This defines a user command (through com!) with the name S.
The command can then be executed with :S some* fancy \string /

Command line editing

The following command makes ctrl-f edit the command line when being in the command line:
:set cedit=^F

Using normal in a script

In order to simulate keystrokes for normal mode in a vim script, one can use normal. For example, the following command deletes the first line:
normal 1Gdd
However, a problem is that this doesn't allow to dynamically specify a line to be deleted. The following wouldn't work
function DelLine(line) 
  normal a:line . "Gdd"
endfu
For this to make it work, one has to use execute normal
function DelLine(line) 
  execute "normal " . a:line . "Gdd"
endfu

Splitted buffers

Scrolling the top buffer

The following two lines create two maps which can be used to scroll the top split of a window up (by typing ,u) and down (by typing ,d), respectively.
nmap ,u <c-w>t<c-e><c-w>p
nmap ,d <c-w>t<c-y><c-w>p
<c-w>t: This goes to the top window
<c-e>: Moves the buffers content one line up
<c-w>p: Goes to the previous window (the one with the input focus before the top window got it).

Yanking

Yanking all lines in a buffer that match a pattern

:g/pattern/y A
This command yankes all lines in the buffer that contain 'pattern' into the register a. If there already was something in a, the lines will be appended in the register.
A's content can then be pasted like
"ap

Windows

View source with gvim instead of notepad in IExplorer

REGEDIT4

[HKEY_CLASSES_ROOT\Applications\gvim.exe]

[HKEY_CLASSES_ROOT\Applications\gvim.exe\shell]

[HKEY_CLASSES_ROOT\Applications\gvim.exe\shell\edit]

[HKEY_CLASSES_ROOT\Applications\gvim.exe\shell\edit\command]
@="c:\\vim\\vim62\\gvim.exe \"%1\""

[HKEY_CLASSES_ROOT\.htm\OpenWithList\gvim.exe]