Thursday, 10 November 2011
Vim Tips Blog has a new Home
Saturday, 16 October 2010
Creating and Using a VIM Scratch File
" put following in your _vimrc
" store for useful text, reusable stuff, code sections
map <f4> :tabe c:/aax/store.txt<cr>
imap <f4> :tabe c:/aax/store.txt<cr>
Hit the F4 key to open store.txt in a separate tab. I've currently got a few snippets of Perl I need frequently , a few telephone numbers etc. I find it very useful. The VIM maps above are not rocket science and you will see how to vary them for your own requirements.
(corrected)
" store for useful text, reusable stuff, code sections
map <f4> :tabe c:/aax/store.txt<cr>
imap <f4> :tabe c:/aax/store.txt<cr>
Hit the F4 key to open store.txt in a separate tab. I've currently got a few snippets of Perl I need frequently , a few telephone numbers etc. I find it very useful. The VIM maps above are not rocket science and you will see how to vary them for your own requirements.
(corrected)
Labels: vim
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
Subscribe to Posts [Atom]