New VIM Tips Blog


This Blog now has its own Domain Name all new blog postings will appear at the new url.


Tuesday, 22 December 2009

 

Number Each Line of Current File


to merely display numbers
:set number

following will actually insert numbers but relies on external Linux utility nl
:new | r!nl # (where hash is alternate file name)

Vim Way
:%s/^/\=line('.'). ' '

Labels:


Monday, 21 December 2009

 

Replace a line with the contents of a file


Replace a marker line (contaning just the word mark) with the contents of an
external file doc.txt

" replace string with contents of a file, -d deletes the "mark"
:g/^MARK$/r doc.txt | -d



Thursday, 10 December 2009

 

Integrating Vim and Cygwin

I use and love Cygwin but mostly use the standard Windows gVim (Gui Vim) rather than that supplied by Cygwin.

in my .vimrc I have

if has('win32')
source $VIMRUNTIME/mswin.vim
behave mswin
set shell=c:\\cygwin\\bin\\zsh.exe shellcmdflag=-c shellxquote=\"
else
set ff=unix
endif

You will probably prefer bash.exe rather than zsh.exe

I can then run zsh/bash scripts from the command line e.g.
:!grep blah blah

Labels:


Wednesday, 9 December 2009

 

How to Get the Latest Patched Version of VIM

The Cream project maintains the latest version of VIM you can download it here . On the download page you can get latest version of VIM with or without Cream.

Cream is a free, easy-to-use configuration of the famous Vim text editor for Microsoft Windows, GNU/Linux, and FreeBSD. It uses common menus, standard keyboard shortcuts, and has extensive editing functions for the beginner and expert alike.

The current version for win32 is gvim-7-2-303.exe they call this a one-click install

Labels: ,


Monday, 7 December 2009

 

The Power of Control R


The Control-R mechanism is very useful:-
:h i_CTRL-R

Usually used for inserting the contents of a register 0-9a-z
but can also insert the following special registers etc

'"' the unnamed register, containing the text of the last delete or
yank
'%' the current file name
'#' the alternate file name
'*' the clipboard contents (X11: primary selection)
'+' the clipboard contents
'/' the last search pattern
':' the last command-line
'.' the last inserted text
'-' the last small (less than a line) delete
=5*5 insert 25 into text (mini-calculator)


Monday, 16 November 2009

 

Deleting all but first two fields in each line of a CSV File


In a csv file full of lines such
16 Prop_Description Property description, String 1000
(note that this is actually a space separated csv)

:%s#\(\d\+ \w\+\).*#\1#g

filters out all but the field number and name eg

16 Prop_Description

a regexp for normal comma separated csv would be

fred,joe,sid
"fred joe","sue","good boy

:%s/\([^,]\+\),\([^,]\+\).*/\1,\2/

Labels:


Sunday, 15 November 2009

 

VIM working with Paste Commands


The paste buffer is usually the easiest way to transfer text between VIM and another application.

" Redirection & Paste register *
:redir @* : redirect commands to paste buffer
:redir END : end redirect
:redir >> out.txt : redirect to a file
" Working with Paste buffer
"*yy : yank current line to paste
"+y} : yank to end of paragraph
"+yi{ : yank current paragraph
"*p : insert from paste buffer
" yank to paste buffer (ex mode)
:'a,'by* : Yank range into paste
:%y* : Yank whole buffer into paste
:.y* : Yank Current line to paster
" filter non-printable characters from the paste buffer
" useful when pasting from some gui application
:nmap p :let @* = substitute(@*,'[^[:print:]]','','g')"*pr

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]