VIM function to create a tag, how to center the cursor position after output

I tried using a simple function for XML echo tags:

func! SayTag() let tagName = input("Tag: ") return "<" . tagName . ">" . "<" . tagName . ">" endfunc 

And binding to:

imap \tag <CR>=SayTag()<CR>

But after the output, the cursor was after the tags, for example < TAG > < /TAG > _CURSOR_

How to set a dynamic cursor position?

+4
source share
2 answers

I do not really like the following solution, but I studied your problem a bit because I could not think of a simple solution:

 func! GetTag() call inputsave() let g:tagName = input("Tag: ") call inputrestore() endfunc imap \t <esc>:call GetTag()<CR>:exe "normal! i<".tagName."></".tagName.">"<CR>bba 

It should work fine, you can read the docs here (see the last example). By the way, if you plan to write a lot of XML or HTML, I would suggest you take a look at the following plugins:

They will save you a lot of information.

+2
source

Another possible implementation using a slightly nicer map.

 function! GetTag() let tag = input("Tag: ") execute "normal! i<".tag."></".tag.">" execute "normal! " . repeat('h', strlen(tag)+2) endfunction inoremap \tag <Co>:call GetTag()<enter> 

However, I strongly agree that you will save a lot of time using plugins designed for this kind of thing.

Edit: delete an unnecessary loop.

+2
source

Source: https://habr.com/ru/post/1379316/


All Articles