Get Vimwiki working with SnipMate (the vim plugins) - vim

My issue is that <Tab> completion for SnipMate does not work when I'm in a vimwiki file. I am testing by typing APACHE<Tab> in insert mode, but I don't get the snippet as I do when I'm in, say, .vimrc.
And even though i_<Tab> doesn't work in a vimwikie file, i_<C-R><Tab> does. I get a list of available snippets. Strange.
To make it stranger (well, to me at least), here's the partial output of :imap in a vimwiki file:
i <Tab> * <C-G>u<C-R>=snipMate#TriggerSnippet()<CR>
i <C-R><Tab> * <C-R>=snipMate#ShowAvailableSnips()<CR>
I think these are correct, and in other files where <Tab> completion does work, the mappings are the same.
oh, and in case you are thinking to suggest putting let g:vimwiki_table_auto_fmt = 0 in my .vimrc as suggested in http://code.google.com/p/vimwiki/wiki/ChangeLog, I tried it and it doesn't solve my problem. That's the only suggestion I found in my search, so that's why I'm here.
I don't know how much of my .vimrc you would want, so I'm just going to give you the ones I think are relevant to keep this question short:
" Vundle: The plugin manager for vim.
set runtimepath+=~/.vim/bundle/vundle/
call vundle#rc()
Bundle 'gmarik/vundle'
" SnipMate: TextMate snippets for vim.
Bundle "MarcWeber/vim-addon-mw-utils"
Bundle "tomtom/tlib_vim"
Bundle "honza/snipmate-snippets"
Bundle "garbas/vim-snipmate"
" Vimwiki: A personal wiki for vim.
Bundle 'vim-scripts/vimwiki'
let g:vimwiki_hl_headers = 1
let g:vimwiki_hl_cb_checked = 1
let g:vimwiki_folding = 1
let g:vimwiki_fold_lists = 1
let s:vimwiki = {}
let s:vimwiki.path = '~/Vimwiki/wiki'
let s:vimwiki.path_html = '~/Vimwiki/html'
let s:vimwiki.ext = '.md'
let s:vimwiki.syntax = 'markdown'
let s:vimwiki.maxhi = 1
let s:vimwiki.diary_rel_path = 'Journal/'
let s:vimwiki.diary_index = 'index'
let s:vimwiki.diary_header = 'Journal'
let s:vimwiki.diary_sort = 'asc'
let g:vimwiki_list = [s:vimwiki]
Thanks
Edit:
I don't know why I forgot about this while writing the above. The output of :imap above is incomplete. It also shows:
i <Tab> *#vimwiki#tbl#kbd_tab()
Regardless of the value of g:vimwiki_table_auto_fmt. It's probably the source of my problem.. and if so I'd like to get rid of it from .vimrc somehow.

The # in the :imap output shows that Vimwiki defines a buffer-local <Tab> mapping that overrides snipMate's global one. A
:iunmap <buffer> <Tab>
should fix that. You can put that command into ~/.vim/after/ftplugin/vimwiki.vim to make it permanent. But apparently Vimwiki allows customization:
" Table mappings
if g:vimwiki_table_mappings
inoremap <expr> <buffer> <Tab> vimwiki#tbl#kbd_tab()
inoremap <expr> <buffer> <S-Tab> vimwiki#tbl#kbd_shift_tab()
endif
So, :let g:vimwiki_table_mappings = 0 in your .vimrc should work, too.

Used Ingo Karkat's solution for the longest time but Shift-Tab in insert mode is broken again.
" New fix.
" SOURCE: https://github.com/vimwiki/vimwiki/issues/1268#issuecomment-1312192484
" SOURCE[*g:vimwiki_key_mappings*]: https://github.com/vimwiki/vimwiki/blob/0629b39815c97f1e4ee31e26faa6891b0e13d2d5/doc/vimwiki.txt#L3671
"
" Disable table mappings:
let g:vimwiki_key_mappings =
\ {
\ 'table_mappings': 0,
\ 'table_format': 0,
\ }

Related

vim-go/synstastic Errors not automatically displaying on save

