I’ve been working on various Linux servers recently (Centos) which had just the standard installs and have had to use console vim. When you type vi on a Linux console you still get vim but it may be tiny-vim or may be vim in original vanilla vi compatibility mode which will drive you crazy as many common commands you use frequently just won’t be there.

What you need is

> yum install  vim-common vim-enhanced vim-minimal

 

and then add an alias or whatever

 

alias vi=’vim’

 

 

 

” type table,,, to get <table></table> automatically close a tag
imap ,,, <esc>bdwa<<esc>pa><cr></<esc>pa><esc>kA

 

or type td,,, to get < td> < /td> etc etc

 

Vim’s visual block mode is a feature that I hardly ever use but is extremely useful when I do need it.
You can highlight a column of text or numbers in the center of a page and perform various edit operations on that text. It is unbelievably powerful and I realize on writing this that I only use a fraction of the blockwise possibilities.

You enter Visual Blockwise mode with Control V however I uses that for Paste so have it remapped to Control Q

” Visual is the newest and usually the most intuitive editing mode

” Visual basics

v : enter visual mode

V : visual mode whole line

: enter VISUAL BLOCKWISE mode (remap on Windows to say C-Q *C*

gv : reselect last visual area (ultra)

o : navigate visual area

“*y or “+y : yank visual area into paste buffer [C]

V% : visualise what you match

V}J : Join Visual block (great)

V}gJ : Join Visual block w/o adding spaces

`[v`] : Highlight last insert

:%s/\%Vold/new/g : Do a substitute on last visual area [N]

:help blockwise-visual

 
The following should all be on one line

:sav! $VIMRUNTIME/doc/vimtips.txt|:1,/^__BEGIN__/d|:/^__END__/,$d|:w!|:helptags $VIMRUNTIME/doc

Save the Vim Tips Web Page as an HTML file edit with VIM then execute the above line. This strips off the unwanted stuff outside the __BEGIN__ and __END__ tags, writes the remainder to the VIM Help Tags folder and then runs helptags to generate the tags. Note the above line of Vim commands is to be found in the VIMTips file execute it with yy@”

” A VIM Help file requires a line such at the start:

*vimtips.txt* For Vim version 7.3. Last change: 2011 Dec 11

” and a line such at the end
vim:tw=78:ts=8:ft=help:norl:

The Searchable Helptags are generated from any text enclosed by stars, replace any spaces by a hypen.
I’m used a leading “best to help me find my own help tags. eg

:help best will list my tags

This tip suggested by Fredrik Acosta

 

Some Vim experts have a practically empty .vimrc file (the config file) others like me have a huge one unfortunately in my case that means there is a lot of dead wood in it. To counteract that I now comment my .vimrc file (the doublequote ” is the comment delimiter) and am gradually commenting older parts of my vimrc and deleting stuff that is no longer relevant or is just plain junk. Remember you can use conditionals in your .vimrc so that particular parts of the config only apply to say php files or distinguish whether you are on your laptop or PC. Finally be aware of the danger that something in your config could “hide” or counteract some of Vim’s functionality.

I regularly backup my .vimrc and also have a copy stashed away on the Internet that I can download and use should I have to set up with a new PC.

 

This tip discusses replacing a marker line with the contents of a file

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

 
This is a common editing problem, I want to replace the first TD tag after a certain string.
So every time ‘string1′ is found replace the next td tag with string2 even if not on the same line.


:%s#string1\(\_.\{-}\)<td[^>]\+>#\1string2#

Explanation:-
\_.\{-} any non-greedy sequence of characters before td tag, multi-line if necessary.
\(..\) memorize it
[^>]\+> any sequence of character up until >

Principle:-
The memory part is important, it puts back the “unknown” string between string1 and string2 as \1.

The simpler same line only version


:%s#string1\(.\{-}\)<td[^>]\+>#&string2#

 

One irritation with the default set up of VIM is where you paste into a file and text is formatted either with unwanted tabs or is prefixed by a “comment” hash etc. As this is rarely what you want the setting to prevent this is:-

:set paste

Another problem is when you paste from say Microsoft Word and you get a lot of junk formatting codes

” filter non-printable characters from the paste buffer
” useful when pasting from some gui application
:nmap p :let @* = substitute(@*,’[^[:print:]]’,”,’g')“*

 
gf can open all files paths relative to the current directory eg:-
index.php
main/main.php
../index.php

But gf doesn’t know what to do with root based paths because it obviously has no idea where the file actually is. This is highly frustrating but as usual with vim there is a work around in fact there are several but the one I find horsiest is the recently introduced includeexpr.

This works by performing a virtual substitute on each root path it recognizes replacing the
root with a full path eg.
/js/main.js
with
/var/www/html/js/main.js

/var/www/html/js/main.js

:set includeexpr=substitute(v:fname,’/js/’,'/var/www/html/js/’,'g’)

Of course you have to set this up for each project you work depending on the root path.

 

I’ve only recently started creating my own menus it’s really easy. I’m maxxed out on maps and abbreviations so find it useful to put stuff in these menus.


" in your _vimrc (or .vimrc)

The "My" identifier is the name of the Menu and binds all the commands together

amenu My.Insert\ a\ VIM\ modeline ggOvim:ff=unix ts=4 ss=4vim60:fdm=markergg
" fName: Only the filename (without path)
" fPath: Only the path
" rName: path+filename relative to current directory
amenu My.Copy\ fName :let @*=expand("%:t")
amenu My.Copy\ fPath :let @*=expand("%:p:h")
amenu My.Copy\ rName :let @*=expand("%:.")
amenu My.Tab\ sball :tab sball
amenu My.vimtips :tabe c:/intranet/vimtips.txt

© 2012 VIM Tips Blog Suffusion theme by Sayontan Sinha