Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To insert mode from autocmd in Vim

Tags:

vim

I am trying to create an autocommand that will create boiler plate comments and code for new Java source files. As a simple start, I have added the following two lines (only a new line after the first line below in the actual file) to my .vim/ftplugin/java.vim:

autocmd BufNewFile *.java
\ exe "normal O/*\r" . expand('%:t') . "\t" . strftime("%B %d %Y") . 
"\r/\r\rpublic class " . expand('%:t:r') . " {\r\t\<Esc>i"

With the last part, \t\<Esc>i, I am trying to insert a tab and shift to insert mode automatically. I can't make the switch to insert mode work and have tried different permutations of two or more of \<Esc>, \<Insert>, "insert" , i and \t. What am I missing ? I am using VIM 7.2 on Linux.

like image 811
user847614 Avatar asked Dec 13 '22 10:12

user847614


1 Answers

You could use the :startinsert command. Just execute it after the :normal command:

autocmd! BufNewFile *.java
      \ exe "normal O/*\r" . expand('%:t') . "\t" . strftime("%B %d %Y") .
      \ "\r/\r\rpublic class " . expand('%:t:r') . " {\r\t" |
      \ startinsert!

Here's some more information on that: http://vimdoc.sourceforge.net/htmldoc/insert.html#:startinsert.

like image 126
Andrew Radev Avatar answered Dec 15 '22 00:12

Andrew Radev