elliptic00
elliptic00

Reputation: 447

abbreviation following command in Vim

I have following abbreviation in Vim.

iabbr forr  for(int i=0; i<len; i++)

It works fine, but I want the cursor move to left after the iabbr.

I have tried following so far:

iabbr forr for(int i=0; i<len; i++) :10h

10h - move the cursor to left 10 characters from current position

It does't work, it only prints all the text including :10h

Question:

how do I execute command after abbreviation? I went through the :help iabbr and did not find any info how to execute command.

Upvotes: 0

Views: 278

Answers (3)

Hugo Raguet
Hugo Raguet

Reputation: 428

I think what you are looking for is simply

iabbr forr  for(int i=0; i<len; i++)<ESC>10hi

<ESC> sets in normal mode, the last i sets back in insertion mode.

Upvotes: 0

Christian Brabandt
Christian Brabandt

Reputation: 8248

You can use expression mappings/abbreviations for this:

:iabbr <expr> forr  'for(int i=0; i<len; i++) '."\<esc>".repeat('h',10)

Read the help at :h map-expression for the details about this.

Alternatively, you could set a mark and jump to it:

:iabbr forr  for(int i=0; i<<C-O>malen; i++) <C-O>`a

Upvotes: 2

statox
statox

Reputation: 2886

I am not sure that is really possible actually.

A pretty crappy workaround would be:

iabbr forr  for(int i=0; i<len; i++) <Esc>hhhhhhhhhh

But that is terrible.

Instead you could be interested in plugins like Vim-snippets and Ultisnips or neosnippets: They allow to enter preformated pieces of code with some part ready to be changed. I think it is what you need.

Upvotes: 2

Related Questions