surrounding a word with tag with a dynamic attribute

38 views
Skip to first unread message

discipulus

unread,
May 4, 2010, 11:17:31 AM5/4/10
to vim...@googlegroups.com

Hello,

I have this problem. I would like to put my cursor under a letter in a word
(e.g. discipulus) and I want that word to be surrounded by: discipulus where
X is the number of the letter in the word.

I need all that to mark a bunch of accents in a document.

I do know about the surround plugin but I don't know how to figure out the
dynamic part.

Thanks, everybody!
Sergius
--
View this message in context: http://old.nabble.com/surrounding-a-word-with-tag-with-a-dynamic-attribute-tp28447764p28447764.html
Sent from the Vim - General mailing list archive at Nabble.com.

--
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

Ben Fritz

unread,
May 4, 2010, 6:28:19 PM5/4/10
to vim_use


On May 4, 10:17 am, discipulus <gera...@gmail.com> wrote:
> Hello,
>
> I have this problem. I would like to put my cursor under a letter in a word
> (e.g. discipulus) and I want that word to be surrounded by: discipulus where
> X is the number of the letter in the word.
>

Huh?

Where's the X come in? What do you want to surround? What do you want
to surround with?

discipulus

unread,
May 4, 2010, 7:23:40 PM5/4/10
to vim...@googlegroups.com

I'm so sorry,

It should have been {span class="sX"}discipulus{/span}

I wrote this function just now:

function! mytest()
let l:current_column=col('.')
exe 'normal b'
let l:beginning_column=col('.')
let l:diff = l:current_column - l:beginning_column + 1
" this relies on the surround plugin
exe 'normal vs'
endfunction

What do you think and thanks for your response!
Sergius
--
View this message in context: http://old.nabble.com/surrounding-a-word-with-a-tag-%2B-a-dynamic-attribute-tp28447764p28454219.html
Sent from the Vim - General mailing list archive at Nabble.com.

David Fishburn

unread,
May 5, 2010, 10:23:52 AM5/5/10
to vim...@googlegroups.com
On Tue, May 4, 2010 at 7:23 PM, discipulus <ger...@gmail.com> wrote:
...
> It should have been {span class="sX"}discipulus{/span}
>
> I wrote this function just now:
>
> function! mytest()
>  let l:current_column=col('.')
>  exe 'normal b'
>  let l:beginning_column=col('.')
>  let l:diff = l:current_column - l:beginning_column + 1
> " this relies on the surround plugin
>  exe 'normal vs'
> endfunction
>
> What do you think and thanks for your response!

I wrote my own function specifically to wrap XML tags around some
highlighted text. If the text is part of a line I wrap it inline. If
the text (visually selected) spans multiple lines, then I add the tags
on new lines before and after the text.

I also allow you to add attributes and remove those when adding the closing tag.

" Tip #346: Tag Select/Wrapper:/*{{{*/
" http://vim.sourceforge.net/tips/tip.php?tip_id=346
" Author: David Fishburn
" These mappings and TagSelection function will allow you to place
" an XML tag around either the current word, or the current selected
" text.
" If the visual select is on a single line, the tag is wrapped
" around the text <this>way</this>. If the visual select extends
" over multiple lines, the tag is wrapped around the text
" <this>
" way
" </this>
"
" When you are prompted for the tag name, you can enter:
" Tag name? p class="classname" attri="bute"
" The select is wrapped with:
" <p class="classname" attri="bute">
" Your selection
" </p>
" Notice the attributes have been stripped from the closing tag.
"
" Use nmap, not nnoremap, since we do want to use an existing mapping
nmap ,,, viw,,,
vnoremap ,,, <Esc>:call TagSelection()<CR>

function! TagSelection()
let tag = input("Tag name (include attributes)? ")

if strlen(tag) == 0
return
endif

" Save b register
let saveB = @b
" <C-R> seems to automatically reindent the line for some filetypes
" this will disable it until we have applied our changes
let saveIndent = &indentexpr
let curl = line(".")
let curc = col(".")
let &indentexpr = ''

" If the visual selection is over multiple lines, then place the
" data between the tags on newlines:
" <tag>
" data
" </tag>
let newline = ''
if getline("'>") != getline("'<")
let newline = "\n"
let curl = line("'>")
endif

" Strip off all but the first word in the tag for the end tag
let @b = newline . substitute( tag, '^[ \t"]*\(\<\S*\>\).*', '<\/\1>\e', "" )
let curc = curc + strlen(@b)
exec "normal `>a\<C-R>b"

let @b = substitute( tag, '^[ \t"]*\(\<.*\)', '<\1>\e', "" ) . newline
let curc = curc + strlen(@b)
exec "normal `<i\<C-R>b"

" Now format the area
exec "normal `<V'>j="

" Restore b register
let @b = saveB
let &indentexpr = saveIndent

call cursor(curl, curc)
endfunction
" /*}}}*/


If it is of any use to you.

Enjoy.
Dave

discipulus

unread,
May 5, 2010, 10:26:56 AM5/5/10
to vim...@googlegroups.com

Unfortunately the function does not work if the cursor is under the first
letter of a word. In addition, I have realized that I need to add another
attribute to that "class" and that would be the position of any letter with
an apex.

Example: the word domús would need to be transformed into {span class="s2
a4"}domus{/span}. Naturally, if the word already has a span with "class" in
it, "s2" would need to be added first (after I press a key combination under
"o") and then "a4" after I press another key combination under "ú".

AND, if no span exists around the word, then one needs to be added as well.

Make sense?
--
View this message in context: http://old.nabble.com/surrounding-a-word-with-a-tag-%2B-a-dynamic-attribute-tp28447764p28461698.html
Sent from the Vim - General mailing list archive at Nabble.com.

Eric Marceau

unread,
Feb 28, 2024, 3:45:11 PMFeb 28
to vim_use
Hi David,

Your posting of the tip dates quite a while ago.

Any idea if that would still work for vim/gvim 8.1 ?

Is that added to the ~/.vim/vimrc  ?

Finally, is that action triggered by typing the sequence   ':TagSelection'   ?

Thank you,

Eric

Eric Marceau

unread,
Feb 28, 2024, 4:12:30 PMFeb 28
to vim_use
Also,

Can someone offer the modified version of that which would permit specifying the desired single tag-string (i.e. 'em') as a parameter for the function call, instead of the open-ended user entry of a string?

I visualize doing something like   ':TagSelection em' for the action to be applied without further interaction.  Is that possible?

Thank you in advance for anyone's contributing that modified code.

Eric

Eric Marceau

unread,
Feb 28, 2024, 4:15:14 PMFeb 28
to vim_use
I forgot to say that I was hoping that using the "repeat last" function, namely the ".", would apply that modified command at the next text location where a new string is highlited.

Eric

Eric Marceau

unread,
Feb 28, 2024, 6:56:19 PMFeb 28
to vim_use
So ...

I've added the function to my vimrc, and that works as "advertised".  Thank  you for that.

One annoying interraction is gvim always giving the " :'<,'> " prompt when I want to call the function in VISUAL mode.  

Is there something I could add to the function to suppress the '' '<,'> ", either a manual toggle off/on before I start using the command, or something to block that after the first instance of the function call, and auto-reinstate after about 1 minute after the last function call?

Eric
Reply all
Reply to author
Forward
0 new messages