I am having trouble figuring out how to change the default trigger for fuzzy find within VIM.
The default trigger is :FZF
I would like the trigger to be :fzf
Thanks!
You can add your own mapping for fzf in your .vimrc.
nnoremap <silent> <C-p> :fzf<CR>
This will call :fzf when you issue Ctrl+P in normal mode.
You can create an abbrev for the command line
cnoreabbrev <expr> fzf getcmdtype() == ":" && getcmdline() == 'fzf' ? 'FZF' : 'fzf'
then if you type :fzf followed by enter or followed by space it will be automatically swapped to :FZF.
Related
I was trying to change my coc.nvim autocomplete key, and found this question in Stack Overflow, but the guy who answer this question doesn't explain really good how to customize it as you want, so I will explain it to help the NeoVim users that are racking the brain for this as I was.
Short Answer
If you want to bind Tab for autocompletion, paste this in your .vimrc or init.vim
inoremap <silent><expr> <tab> pumvisible() ? coc#_select_confirm() : "\<C-g>u\<TAB>"
inoremap <silent><expr> <cr> "\<c-g>u\<CR>"
Detailed answer
So, you have to make 2 insert mode remaps, in this case I will remap my completion to Tab key.
inoremap <silent><expr> <tab> pumvisible() ? coc#_select_confirm() : "\<C-g>u\<TAB>"
Obs: If you want to bind other key to autocomplete:
inoremap <silent><expr> [the key that you want to autocomplete] pumvisible() ? coc#_select_confirm() : "\<C-g>u\<TAB>"
Now, CoC will autocomplete with Tab key too, but Enter is also autocompleting, I want to bind Enter by just be Enter, not an autocompletion key.
In VimScript Enter is represented by <cr>
inoremap <silent><expr> <cr> "\<c-g>u\<cr>"
Obs:
inoremap <silent><expr> [this is the current autocompletion key] "\<c-g>u\[this is the bind that I am giving to the key]"
The example vim configuration gives some very useful tips for COC.
But specifically for you this is relevant:
" Use tab for trigger completion with characters ahead and navigate.
" NOTE: There's always complete item selected by default, you may want to enable
" no select by `"suggest.noselect": true` in your configuration file.
" NOTE: Use command ':verbose imap <tab>' to make sure tab is not mapped by
" other plugin before putting this into your config.
inoremap <silent><expr> <TAB>
\ coc#pum#visible() ? coc#pum#next(1) :
\ CheckBackspace() ? "\<Tab>" :
\ coc#refresh()
inoremap <expr><S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<C-h>"
" Make <CR> to accept selected completion item or notify coc.nvim to format
" <C-g>u breaks current undo, please make your own choice.
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
\: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
function! CheckBackspace() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~# '\s'
endfunction
I need to override/redefine Vim's search operator "/" to also execute "zszH" after the search to center the search results on the screen horizontally.
For example, I want to enter: /varchar and have the search results (i.e., the string "varchar") displayed in the middle of the scren horizontally.
I can do that now by manually entering "zszH" after each search, but that is very tedious.
You can use the CmdlineLeave event. Add the following to your vimrc
augroup RecenterSearch
autocmd!
autocmd CmdlineLeave [/?] call feedkeys('zszH', 't')
augroup END
Note: CmdlineLeave requires Vim 8.1
Or you can map <cr>:
cnoremap <expr> <cr> "\<cr>" . (getcmdtype() =~ '[?/]' ? "zszH" : '')
Some mappings which might be helpful:
nnoremap n nzszH
nnoremap N NzszH
If you do not have a new enough version on Vim then maybe look into 'wrap' or create a mapping
For more help see:
:h CmdlineLeave
:h :autocmd
:h feedkeys()
:h expression-mapping
:h getcmdtype()
In MacVim 8.0, ctrl-f brings up a search window instead of scrolling forward in a buffer by one full screen. ctrl-F (i.e. pressing the SHIFT key also) gives the same result.
Is there a troubleshooting step or a repair step I can take?
I don't know about the MacVim. But I had experienced same situation at window gvim.
I mean when I press "Ctrl+F" I don't want to see search window.
I found the below at "C:\Program Files (x86)\Vim\vim81\mswin.vim".
To resolve your problem, all you have to do is just comment the below line, that's it.
if has("gui")
" CTRL-F is the search dialog
noremap <expr> <C-F> has("gui_running") ? ":promptfind\<CR>" : "/"
inoremap <expr> <C-F> has("gui_running") ? "\<C-\>\<C-O>:promptfind\<CR>" : "\<C-\>\<C-O>/"
cnoremap <expr> <C-F> has("gui_running") ? "\<C-\>\<C-C>:promptfind\<CR>" : "\<C-\>\<C-O>/"
" CTRL-H is the replace dialog,
" but in console, it might be backspace, so don't map it there
nnoremap <expr> <C-H> has("gui_running") ? ":promptrepl\<CR>" : "\<C-H>"
inoremap <expr> <C-H> has("gui_running") ? "\<C-\>\<C-O>:promptrepl\<CR>" : "\<C-H>"
cnoremap <expr> <C-H> has("gui_running") ? "\<C-\>\<C-C>:promptrepl\<CR>" : "\<C-H>"
endif
I think what happened is that gvim 8.0 has a mapping for <Ctrl-f> in mswin.vim, but the previous version of gvim does not have that mapping in mswin.vim
I use both versions (on different computers), and so got surprised by the <Ctrl-f> mapping in the gvim 8.0 version.
My vimrc sources mswin.vim because I like the copy-paste functionality.
if you are using Vscode, change "<C-f>" from false to true.
I would like Vim to place my cursor in the middle of screen after search. I have achieved for *, #, n and N commands with the following lines in .vimrc
nmap * *zz
nmap # #zz
nmap n nzz
nmap N Nzz
My question is: how to map / and ? the same way? I.e. I would like to position cursor after some text has been found using
/some-text-to-find-forward
?some-text-to-find-backward
Edit: Threw away my initial answer as it was too much of a kludge. Here's a much better solution.
function! CenterSearch()
let cmdtype = getcmdtype()
if cmdtype == '/' || cmdtype == '?'
return "\<enter>zz"
endif
return "\<enter>"
endfunction
cnoremap <silent> <expr> <enter> CenterSearch()
The way this works is to remap Enter in command-line-mode to a custom expression.
The function performs the current search followed by zz if the command-line is currently in a search. Otherwise it just executes whatever command was being done.
It's not very pretty, but
:nnoremap / :execute "normal! /\<lt>cr>zz"<c-left><right>
will get the job done. (Puts an :execute "normal! /" command on the commandline, then adds a <cr>zz to the end to it so that you automatically zz when you issue the command. The final <c-left><right> just steps into the search pattern at the right spot
Solution from Randy Morris but as a oneliner:
cnoremap <silent><expr> <enter> index(['/', '?'], getcmdtype()) >= 0 ? '<enter>zz' : '<enter>'
I've used Vim to code my Go script for a month, and I'm pretty comfortable with :Import and :Drop vim command when it comes to importing and unimporting any packages.
However, I've more and more tired of typing such a long word which includes Capital letter so frequently, so I came to think that if I can use :i and :d other than :Import and :Drop, I'd be satisfied even more. However, I'm not sure whether I can define such a command in Vim, since it involves 1) turning into command-line mode, and 2) taking a variable which is determined dynamically in typing.
So for example, if I import encoding/csv package, all I want to type in is :i encoding/csv, etc... Can I define those commands?
I stumbled upon this question, but I cannot get what it is doing nor I'm not sure whether the answer applies to this question in the first place. Can I solve the issue by replacing all of w and W on the linked post to i or d?
Also, does it differ from the case that doesn't take a variable (say, Fmt command to go-format the script)?
Thanks.
EDIT 2: As pointed out by glts below, it's better to use cnoreabbrev or cnorea as pointed out in this answer.
Better working example (paste into ~/.vimrc):
cnorea <expr> i ((getcmdtype() is# ':' && getcmdline() is# 'i')?('Import'):('i'))
cnorea <expr> d ((getcmdtype() is# ':' && getcmdline() is# 'd')?('Drop'):('d'))
EDIT: Simple answer. Just using cabbrev or ca (command abbreviation) seems to work:
WORKING EXAMPLE (Paste this in ~/.vimrc):
ca i Import
ca d Drop
Working on vim 7.3, Ubuntu 64 bit.
ORIGINAL ANSWER (more complex):
According to http://vim.wikia.com/wiki/Replace_a_builtin_command_using_cabbrev :
You can use :command to define your own commands, but user-defined commands must start with an uppercase letter to avoid confusion with built-in commands.
So, using :command, you can probably use :I and :D, but not :i and :d.
It goes on to say:
Suppose you have a user-defined :E command that you want to use to override the default :e command. You could do the following:
:cabbrev e <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'E' : 'e')<CR>
The (getcmdtype()==':' && getcmdpos()) makes sure the replacement happens only in the first column of the command line (i.e. not later in the line, where it is most likely NOT intended to be used as a command, and not on the search line, which is also affected by cabbrev).
If you do this a lot, it would be useful to define a function to do it for you. Use this to quickly and easily define lowercase abbreviations for whatever command you want:
function! CommandCabbr(abbreviation, expansion)
execute 'cabbr ' . a:abbreviation . ' <c-r>=getcmdpos() == 1 && getcmdtype() == ":" ? "' . a:expansion . '" : "' . a:abbreviation . '"<CR>'
endfunction
command! -nargs=+ CommandCabbr call CommandCabbr(<f-args>)
" Use it on itself to define a simpler abbreviation for itself.
CommandCabbr ccab CommandCabbr
This not only creates the function, but also provides the (lowercase!) command :ccab to define such abbreviations "on the fly".
So using a function looks like one way to go if you want to use lowercase :i and :d.
The cmdalias.vim - Create aliases for Vim commands plugin allows you to set up lowercase aliases, e.g.:
:Alias i Import
A simple solution:
nnoremap <leader>i :Import<Space>
nnoremap <leader>d :Drop<Space>
In normal mode, <leader>i populates the command-line with
:Import | <--- cursor
ready for you to type in or <tab>-complete an argument.
Better:
nnoremap <leader>i :Import <C-d>
nnoremap <leader>d :Drop <C-d>
<leader>i populates the command-line with
:Import | <--- cursor
and shows a list of possible completions.
Even better:
set wildcharm=<C-z>
nnoremap <leader>i :Import <C-z>
nnoremap <leader>d :Drop <C-z>
Supposing you have wildmenu already setup, <leader>i populates the command-line with
:Import firstpackagenameinthelist
with the wildmenu open and ready for <tab>bing.