Skip to main content
1 of 10
Mega Bang
  • 319
  • 1
  • 12

I need help updating complex code from vim8 to vim9

About three years ago, I had gathered chunks of code from the net and made my init.vim config file. Now, with vim9 released, I am unable to convert it.

And it's down to you folks whether you can help me. You are my last hope.

Yes, I've read the documentation and whatnot. Still, I don't get it. Most of the code in my init.vim file are complex while I can do only simpler ones.

if !exists("g:os")
    if has("win64") || has("win32") || has("win16")
        let g:os = "Windows"
    else
        let g:os = substitute(system('uname'), '\n', '', '')
    endif
endif

set title
lua vim.opt.titlestring = [[%f - %{v:progname} (%{tabpagenr()} of %{tabpagenr('$')})]]

function! TabLcd()
    let current_tab = tabpagenr()
    tabdo lcd %:p:h
    execute 'tabnext' current_tab
endfunction

nnoremap <silent> <leader>lcd :call TabLcd()<cr>
nnoremap <leader>cd :execute 'cd' getcwd(-1)<cr>

autocmd BufNewFile *.cpp if filereadable(expand("~/Documents/library/normal.cpp")) | execute 'r ~/Documents/library/normal.cpp | 1d' | else | execute 'echohl Todo | echom "Missing file ~/Documents/library/normal.cpp" | echohl None' | endif
autocmd FileType cpp nnoremap <F7> :r ~/Documents/Library/

tnoremap <Esc> <C-\><C-n>

if has('nvim')
    augroup terminal_setup | au!
        autocmd TermOpen * nnoremap <buffer><LeftRelease> <LeftRelease>i
        " more stuff
    augroup end
endif

function! TermWrapper(command) abort
    if !exists('g:split_term_style') | let g:split_term_style = 'vertical' | endif
    if g:split_term_style ==# 'vertical'
        let buffercmd = 'vnew'
    elseif g:split_term_style ==# 'horizontal'
        let buffercmd = 'new'
    else
        echoerr 'ERROR! g:split_term_style is not a valid value (must be ''horizontal'' or ''vertical'' but is currently set to ''' . g:split_term_style . ''')'
        throw 'ERROR! g:split_term_style is not a valid value (must be ''horizontal'' or ''vertical'')'
    endif
    exec buffercmd
    if exists('g:split_term_resize_cmd')
        exec g:split_term_resize_cmd
    endif
    exec 'term ' . a:command
        exec 'setlocal nornu nonu'
    exec 'startinsert'
endfunction

command! -nargs=0 Compile call TermWrapper(printf('g++ -std=c++17 -O2 -Wall %s -o %s.out', expand('%'), expand('%:r')))
autocmd FileType cpp nnoremap <F9> <Esc>:w <bar> Compile<CR>
autocmd FileType cpp imap <F9> <Esc>:w <bar> Compile<CR>

command! -nargs=0 Run call TermWrapper(printf('./%s.out', expand('%:r')))
autocmd FileType cpp nnoremap <F10> :Run<CR>

command! -nargs=0 CompileAndRun call TermWrapper(printf('g++ -std=c++17 -O2 -Wall %s -o %s.out && ./%s.out', expand('%'), expand('%:r'), expand('%:r')))
autocmd FileType cpp nnoremap <F12> <Esc>:w <bar> CompileAndRun<CR>
autocmd FileType cpp imap <F12> <Esc>:w <bar> CompileAndRun<CR>

command! -nargs=1 -complete=file CompileAndRunWithFile call TermWrapper(printf('g++ -std=c++17 -O2 -Wall %s -o %s.out && ./%s.out < %s', expand('%'), expand('%:r'), expand('%:r'), <q-args>))
autocmd FileType cpp nnoremap <F5> <Esc>:w <bar> CompileAndRunWithFile 
autocmd FileType cpp imap <F5> <Esc>:w <bar> CompileAndRunWithFile 

command! -nargs=1 -complete=file RunWithFile call TermWrapper(printf('./%s.out < %s', expand('%:r'), <q-args>))
autocmd FileType cpp nnoremap <F4> :RunWithFile 

func! Hack() abort
    if exists("b:rwfcmd1") && b:rwfcmd1
        nnoremap <buffer> <F4> :RunWithFile 
    nnoremap <buffer> <F5> <Esc>:w <bar> CompileAndRunWithFile 
    imap <buffer> <F5> <Esc>:w <bar> CompileAndRunWithFile 
    echo "HackerCup Special Deactivated"
        let b:rwfcmd1 = 0
    else
        nnoremap <buffer> <F4> :RunWithFile ~/Downloads/ > output.txt && cat output.txt<s-left><s-left><s-left><s-left><s-left><left>
    nnoremap <buffer> <F5> <Esc>:w <bar> CompileAndRunWithFile ~/Downloads/
    imap <buffer> <F5> <Esc>:w <bar> CompileAndRunWithFile ~/Downloads/
    echo "HackerCup Special Activated"
        let b:rwfcmd1 = 1
    endif
endfunc

com -nargs=0 Cup call Hack()
com -nargs=0 Hackercup let hack_tab = tabpagenr()|tabdo call Hack()|execute 'tabnext' hack_tab

cnoreabbrev cup Cup
cnoreabbrev hackercup Hackercup

let g:split_term_style = 'horizontal'
let g:split_term_resize_cmd = 'resize 30'
set splitbelow

au TextYankPost * silent! lua vim.highlight.on_yank {higroup="IncSearch", timeout=300}

au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif

" type :messages to see all the changed files
set autoread
autocmd FocusGained,BufEnter,CursorHold,CursorHoldI *
            \ if mode() !~ '\v(c|r.?|!|t)' && getcmdwintype() == '' | checktime | endif
autocmd FileChangedShellPost *
  \ echohl WarningMsg | echom "File " . expand('<afile>') . " changed on disk. Buffer reloaded." | echohl None

" No highlight after search - triggers on motion or going into insert mode
noremap <expr> <Plug>(StopHL) execute('nohlsearch')[-1]
noremap! <expr> <Plug>(StopHL) execute('nohlsearch')[-1]

fu! HlSearch()
    let s:pos = match(getline('.'), @/, col('.') - 1) + 1
    if s:pos != col('.')
        call StopHL()
    endif
endfu

fu! StopHL()
    if !v:hlsearch || mode() isnot 'n'
        return
    else
        sil call feedkeys("\<Plug>(StopHL)", 'm')
    endif
endfu

augroup SearchHighlight
au!
    au CursorMoved * call HlSearch()
    au InsertEnter * call StopHL()
augroup end
Mega Bang
  • 319
  • 1
  • 12