Monday, March 1, 2010

Re: put html tags around selected text

Hej :)

> Tried everything but I believe I haven't understand very well what you
> are doing here.
> what is 41a + a ?
I'll try to explain...

--------------------------------------
1 function! Blub() range
2 exec "normal `<i<!--\<esc>" " start of the comment
3 if a:firstline == a:lastline
4 exec "normal `>4la-->" " end
5 else
6 exec "normal `>a-->" " end
7 endif
8 endfunction
9
10 vmap ,C :call Blub()<CR>
--------------------------------------

After you have selected something in visual mode the normal mode command
`< moves you to the start of your selection, and `> to the end of it.
See :help `<

Line 2 first jumps to the start of the selection, (`<) enters insert
mode (i) and inserts "<!--". The explicit use of \<esc> is necessary to
get out of insert mode (or so I think).
The conditional is used because if the end of the selection is on the
same line as is the start of the selection then the last character you
selected will be 4 steps to the right of the original selection, because
the string "<!--" has been inserted before. As you can see, the only
difference of the commands in the body of the if/else is the 4l, which
moves the cursor 4 steps to the right.
The a is for append which inserts "-->" after the cursor position (which
in this case is after the originally selected character), and somehow
doesn't need "\<esc>" at the end. Maybe this isn't necessary in line 2
either. I'm no vimscript expert ;)

So as you can see, this is just a hack. If you want to use it for more
than just one case -- luckily I don't have to edit much html/xml, so I
did not care ;) -- it would be best to get rid of that hardcoded four,
see :help functions and choose wisely :)

> This is my output:
> <!-->test -->
That looks strange. Are you sure you got the quotes/accents right?

I transform
^test$
(where ^ is start-of-line and $ is end-of-line)
to
^<!--test-->$

Or when selecting both lines
^test$
^blub$
^<!--test$
^blub-->$

Hope this was helpful :)
Kai

--
All mail clients suck. This one just sucks less.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: