There is something I cannot do with YouCompleteMe
that I could do when just using tags to navigate to a definition or declaration.
The desired behaviour is (taking into account that , is my leader key):
- ,g should attempt to go to the definition/declaration in the current buffer. If changes to the buffer are not saved, it should print an error "No write since last change" or something similar and do nothing.
- ,ds should open the definition/declaration in a vertical split, regardless of whether changes to the current buffer have been saved or not (since the original buffer will remain open).
- ,dt should open the definition/declaration in a new tab, regardless of whether changes to the current buffer have been saved or not (since the original buffer will remain open).
With ctags
, behaviour 1 was the standard Ctrl], and 2 and 3 could be achieved with:
" Open definition in new vertical split
map <leader>ds :vsp <CR>:exec("tag ".expand("<cword>"))<CR>
" Open definition in new tab
map <leader>dt :tab split<CR>:exec("tag ".expand("<cword>"))<CR>
However, YouCompleteMe
seems to behave differently. According to the official documentation:
The g:ycm_goto_buffer_command option
Defines where GoTo* commands result should be opened. Can take one of the following values: [ 'same-buffer', 'horizontal-split', 'vertical-split', 'new-tab', 'new-or-existing-tab' ] If this option is set to the 'same-buffer' but current buffer can not be switched (when buffer is modified and nohidden option is set), then result will be opened in horizontal split.
This means that if I define my new mappings as:
" Shortcuts for navigating to definitions using YouCompleteMe
map <leader>g :YcmCompleter GoToDefinitionElseDeclaration<CR>
" Open definition in new vertical split
map <leader>ds :vsp <CR>:exec("YcmCompleter GoToDefinitionElseDeclaration")<CR>
" Open definition in new tab
map <leader>dt :tab split<CR>:exec("YcmCompleter GoToDefinitionElseDeclaration")<CR>
I can't achieve any of the behaviours if the changes to the file have not been saved:
- ,g does not print an error, but instead opens the definition/declaration in a new horizontal split.
- ,ds does not open the definition/declaration in the new split directly, but instead creates the vertical split and then opens the definition/declaration in a horizontal split inside the vertical split.
- ,dt does not open the definition/declaration in the new tab directly, but instead creates the new tab and then opens the definition/declaration in a horizontal split inside the tab.
What settings/mappings should I set to achieve the desired behaviour?
g:ycm_goto_buffer_command
. What did you set it to?