Search code examples
vimcommanddashboardstartupneovim

Why does this Vim command not execute properly?


I am trying to write a simple dashboard for vim. When I try and centre the text, it does not work. If I type each command in manually it works. But when I use it in a command it doesn't work. Here is the code. My issue is the line

normal! VG:center.
function! DoSomethingIfNoArgs()
    if argc()
        return
    endif

  " Start a new buffer ...
  enew

  " ... and set some options for it
  setlocal
        \ textwidth=174
        \ bufhidden=wipe
        \ buflisted
        \ nocursorcolumn
        \ nocursorline
        \ nolist
        \ nonumber
        \ norelativenumber
        \ noswapfile

  " Now we can just write to the buffer, whatever you want.
  call append('.', ["",                                                                       
              \"",
              \"",
              \"",
              \" |                                                                     |",
              \" |       ████ ██████           █████      ██                     |",
              \" |      ███████████             █████                             |",
              \" |      █████████ ███████████████████ ███   ███████████   |",
              \" |     █████████  ███    █████████████ █████ ██████████████   |",
              \" |    █████████ ██████████ █████████ █████ █████ ████ █████   |",
              \" |  ███████████ ███    ███ █████████ █████ █████ ████ █████  |",
              \" | ██████  █████████████████████ ████ █████ █████ ████ ██████ |",
              \"                                                                       ",
              \"                                                                       ",
              \"                                                                       "])
  
  normal! VG:center<cr>
    
  " No modifications to this buffer
  setlocal nomodifiable nomodified

  " When we go to insert mode start a new buffer, and start insert
  nnoremap <buffer><silent> e :enew<CR>
  nnoremap <buffer><silent> i :enew <bar> startinsert<CR>
  nnoremap <buffer><silent> o :enew <bar> startinsert<CR>

endfunction
autocmd VimEnter * call DoSomethingIfNoArgs()

If I type VG to highlight the text and then :centre. Everything works.

But if I use

:normal! VG:center 

this doesn't seem to do anything.

What am I missing here.


Solution

  • :normal :center doesn't work because : is not a normal-mode command, it switches from Normal mode to command line.

    IMO the entire approach is wrong (not vimmy enough). You don't need to do VG to select the entire text. Actually you don't need to select text at all. Instead run :center over a range of lines. Do you want to center the entire text? It's :%center% as a range specifier means 1,$, i.e. from the 1st line to the last one.

    Do you want to center from the current line to the end? It's :.,$center

    Inside a function you don't need to prepend : (inside a function every line is an Ex-command) so the command to center the entire text becomes just %center . That's all, no need for :normal or VG!