How to put a constant prefix before each new line in vim as you type?

Well, this is a rather complicated desire. This is because most of my fortran lines contain a “call” statement, and I'm tired of entering a call, call, call, call ...

I would like to have the following:

  • Each time I press the enter button, the line “call” is added to the next line, automatically added in front.
  • If I click the tab, the tab is added before the string call (so that I can back out)
  • If I am at the beginning of the line and I press back once, it will delete the call record, but leave the tabs. Likewise, it would be nice if he could automatically delete the record if I type "if", "do", "enddo" and everything related to it

Did you know that such a thing already exists, perhaps, and if you have any hints or similar scripts that I can extract, that would be greatly appreciated.

For example, in a C-style comment, every time I press Enter, an asterisk is automatically added at the beginning of the line. Where is the code that does this (I assume this is plugin functionality, not hardcoded in vim)?

+5
source share
2 answers

I followed your hint at C comments and came up with the following:

:set formatoptions+=ro
:set comments+=s:call,m:call,e:call

"", . , "", "" . , , .

>> Ctrl + T . "" , Ctrl + W Backspace.

:

:iab ,, call
+3

, <CR> <BS>, , - , .

, , , , , , . . , . , myki , .

vimrc .

"" When pressed 'return' in insert mode:
"" <Esc>: Exit from Insert Mode to Normal Mode.
"" o: Add a new line below the current one and set Insert Mode.
"" call <Esc>: Write literal 'call ' and exit from Insert Mode.
"" A: Set cursor at end of line and enter Insert Mode to begin writting.
inoremap <cr> <Esc>ocall <Esc>A

function! SpecialTab()

    "" Get cursor position.
    let cursor_pos = getpos('.')

    "" If line is empty, insert a tab, update current position and finish
    let line_len = strlen( getline( line('.') ) ) 
    if empty( getline( line('.') ) ) || line_len == 1
        s/\%#/\t/   
        let cursor_pos[2] += 1
        call setpos( '.', cursor_pos )
        return
    endif

    "" Search for a line beginning with 'call', omitting spaces. If found
    "" insert a tab at the beginning of line.
    if match( getline( line('.') ), "\s*call" ) != -1
        s/^/\t/
    else
        "" Insert a normal tab in current cursor position. I cannot use
        "" the regular <Tab> command because function would entry in a 
        "" infinite recursion due to the mapping.
        s/\%#\(.\)/\1\t/
    endif

    "" Set cursor column in the character it was before adding tab character.
    let cursor_pos[2] += 2
    call setpos( '.', cursor_pos )
endfunction

"" Map the tab character.
inoremap <Tab> <Esc>:call SpecialTab()<cr>:startinsert<cr>

function! SpecialBackspace()
    "" Do nothing if line is empty.
    if empty( getline( line('.') ) ) 
        return
    endif

    "" Get cursor position.
    let cursor_pos = getpos( '.' )

    "" If cursor is not in first column press 'delete' button and done.
    if col('.') > 1 
        execute "normal \<Del>"
        return
    endif

    "" Search for 'call' string. If found delete it and set cursor in
    "" previous position.
    if match( getline( line('.') ), "\s*call" ) != -1
        s/call//
        let cursor_pos[2] = 1 
        call setpos( '.', cursor_pos )
        return
    endif

    "" A line with one character is a special case. I delete the complete
    "" line.
    let line_len = strlen( getline( line('.') ) )
    if line_len == 1
        s/^.*$//
        return
    endif

    "" If cursor is in first column, delete character. Messy behavior, I
    "" think :-/
    if col('.') == 1
        s/^.//
    endif
endfunction

"" Map the 'backspace' character.
inoremap <BS> <Esc>:call SpecialBackspace()<cr>:startinsert<cr>
0

All Articles