简体   繁体   中英

Enter insert mode between two tags after command in vim

I'm looking to change the following vim command so that after entering the :%s/temp123//g part it will put me into insert mode between the \\begin and \\end tags.

 inoremap \\beg \begin{temp123}<enter><enter>\end{temp123}<enter><esc>:%s/temp123//g<left><left>

I have been able to use :startinsert to get into go into insert mode after entering the search/replace command but I am not able to place the cursor between the \\begin and \\end tags.

Any help/solutions/improvements would be appreciated.

TL;DR: can't.

 :star[tinsert][!] Start Insert mode just after executing this command. Works like typing "i" in Normal mode. When the ! is included it works like "A", append to the line. Otherwise insertion starts at the cursor position. Note that when using this command in a function or script, the insertion only starts after the function or script is finished. This command does not work from :normal. 

I was trying to get the following line working:

nnoremap <MiddleMouse> :set paste<cr>:startinsert<MiddleMouse><esc>

What this means is that all of my commands that I put after :startinsert instead run immediately before :startinsert and then :startinsert runs and changes the mode to insert (Note: this appears to hold true for using i instead of :startinsert as well).

My next step was to try to make a nested function, where I had one function that calls another function, and the second function runs :startinsert and then returns to the first function, which then completes the paste:

function! Paste()
  call InsertMode()<cr>
  :set paste<cr>
  <S-Insert>
  :set nopaste<cr>
  <esc>
endfunction
function! InsertMode()
  :startinsert<cr>
endfunction
nnoremap <MiddleMouse> call Paste()<cr>

But this did not work either. I also tried using the "+p and "*p registers without :startinsert with nnoremap <MiddleMouse> :set paste<cr>"+p:set nopaste<cr> , but this again just pastes directly as if I were typing it in, it does not enter insert mode first. I am willing to believe this would work on a version of Vim compiled with +clipboard, but that is not the version I have. Link to my original question and answer

Yep, you're looking for an interactive substitution, but :s/new/old/g c doesn't let you edit each match. For this kind of work, switch to the gn command + . recipe:

First search for {temp123} (or whatever you wish to replace) with /
Then press cgn to change the visually selected next match
Once done, go back to normal mode to commit your edits.
If the next edit is the same as the last, press only . otherwise cgn
Rise and repeat.

For more ideas, see this vimcast: gn command

This is the solution I am now using.

function TexBegin()
    let currline = line(".")
    call inputsave()
    let tagname = input("enter tag name: ")
    call inputrestore()                            
    call setline(currline, "\\begin{" . tagname . "    }")
    normal o
    normal o
    call setline(currline + 2, "\\end{" . tagname .     "}")
    normal l
    startinsert
    normal o<Esc>
endfunction 
inoremap  \\beg <Esc>:call TexBegin()<CR>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM