Wednesday, 24 March 2010
VIM: Opening an External File using Wildcards
VIM allows the use of wild cards when opening or reading another file with the proviso that only ONE file may match.
e.g.
:r inc/*export*
or
:tabe ../*books/booklist*
(corrected)
e.g.
:r inc/*export*
or
:tabe ../*books/booklist*
(corrected)
Labels: wildcards
Friday, 19 March 2010
VIM: Creating a Custom Command with a Temporary Map
Maps allow us to create our custom Vim commands
The VIm initialization file .vimrc is typically used to store maps.
eg:-
imap ,,, <esc>bdwa<<esc>pa><cr></<esc>pa><esc>kA
" s commands these are wrap html around visually selected text
vnoremap sb "zdi<b><C-R>z</b><ESC>
vnoremap sq "zdi"<C-R>z"<ESC>
vnoremap sp "zdi<p><C-R>z</p><ESC>
vnoremap s( "zdi(<C-R>z)<ESC>
However it is very easy to create a temporary map eg to use the key '#' as a map
eg a map to copy the word under the cursor into the paste buffer
:map # "*yiw
A map has an advantage over a recording in this case as it is only necessary to type one character, a recording has an advantage over a map in that you don't have to think it out, so why not combine the two?
Map the key # such that it executes the recording q
:map # @q
The VIm initialization file .vimrc is typically used to store maps.
eg:-
imap ,,, <esc>bdwa<<esc>pa><cr></<esc>pa><esc>kA
" s commands these are wrap html around visually selected text
vnoremap sb "zdi<b><C-R>z</b><ESC>
vnoremap sq "zdi"<C-R>z"<ESC>
vnoremap sp "zdi<p><C-R>z</p><ESC>
vnoremap s( "zdi(<C-R>z)<ESC>
However it is very easy to create a temporary map eg to use the key '#' as a map
eg a map to copy the word under the cursor into the paste buffer
:map # "*yiw
A map has an advantage over a recording in this case as it is only necessary to type one character, a recording has an advantage over a map in that you don't have to think it out, so why not combine the two?
Map the key # such that it executes the recording q
:map # @q
Saturday, 6 March 2010
Useful VIM Abbreviations for debugging Perl
iab perlb print "<p>debug ::: $_ :: $' :: $` line ".__LINE__."\n";exit;
iab perlbb print "<p>debug ::: <C-R>a line ".__LINE__."\n";exit;
iab perlbd do{print "<p>debug :: <C-R>a line ".__LINE__."\n";exit} if $_ =~ /\w\w/i;
iab perld use Data::Dumper;$Data::Dumper::Pad="<br>";print Dumper @product_array;exit;
the <C-R>a automatically inserts whatever variable you had previously stored in register a
Dumper allows you to display arrays and hashes, you really should be using it.
Perl can be debugged using the Perl debugger eg > perl -d test.pl
or with the tk gui
perl -d:ptkdb test.pl
iab perlbb print "<p>debug ::: <C-R>a line ".__LINE__."\n";exit;
iab perlbd do{print "<p>debug :: <C-R>a line ".__LINE__."\n";exit} if $_ =~ /\w\w/i;
iab perld use Data::Dumper;$Data::Dumper::Pad="<br>";print Dumper @product_array;exit;
the <C-R>a automatically inserts whatever variable you had previously stored in register a
Dumper allows you to display arrays and hashes, you really should be using it.
Perl can be debugged using the Perl debugger eg > perl -d test.pl
or with the tk gui
perl -d:ptkdb test.pl
Labels: debugger, debugging, Perl, vim
Wednesday, 24 February 2010
VIM Folding Away Unwanted Text/code
I've only just started using folding but how did I live without it before and it's so EASY! You make sections of the code you are uninterested in "disappear" with a fold or several folds. Of course many editors have folding but VIM combines folding with the power of its motion commands, you can even use regex!
" folding : hide sections to allow easier comparisons
zf} : fold paragraph using motion
v}zf : fold paragraph using visual
zf'a : fold to mark
zo : open fold
zc : re-close fold
:help folding
the folds I used
a) move to beginning of code I wish to work on and everything before
zf1G : fold all lines from current line to beginning of the file
a) move to end of code I wish to work on and everything after
zfG
zf} : fold paragraph using motion
v}zf : fold paragraph using visual
zf'a : fold to mark
zo : open fold
zc : re-close fold
:help folding
the folds I used
a) move to beginning of code I wish to work on and everything before
zf1G : fold all lines from current line to beginning of the file
a) move to end of code I wish to work on and everything after
zfG
Labels: folding, matchit.vim
Monday, 22 February 2010
Matching html/xml/script tags with matchit.vim
It is incredibly useful to be able to match tags in tangled PHP,Javascript and HTML files this is possible with the plugin matchit.vim. You will already know that Vim matches or jumps to a matching parenthesis ({{ , with matchit.vim you can jump to a matching tag.
Recently I need to configure it so that it would match tags in html files with the arbitrary extension index.raa
I achieved this by adding *.raa to the following line
au BufNewFile,BufRead *.html,*.htm,*.shtml,*.stm,*.raa call s:FThtml()in filetype.vim
but that's not upgrade safe so instead create
a subdirectory ftdetect in your vim folder
and create a file (in my case)
raa.vim containing just one line
au! BufRead,BufNewFile *.raa set filetype=raa
Labels: matchit.vim
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
"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: vim syntax
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 "%"
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 "%"
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: numbering lines
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
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: cygwin
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
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
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)
Subscribe to Posts [Atom]