title: Returning to Normal Mode from Insert Mode after "execute normal"
body: <p>You can perform normal mode commands programmatically in Ex mode, via execute normal, e.g.</p>
  
  <pre><code>:execute "normal" "iNEWTEXT\&lt;Esc&gt;0"
  </code></pre>
  
  <p>This switches to insert mode (i), writes "NEWTEXT", escapes to normal mode (\&lt; Esc>), then moves to the start of the line (0).</p>
  
  <p>However, using a non-constant string, either a register or variable, the behavior is different.  For example, suppose you have the same command above saved on a line in any file (not necessarily a vimscript file):</p>
  
  <pre><code>iNEWTEXT\&lt;Esc&gt;0
  </code></pre>
  
  <p>You can then copy the text into any register (here, z) via <code>"zy$</code> and execute the register via <code>@z</code>.  This time, though, the output is different:</p>
  
  <pre><code>NEWTEXT\&lt;Esc&gt;0
  </code></pre>
  
  <p>After entering insert mode, the Escape is no longer treated as a special character, and is instead taken literally. Alternative forms like \e don't work either. Is there a way around this?  </p>
  
  <p><strong>EDIT:</strong> Using Ingo's answer, I created the the following function.  Basically, the use is for having a set of normal/insert commands embedded within the text of the file, and being able to execute them.    More commonly, something similar is used for <a href="https://stackoverflow.com/questions/14385998/how-can-i-execute-the-current-line-as-vim-ex-commands">running Ex commands from a line of text</a>, but I couldn't find anything that did this exact thing for normal and insert mode.  </p>
  
  <p>So, you'd have text like the following in your file:</p>
  
  <pre><code>jy10j10jpO\&lt;Esc&gt;jEll
  </code></pre>
  
  <p>When on that line, you could call the function or a remap, and the commands would execute (in this example, copying and pasting 10 lines, and moving 2 columns past the first word).  Ingo's alternatives are better for serious usage, namely sourcing commands from another file, having the command in the .vimrc, or a file-type specific option.  Macros saved by a session would work just as well, and are more practical than having commands scattered throughout a file.  In my case, I was syncing across multiple devices, and didn't want to have another file or clutter my vimrc with this very specific command, but didn't mind cluttering this specific file itself.  Think of this like a portable macro.</p>
  
  <pre><code>" Execute current line as Vim normal mode commands.
  nnoremap &lt;A-y&gt; :call EvaluateLineAsNormalModeCmd()&lt;CR&gt;
  
  function! EvaluateLineAsNormalModeCmd()
      let g:getCurrentLine = getline(".")
      "have to :execute twice: once to get the contents of the 
      "register inserted into a double-quoted string, and then once for 
      "the :normal to evaluate the string.
      execute 'execute "normal" "' . g:getCurrentLine  . '"'
  endfunction
  </code></pre>
  
  <p><strong>EDIT2/3:</strong> Here are two functions using Christian Brabandt's answer.  They work about the same but can put the user in insert mode at the end (whereas, based on my minimal information, 'i' in the other context is considered an incomplete command and not executed, and :startinsert can't be used in that situation).  PS: Please don't ask me what all those single and double quotes are doing, as I can't wrap my head around it O_o</p>
  
  <pre><code>function! EvaluateLineAsNormalModeCmd()
      normal! 0y$
      execute ':call feedkeys("'.@".'", "t")'
  endfunction
  
  function! EvaluateLineAsNormalModeCmd()
      let g:getCurrentLine = getline(".")
      execute ':call feedkeys("'.g:getCurrentLine.'", "t")'
  endfunction
  </code></pre>