Vim how to configure complex autocomplete using SuperTab or alternative - vim

I'm using SuperTab. By default it allow me autocomplete all early used parts of text. But, I also need to autocomplete some languages or filepathes.
Currently, I can use C-x C-u for omnicomplete, but this is not handy. Is it possible to configure all autocompletes at tab ?
For example, I want to autocomplete PHP function name str_ . If function not exists, then script must use default vim autocomplete.
Any help would be appreciated! Thanks a lot!
UPDATED
This is my SuperTab configuration
" Supertab
let g:SuperTabDefaultCompletionType = "context"
let g:SuperTabCompletionContexts = ['s:ContextText', 's:ContextDiscover']
let g:SuperTabContextTextOmniPrecedence = ['&omnifunc', '&completefunc']
let g:SuperTabContextDiscoverDiscovery =
\ ["&completefunc:<c-p>", "&omnifunc:<c-x><c-o>"]
This use only omnicompletion for php, but not autocomplete another text in current buffer, which only works now when I using <c-p> .
When I adding next lines to .vimrc :
autocmd FileType *
\if &omnifunc != '' |
\call SuperTabChain(&omnifunc, "<c-p>")
\endif
<c-p> not works

SuperTab provides two mechanisms: context completion and completion chaining, both documented in its help page.
In short, the former can switch to a different (like omni) completion if the text before the cursor matches a certain pattern (like a variable or class name). The latter first attempts omnicompletion and falls back to normal insert completion if there were no matches.

Related

Highlight copied area in Vim

