mjbrownie / swapit

vim plugin: Extensible Keyword Swapper
39 stars 8 forks source link

SwapList doesn't work in vimrc #20

Open bobpaul opened 9 years ago

bobpaul commented 9 years ago

I'm using Vundle to manage swapit and my other plugins, so maybe this is a vundle interaction, but my other plugins don't have this problem.

If I put at the end of my vimrc: SwapList CTrueFalse TRUE FALSE

I get an error when starting vim "Error detected while processing c:\users\redacted.vimrc: line 174: E492: Not an editor command: SwapList CTrueFalse TRUE FALSE"

If I source ~/.vimrc after vim has loaded then there's no error and the swaplist works. It seems that swapit's commands and variables aren't loaded until after an initial buffer is created? What's the recommended way to define swaplists so I don't have to manually load them every time I restart vim?

mjbrownie commented 9 years ago

Hi Bob, That is true of any vim :command's that are defined in the .vim/plugin folders (as is the case for SwapIt) . Your vimrc file is being executed first. Swapit has an autosourcing mechanism based around FileTypes that is in the help docs (note I haven't heard if it works on windows).

But for your case a workaround is possible using the VimEnter autocommand. Here are some examples

"unsafe version (will generate an error if swapit is not present) au VimEnter * SwapList animals aardvark badger cat dog elephant sloth "A safe (portable) version if you reuse your vimrc on other machines. au VimEnter * if exists(":SwapList") | SwapList animals aardvark badger cat dog elephant sloth | endif " Source a file containing a list of SwapList commands (if you get sick of typing "au VimEnter " au VimEnter \ if exists(":SwapList") | source c:/path/to/your/swaplists.vim | endif

mawkler commented 4 years ago

@mjbrownie Adding au VimEnter * SwapList animals aardvark badger cat dog elephant sloth to my ~/.vimrc seems to only work for the first buffer opened. Any other buffers don't have that SwapList.

I tried solving this by using BufEnter instead but that creates a new identical SwapList whenever I re-enter a buffer which causes a collision when trying to increment a word in the SwapList.

mjbrownie commented 4 years ago

hey sure 2015 me appears incorrect. There was a ClearSwapList command for a given buffer which could be wrapped in a function as a workaround. I was mostly using the generic list and filetype specific ones when I knocked this up.. a workaround is as follows...

eg in your .vimrc

fun SwapLists()
     ClearSwapList
     SwapList animals aardvark badger cat dog elephant sloth
endfun
autocmd BufEnter * call SwapLists()
mawkler commented 4 years ago

Thank you very much, that works! I was looking for something like ClearSwapList in the documentation but somehow missed it :)

Addisonbean commented 4 years ago

I'm getting a similar error, but when loading the builtin ftplugin files:

Error detected while processing /Users/me/.vim/plugged/swapit/after/ftplugin/html_swapit.vim:
line    2:
E492: Not an editor command: ClearSwapList
line    4:
E492: Not an editor command: SwapList input_types text checkbox radio hidden button submit file
line    5:
E492: Not an editor command: SwapList form_method post get
line    6:
E492: Not an editor command: SwapList target _blank _parent _self _top
line    7:
E492: Not an editor command: SwapList formats h1 h2 h3 h4 p strong em a
line    8:
E492: Not an editor command: SwapList layout div span img script
line    9:
E492: Not an editor command: SwapList atri  id class href src alt title
line   11:
E492: Not an editor command: SwapXmlMatchit formats layout
Press ENTER or type command to continue

It's worth mentioning I'm using vim-plug to install this, I don't know if that'd affect anything.

mjbrownie commented 4 years ago

@Addisonbean have you tried a simplified vimrc? (there may be a conflict with another plugin/vimrc setting

~/test.vim

call plug#begin()
Plug 'mjbrownie/swapit'
call plug#end()

vi -u ~/test.vim test.html

<h3></h3>

should be enough to see it working.

other than that :scriptnames might give you an idea if swapit.vim getting loaded. also :version might be of interest.

Addisonbean commented 4 years ago

Yep, it worked when I took everything else out. It turns out it was this mapping:

nnoremap <silent> <leader>rv :source $MYVIMRC<cr> | doautocmd BufRead.

It was trying to read the buffer during the vimrc loadings. I wanted that mapping to reload my vimrc then refresh the buffer, but apparently I had the command a little wrong, it should have been this:

nnoremap <silent> <leader>rv :source $MYVIMRC <bar> doautocmd BufRead<cr>.

Thanks for the help!