I noticed that when I save a file and I have a syntax error in my code, the error quickfix window does not automatically appear. I recently rebuilt my system and simply copied over my .vim/ directory along with the same .vimrcfile. I've done this before and have never had any issues. However, if I manually enter :GoErrCheck or GoBuild, the window shows up. What gives?
Here is my .vimrc
execute pathogen#infect()
syntax on
filetype plugin indent on
set nu
set completeopt-=preview
set encoding=utf-8 " Set default encoding to UTF-8
set autoread
set laststatus=2
set noswapfile " Don't use swapfile
set nobackup " Don't create annoying backup files
"
nmap <Leader>t :TagbarToggle<CR>
autocmd FileType qf wincmd J
"CtrlP Settings
let g:ctrlp_show_hidden = 1
let g:neocomplete#enable_at_startup = 1
let g:go_highlight_functions = 1
let g:go_highlight_methods = 1
let g:go_highlight_fields = 1
let g:go_highlight_structs = 1
let g:go_highlight_interfaces = 1
let g:go_highlight_operators = 1
let g:go_highlight_build_constraints = 1
let g:molokai_original = 1
let mapleader=","
colorscheme molokai
Readme file in vim-go explains its usage with syantastic
Sometimes when using both vim-go and syntastic Vim will start lagging while saving and opening files. The following fixes this:
let g:syntastic_go_checkers = ['golint', 'govet', 'errcheck']
let g:syntastic_mode_map = { 'mode': 'active', 'passive_filetypes': ['go'] }
Another issue with vim-go and syntastic is that the location list window that contains the output of commands such as :GoBuild and :GoTest might not appear. To resolve this:
let g:go_list_type = "quickfix"
In this issue
One recommendation is to remove the lines
let g:syntastic_go_checkers = ['golint', 'govet', 'errcheck']
let g:syntastic_mode_map = { 'mode': 'active', 'passive_filetypes':
and use
let g:syntastic_go_checkers = ['govet', 'errcheck', 'go']
instead
Assuming you are talking about errors shown by syntastic, this issue is probably similar to the one discussed here : vim-go with syntastic
Synastic doesn't check Go files on save by default (anymore). Add this
to your .vimrc to make that happen:
let g:syntastic_go_checkers = ['go']

vim - UltiSnips not working with YCM

when I use YouCompleteMe and UltiSnips together, I encounter 2 errors:
I can type a snip pattern and the YCM dropdown menu recognizes it, however when I tab down to the snip in the menu, I cannot expand it. I have tried remapping g:UltiSnipsExpandTrigger to several different key (c-l, c-j, c-k, c-l, F5) and still have no success. I have yet to see UltiSnips actually expand a snippet.
When I open a new file, e.g. a.py, YCM does not recognize ultisnippets in the file. I am basically trying to recreate the usage demonstrated in the nice gif on the ultisnips git page. Typing #! is not recognized by YCM, but if I open an already saved python file, #! will be recognized.
Perhaps these two problems are related.
Thanks for your time.
I solved this problem with the following configurations:
UltiSnips:
let g:UltiSnipsSnippetsDir = $HOME.'/.vim/UltiSnips/'
let g:UltiSnipsSnippetDirectories=["UltiSnips"]
let g:UltiSnipsExpandTrigger="<c-j>"
let g:UltiSnipsJumpForwardTrigger="<c-j>"
let g:UltiSnipsJumpBackwardTrigger="<c-k>"
let g:UltiSnipsListSnippets="<c-h>"
YCM:
let g:ycm_complete_in_comments = 1
let g:ycm_seed_identifiers_with_syntax = 1
let g:ycm_collect_identifiers_from_comments_and_strings = 1
I had the same issue with making both of them work together.
here is my .vimrc, you might find what you need:
" ----------------------------------------------------------------------------
" Autocompletion & Snippets Plugins
" ----------------------------------------------------------------------------
if has('nvim')
runtime! python_setup.vim
endif
Plug 'Valloric/YouCompleteMe', { 'do': './install.sh --clang-completer' }
Plug 'SirVer/ultisnips'
Plug 'ladislas/vim-snippets'
" YouCompleteMe setup
let g:ycm_autoclose_preview_window_after_completion = 1
let g:ycm_filetype_blacklist={'unite': 1}
let g:ycm_min_num_of_chars_for_completion = 1
nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR>
" UltiSnips setup
let g:UltiSnipsExpandTrigger='<c-k>'
let g:UltiSnipsJumpForwardTrigger='<c-k>'
let g:UltiSnipsJumpBackwardTrigger='<c-s-k>'
let g:UltiSnipsSnippetsDir=plugDir.'/vim-snippets/UltiSnips'
Hope this helps.

How to get 'filetype' of a buffer specified by a number?

I'm writing plugin for VIM and i need to know a filetype of buffer specified by a number. But filetype is an option - how can i get specified buffer option programmatically? I can get buffer name via bufname() vimscript function - but i did not found any function that allows me to get an option. Maybe it's some trick with bufdo?
The getbufvar() function cannot just retrieve variables, but also buffer-local options (prefixed with &):
:let bufFiletype = getbufvar(bufNr, '&filetype')
Prefix the option with a &. E.g.
:echo &tabstop
:let myts = &tabstop
:echo myts
:let &ts = myts
See :help :let-&.
Other functions that you might need on your quest are:
getbufvar() / setbufvar()
getwinvar() / setwinvar()
gettabvar() / settabvar()
gettabwinvar() / settabwinvar()
See :help var-functions.
For simple cases you could also do something like this:
function! SetTabstopFromBufname(name)
execute 'silent edit' bufname(a:name)
let myts = &tabstop
silent edit #
let &l:tabstop = myts
endfunction
call SetTabstopFromBufname('vimrc')

UltiSnips and YouCompleteMe

I have bundles ultisnips and youcompleteme installed on my macvim.
The problem is that ultisnips doesn't work because tab is bound by ycm.
I tried putting let g:UltiSnipsExpandTrigger = "<s-tab>" so that I can trigger the snippet completion with shift-tab, but it doesn't work for some unknown reason. I could use caps as the trigger, but so far I've found no way to do that.
Do any of you use those two add-ons together?
What can I do to make shift-tab work?
Can you recommend another key to trigger snippets?
Another option is using the SuperTab plugin:
" if you use Vundle, load plugins:
Bundle 'ervandew/supertab'
Bundle 'Valloric/YouCompleteMe'
Bundle 'SirVer/ultisnips'
" make YCM compatible with UltiSnips (using supertab)
let g:ycm_key_list_select_completion = ['<C-n>', '<Down>']
let g:ycm_key_list_previous_completion = ['<C-p>', '<Up>']
let g:SuperTabDefaultCompletionType = '<C-n>'
" better key bindings for UltiSnipsExpandTrigger
let g:UltiSnipsExpandTrigger = "<tab>"
let g:UltiSnipsJumpForwardTrigger = "<tab>"
let g:UltiSnipsJumpBackwardTrigger = "<s-tab>"
Here YouCompleteMe is bound to a different combination Ctrln, but then that combination is bound to tab through SuperTab. UltiSnips and SuperTab play nice together, so you can then just bind UltiSnips to tab directly and everything will work out.
Try this suggestion on a page from the YouCompleteMe issue tracker. In your .vimrc:
let g:UltiSnipsExpandTrigger="<c-j>"
While this setting will make expanding a snippet share the default mapping for jumping forward within a snippet, it simulates TextMates' behavior as mentioned in the UltiSnips help tags.
Since I've mapped my Caps Lock key to Ctrl, this mapping works pretty smoothly.
copy the following code to your vimrc, and enjoy. This function will handle all issues between YCM and UltiSnips.
function! g:UltiSnips_Complete()
call UltiSnips#ExpandSnippet()
if g:ulti_expand_res == 0
if pumvisible()
return "\<C-n>"
else
call UltiSnips#JumpForwards()
if g:ulti_jump_forwards_res == 0
return "\<TAB>"
endif
endif
endif
return ""
endfunction
au BufEnter * exec "inoremap <silent> " . g:UltiSnipsExpandTrigger . " <C-R>=g:UltiSnips_Complete()<cr>"
let g:UltiSnipsJumpForwardTrigger="<tab>"
let g:UltiSnipsListSnippets="<c-e>"
" this mapping Enter key to <C-y> to chose the current highlight item
" and close the selection list, same as other IDEs.
" CONFLICT with some plugins like tpope/Endwise
inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
i have this in my vimrc
"" YouCompleteMe
let g:ycm_key_list_previous_completion=['<Up>']
"" Ultisnips
let g:UltiSnipsExpandTrigger="<c-tab>"
let g:UltiSnipsListSnippets="<c-s-tab>"
thats what i did on my first try, but i misspelled UltiSnips with Ultisnips.. oh well, worked out in the end!
I personally chose to not use <tab> with YouCompleteMe but navigate it manually.
So I added this to my .vimrc:
let g:ycm_key_list_select_completion=[]
let g:ycm_key_list_previous_completion=[]
which simply disables the tab key for YCM. Instead you use the movement keys (arrows or CTRL-N/CTRL-P) and select the entry with CR. UltiSnips works default with tab.
Just putting together answers by Michaelslec, Joey Liu and along with solutions I found in this issue thread and this guy's vimrc, I now have this which solved pretty much all problems.
function! g:UltiSnips_Complete()
call UltiSnips#ExpandSnippet()
if g:ulti_expand_res == 0
if pumvisible()
return "\<C-n>"
else
call UltiSnips#JumpForwards()
if g:ulti_jump_forwards_res == 0
return "\<TAB>"
endif
endif
endif
return ""
endfunction
function! g:UltiSnips_Reverse()
call UltiSnips#JumpBackwards()
if g:ulti_jump_backwards_res == 0
return "\<C-P>"
endif
return ""
endfunction
if !exists("g:UltiSnipsJumpForwardTrigger")
let g:UltiSnipsJumpForwardTrigger = "<tab>"
endif
if !exists("g:UltiSnipsJumpBackwardTrigger")
let g:UltiSnipsJumpBackwardTrigger="<s-tab>"
endif
au InsertEnter * exec "inoremap <silent> " . g:UltiSnipsExpandTrigger . " <C-R>=g:UltiSnips_Complete()<cr>"
au InsertEnter * exec "inoremap <silent> " . g:UltiSnipsJumpBackwardTrigger . " <C-R>=g:UltiSnips_Reverse()<cr>"
Based on Siegfried's answer, I am using the following which seems more natural:
let g:ycm_key_list_select_completion = ['<C-j>']
let g:ycm_key_list_previous_completion = ['<C-k>']
let g:UltiSnipsExpandTrigger = "<C-l>"
let g:UltiSnipsJumpForwardTrigger = "<C-j>"
let g:UltiSnipsJumpBackwardTrigger = "<C-k>"
I also use the c-hjkl bindings somewhere else (switching from a pane to another), but that would only be in normal mode, so there's no problem.
Although I know this post is a little old, I have my own function that is a little more optimized than the one given above:
function! g:UltiSnips_Complete()
call UltiSnips#ExpandSnippetOrJump()
if g:ulti_expand_or_jump_res == 0
if pumvisible()
return "\<C-N>"
else
return "\<TAB>"
endif
endif
return ""
endfunction
Of course, if you just keep the settings that Joey Liu provided and then just use this function everything will work just perfectly!
EDIT: Also, I use another function to increase back-stepping functionality between YouCompleteMe and UltiSnips. I'll show you what I mean:
function! g:UltiSnips_Reverse()
call UltiSnips#JumpBackwards()
if g:ulti_jump_backwards_res == 0
return "\<C-P>"
endif
return ""
endfunction
Then just put this in your .vimrc:
au BufEnter * exec "inoremap <silent> " . g:UltiSnipsJumpBackwardTrigger . " <C-R>=g:UltiSnips_Reverse()<cr>"
As well as let g:UltiSnipsJumpBackwardTrigger="<s-tab>" and your set!
I use both of them together. By default YouCompleteMe binds <Tab> and <Down> to select the next completion item and also <S-Tab> and <Up> to select the previous completion item. You can change the YouCompleteMe bindings with the g:ycm_key_list_select_completion and g:ycm_key_list_previous_completion options. Note that the names of these options were recently changed when the option was changed from a single string to a list of strings.
While Many answer works fine in this post, I just want to say that the problem is caused by key binding collision between YCM and UltiSnip, while YCM support UltiSnip snippets by default, it takes the default UltiSnip expand trigger <tab> as its completion select key, so UltiSnip snippets will not be expaned by <tab>. Give them different key binding will solve the problem, I personally use <c-n and <c-p> for YCM and use the default <tab> for UltiSnip. You can get more details with help youcompleteme doc in vim.
I installed the UltiSnips plugin after the YouCompleteMe plugin so I thought they were conflicting, but in reality I had something more interfering:
set paste
Make sure to remove that from .vimrc if it's present.
I use ; to expand UltiSnips, it's so nifty for me
let g:UltiSnipsExpandTrigger = ";"
I use kj. This is what is in my .vimrc:
let g:UltisnipsExpandTrigger="kj".
It rarely happens that I run into word that has kj in it. If this is the case I would just wait a couple of seconds after typing k and that type j.
As mentioned by others, mapping C-j to ultisnips works great.
let g:UltiSnipsExpandTrigger="<c-j>"
Now, if you go a bit further and install xcape and use
xcape -e "Shift_L=Control_R|J"
You unleash the power of using just the shift key for utlitsnips.

Vim script: Buffer/CheatSheet Toggle

I want to make a vim cheat sheet plugin. It's real simple:
I want to toggle my cheatsheets. A vertsplit toggle, like Taglist or NERDTree.
I want the cheatsheet to be filetype specific. So I toggle my c++ cheatsheet when I have opened a .cpp file.
I want the cheatsheet to be horizontally split. So it shows two files, my syntax cheat sheet and my snippet trigger cheat sheet.
I already have a collection of these cheatsheets, in vimhelp format, but now I have to manually open them.
I haven't really done any vim scripting, but I imagine this would be really simple to put together. I'm sorta sick of googling unrelated codesnippets, so what I'm asking here is:
Could anyone give me a short sum-up of what I need to learn in regards to vim scripting to piece this together. What I have a hard time finding is how to toggle the buffer window.
If you know any intro tutorials that covers the material I need to get this up and running, please provide a link.
tx,
aktivb
The function below may not do exactly what you want, and I haven't tested it, but it should give you some ideas.
The main idea is that the function reads the filetype of the current buffer (you can test this by typing :echo &ft) and then sets the path of the appropriate cheat sheat. If it exists, this path is then opened (read-only and non-modifiable) in a split window. You can then call this function any way you wish, for example by mapping it to the {F5} key as shown.
I'm not sure about the toggling possibilities (is this really easier than just closing the split window?) but you could look at the bufloaded() function, which returns whether or not a given file is currently being accessed.
function! Load_Cheat_Sheet()
let l:ft = &ft
if l:ft == 'html'
let l:path = 'path/to/html/cheat/sheet'
elseif l:ft == 'c'
let l:path = 'path/to/c/cheat/sheet'
elseif l:ft == 'tex'
let l:path = 'path/to/tex/cheat/sheet'
endif
if l:path != '' && filereadable(l:path)
execute ':split +setlocal\ noma\ ro ' l:path
endif
endfunction
map <F5> :call Load_Cheat_Sheet()<CR>
Hope this helps. Just shout if anything is unclear, or you want to know more.
I had forgotten about this until I got a notice about Eduan's answer. Since I posted this question I've done quite a bit of vim scripting, including getting this to work:
let g:cheatsheet_dir = "~/.vim/bundle/cheatsheet/doc/"
let g:cheatsheet_ext = ".cs.txt"
command! -nargs=? -complete=customlist,CheatSheetComplete CS call ToggleCheatSheet(<f-args>)
nmap <F5> :CS<CR>
" strip extension from complete list
function! CheatSheetComplete(A,L,P)
return map(split(globpath(g:cheatsheet_dir, a:A.'*'.g:cheatsheet_ext)),
\ "v:val[".strlen(expand(g:cheatsheet_dir)).
\ ":-".(strlen(g:cheatsheet_ext) + 1)."]")
endfun
" specify cheatsheet or use filetype of open buffer as default
" instead of saving window status in a boolean variable,
" test if the file is open (by name). If a boolean is used,
" you'll run into trouble if you close the window manually with :wq etc
function! ToggleCheatSheet(...)
if a:0
let s:file = g:cheatsheet_dir.a:1.g:cheatsheet_ext
else
if !exists("s:file") || bufwinnr(s:file) == -1
let s:file = g:cheatsheet_dir.&ft.g:cheatsheet_ext
endif
endif
if bufwinnr(s:file) != -1
call ToggleWindowClose(s:file)
else
call ToggleWindowOpen(s:file)
endif
endfun
" stateless open and close so it can be used with other plugins
function! ToggleWindowOpen(file)
let splitr = &splitright
set splitright
exe ":vsp ".a:file
exe ":vertical resize 84"
if !splitr
set splitright
endif
endfun
function! ToggleWindowClose(file)
let w_orig = bufwinnr('%')
let w = bufwinnr(a:file)
exe w.'wincmd w'
exe ':silent wq!'
if w != w_orig
exe w_orig.'wincmd w'
endif
endfun
Thought I would add to Goulash's answer.
I think in order to implement the toggle you would simply use some if statements and a global variable.
let g:cheatsheet_toggle_on=0
if (g:cheatsheet_toggle_on == 0)
" Turn the cheatsheet on
" Also make sure to know that the toggle is on:
let g:cheatsheet_toggle_on=1
elseif (g:cheatsheet_toggle_on=1
" Do whatever you need to turn it off, here
endif
Hope this figures out that logic. :)

Resources