search

Home  >  Q&A  >  body text

How to write functions in vim that can be interacted with on the command line?

nnoremap <leader>c :Ack '^class ' <c-r>=&path<cr><home><right><right><right><right>< ;right><right><right><right><right><right><right><right>

The general idea is to use Ack to search for pattern.

under the &path path.

The n*<right> in this mapping is so ugly. How can it be optimized?

After help from the community, I put the final method below:

" help input

function! AckClass()
    let l:classPattern = input('Pattern: ')
    execute " Ack '^class " . l:classPattern . "' " . &path
endfunction

command! AckClass call AckClass()
phpcn_u1582phpcn_u15822774 days ago572

reply all(1)I'll reply

  • PHPz

    PHPz2017-05-16 16:40:35

    I don’t know how far you want to interact

    function! ChangeFileencoding()
        let encodings = ['GB18030', 'GBK', 'cp936', 'utf-8', 'big5', "latin1", "euc-kr", "euc-jp", "ucs-bom", "shift-jis"]
        let prompt_encs = []
        let index = 0
        while index < len(encodings)
            call add(prompt_encs, index.'. '.encodings[index])
            let index = index + 1
        endwhile
        let choice = inputlist(prompt_encs)
        if choice >= 0 && choice < len(encodings)
            execute 'e ++enc='.encodings[choice].' %:p'
        endif
    endf

    This is my vimrc function that changes the encoding and opens the file. It will display a list of options for your reference

    reply
    0
  • Cancelreply