I would like to mimic a nice effect found in the game Vim Adventures: When a yank command is done, I would like the yanked area to be highlighted (let's say in red) for a second to show me that my selection was correct.
For example, yy would highlight the current line in red one second, then I would know what was selected.
For Neovim and Vim
There is a plugin named vim-highlightedyank for this, which works both for Vim and Neovim. Install it and it just works. You can also set the highlight duration by adding the following config:
" set highlight to 1000 ms
let g:highlightedyank_highlight_duration = 1000
Neovim only
If you are using nvim 0.5+, they have made this little feature builtin in this pull request.
No plugin is needed in this case. Just install nvim 0.5+ and add the following config to your init.vim:
augroup highlight_yank
autocmd!
au TextYankPost * silent! lua vim.highlight.on_yank({higroup="IncSearch", timeout=700})
augroup END
See Neovim's documentation on this feature for more.
For Neovim >= 0.7
Neovim includes a function for highlighting a selection on yank, see Neovim Lua documentation.
With Neovim version >= 0.7, you can also Neovim API to define autocommand to use it with TextYankPost event. Add this configuration in your init.lua file :
vim.api.nvim_create_autocmd('TextYankPost', {
group = vim.api.nvim_create_augroup('highlight_yank'),
desc = 'Hightlight selection on yank',
pattern = '*',
callback = function()
vim.highlight.on_yank { higroup = 'IncSearch', timeout = 500 }
end,
})
One vimmer (not me) saw this question and just wrote a plugin for that. Note that this is not a simple solution and probably not for those who prefer to keep vim minimum, but it works like a charm. Also note that this was not meant to be a serious work, thus you can't necessarily seek for a steady support.
plugins to install
kana/vim-operator-user : Lets user define their own operators.
thinca/vim-operator-sequence : "Operator to do two or more operators."
osyo-manga/vim-operator-highlight : the plugin for this.
Use your preferred way to install these plugins.
Settings
Let's say you want to use Meta+y to "yank and highlight". Write the following settings in your .vimrc.
noremap <expr> <Plug>(yank-highlight) operator#sequence#map("y", "\<Plug>(operator-highlight)")
nmap <A-y> <Plug>(yank-highlight)
vmap <A-y> <Plug>(yank-highlight)
Change <A-y> to whatever keymap (just y might not work) you'd like to use : <Leader>y can be nice, for example.
The highlight can automatically be cleared. Write the following:
let g:operator#highlight#clear_time=2.0
This will clear that highlight in about 2 secs. The interval also depends on :set updatetime?, which defaults to 4000(ms), so if this doesn't seem to clear the highlighting, try setting updatetime to a smaller value.
I use the plugin by markonm called hlyank.vim.
I opened the plugin and changed the time as it only blinked the color on the screen so changed it from 200 to 800. In the autoload folder, I opened hlyank.vim and edited the following lines:
call timer_start(800, {-> matchdelete(id, winid)})
else
call timer_start(800, {-> matchdelete(id)})

Change emmet's leader key in Vim

Is it possible to to change the way you expand emmet code into HTML?
The default is ctrl+y+, and thats about two too many keys for my liking.
In sublime I just enter the emmet code and hit tab and it expands it. Is there a way to have it do this vim?
Maybe you should add the line below to your .vimrc:
let g:user_emmet_expandabbr_key = '<Tab>'
:help emmet-customize explains how to customize Emmet's mappings. As a "noob", you owe it to yourself to get used to Vim's documentation.
You could add the line below to ~/.vimr/after/ftplugin/html.vim:
inoremap <buffer> <tab> <plug>(emmet-expand-abbr)
However, Emmet has a lot of features accessible via a number of mappings all using the same "leader", <C-y> so I'm not sure it is a good idea to take the direction you want to take.
I added to my configuration the following mapping
imap ,, <C-y>,
So for example, if I type div or any other emmet expression, then I just type ,, and it gets completed.
i'm using let g:user_emmet_leader_key='<A-e>'
use some special key (<C-e>, <A-e>...) or vim will wait for a emmet-vim command every time you type , or \

How do i add a shortcut to print a variable to stdout?

While coding, i want to select a variable and automatically add a print statement like below by using a custom shortcut:
foo = 5
bar = foo * 5
If i place my cursor on bar and use this shortcut, i want the output to change to:
foo = 5
bar = foo * 5
p "bar = #{bar}"
Can anyone help me in adding this shortcut to my vimrc based on the filetype (ruby, python, java etc)?
snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork. A modern alternative (that requires Python though) is UltiSnips. There are more, see this list on the Vim Tips Wiki.
There are three things to evaluate: First, the features of the snippet engine itself, second, the quality and breadth of snippets provided by the author or others; third, how easy it is to add new snippets.
function Print(p)
let tmp = a:p.' "'.expand("<cword>").' = #{'.expand("<cword>").'}"'
call append(line('.'), tmp)
endfunction
autocmd BufNewFile,BufRead *.py nmap <Leader>x :call Print("print")<CR>

Not able to hide <# and #> with parameters for clang_snippets=1 with clang_complete

I've set this on my .vimrc:
let g:clang_snippets=1
let g:clang_snippets_engine='clang_complete'
let g:clang_conceal_snippets=1
set conceallevel=2 concealcursor=inv
I don't know how conceal is expected to work, maybe the clang_complete's docs should have a tip for a specific setting to hide the snippets adorns.
How do I hide it? I'm using MacVim built with +conceal, but it's not working. This is my messy .vimrc by now.
NOTE:
I'm sticking with g:clang_snippets_engine='clang_complete' because it seems to be more smart than the snipMate parameter completion, switching to NORMAL mode is a wiser choice to navigate between parameters since I can use SuperTab completion for params in INSERT mode while being able to navigate through them with the same tab at NORMAL mode. snipMate engine was acting weird to me sometimes too, sometimes it switched to a parameter after a completion, sometimes not.
Also, I'm missing a final tab to go after the last parameter, right after the function call (snipMate does that), so I can just insert ; and hit Enter.
Disclaimer: This question is related with the issue at https://github.com/Rip-Rip/clang_complete/issues/176.
EDIT:
My problem was with this line at my .vimrc:
au BufNewFile,BufRead *.cpp set syntax=cpp11
I'm using C++11 Syntax Support and #xaizek has discovered and pointed it out as the problem in the comments bellow in the accepted response, it seems the root cause is the use of the syntax clear command in it.
According to :help 'concealcursor':
Sets the modes in which text in the cursor line can also be concealed.
When the current mode is listed then concealing happens just like in
other lines.
n Normal mode
v Visual mode
i Insert mode
c Command line editing, for 'incsearch'
So with concealcursor=iv you asked Vim to hide concealed text in insert and visual modes, but show it in normal mode. So just do:
:set concealcursor=inv

How to detect a function in Vim?

If the cursor is in somewhere within a very long function, is there a way to let Vim tell the user in which function he/she is editing?
By the way, I use taglist but seems that taglist does not auto update where you are even if you have moved the cursor to a different function.
The taglist plugin provides this feature. The function in which
the cursor is currently positioned is highlighted automatically in the list of
functions of taglist.
Make sure that Tlist_Auto_Highlight_Tag is not equal 0 to enable this feature.
'updatetime' defines the time of no activity which must elapse before
taglist highlights the current function. Default is 4 seconds.
:help taglist.txt See section "Highlighting the current tag"
As a quick test:
Type :TlistHighlightTag to force taglist to highlight the current function.
If this works I suppose that you have disabled the automatic highlighting in any
way (see Tlist_Auto_Highlight_Tag).
As an addition to Habi's answer, if you want to do it without using taglist, you can quite easily define a function that will work it out. It depends what language you're programming in, but for C-like languages, you can do this:
nmap ,f call ShowFuncName()
" Show the name of the current function (designed for C/C++, Perl, Java etc)
fun! ShowFuncName()
let lnum = line(".")
let col = col(".")
echohl ModeMsg
echo getline(search("^[^ \t#/]\\{2}.*[^:]\s*$", 'bW'))
echohl None
call search("\\%" . lnum . "l" . "\\%" . col . "c")
endfun
Put that it in your vimrc and then press ,f to see the current function.
Taken from here.

Resources