Thursday, 28 January 2010

 

Automatically Enable VIM Syntax Highlighting for a non-standard file extension

:syntax on

"enable perl syntax highlighting for a file with a non-standard extension
:autocmd BufReadPost *.plx set syntax=perl

" some odd useful abbreviations
:cab synh set syntax=html
:cab synp set syntax=php

Labels:


Monday, 18 January 2010

 

Reading MS Word Documents with VIM

Put this in your .vimrc
autocmd BufReadPost *.doc %!antiword "%"

install antiword on your PC, comes with Cygwin and as a win32 executable.

then vou can do

gvim cv.doc

Remember this is READY ONLY you cannot Modify a Word Document but nevertheless very useful for fishing out the text you need from a Microsoft Word document or alternatively reading a Word document with all the advantages of Vims powerful search options

" set up vim to read MS Word documents read only
autocmd BufReadPre *.doc set ro
autocmd BufReadPre *.doc set hlsearch!
autocmd BufReadPost *.doc %!antiword "%"

Labels: ,


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

Sunday, 8 November 2009

 

Command Line Window q: or q/


One of original great improvements of VIM over Classic Vi was the ability to reuse/edit/correct commands laboriously typed in, you had only simple line edit commands available to do this, normally perfectly sufficient of course. However you can also open a command line window to gain full vim command line editing commands:-

Note these are normal-mode commands!
q:
Creates a small window of your most recent commands

q/
Creates a small window of your most recent searches

hitting "return" will execute whatever line you are editing

to close/leave the command window without executing the current line use :quit


Labels:


Wednesday, 4 November 2009

 

Using CVS with VIM


I'm now using the CVS that comes with Cygwin and the CVS plugin for VIM

and the plugin
http://www.vim.org/scripts/script.php?script_id=58

I used to save backups to a directory with a timestamp appended to the filename.

Now I'll have a proper system with version control, difference between version etc

Labels: , ,


Thursday, 29 October 2009

 

Editing the File name you Save or Write to (2)


Remember you can recall the file name with Control-R %
You can then edit the file name freely

Vim supports wild cards as long as only ONE file matches eg

:r ../*/ref.c

:e long*file*name.h

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

Subscribe to Posts [Atom]