Vim: Configure vim to show autocomplete suggestions from current open files - vim

I had this vim configuration some time back which shows autocomplete suggestions on pressing Ctrl + N from all the files I have opened during a session, not just the current file I am editing. Unfortunately I lost the vimrc and now I can't figure out what was making it do so.
Searched over internet and found results related to omnicomplete. But I didn't understand how it works. Any help would be appreciated. Thanks.

From :help i_ctrl-n:
CTRL-N Find next match for words that start with the
keyword in front of the cursor, looking in places
specified with the 'complete' option. The found
keyword is inserted in front of the cursor.
From :help 'complete' (or <C-]> on 'complete'):
b scan other loaded buffers that are in the buffer list
Don't google, :help.

Related

How to back to editing file after :Ex

I'm editing a new file in NeoVim and I don't remember the path of the directory I want to save my file to, so I want to look at the directory tree before saving. I do this by typing :Ex, which takes me to Netrw.
I'm done looking at the directory tree and I want to go back to my file and and finish my work. However I don't know the command to get back from Netrw to the file, and none of the numerous solutions I looked up online worked. The help file didn't help either. The intuitive command would be :q, which works fine for help but doesn't work for this situation. I have also tried q, Q, gq, gQ, :visual and :vi as suggested by people online.
Right, after making this post I finally stumbled upon a working solution in a comment by another.anon.coward on How to go back when I run :Ex command in Vim
You could try :bunload to unload current buffer & go back to previous
I think you can just press Ctrl-6 to go back to the previous buffer. Also check the nvim help: :help CTRL-6.
You can use those default vim mappings for that:
CTRL-O - Go to older cursor position in jump list
CTRL-I - Go to newer cursor position in jump list
So when you'll be at netrw press <C-o> couple of times and it will bring you back to the place where you started.
To read more about jump commands: :help jump-motions

Vim Ctrl P take out my search highlighting. How to make the highlighting stay while in Ctrl P search mode

I usually search for the route in my route file and it will be highlighted.
However, when I start searching for it in Ctrl P, the highlighted word is not highlighted anymore. It really bother me to find back where exactly that word I was searching for due to the highlighting disappear in Ctrl P search mode.
I believe there is a way to reprogram this in vimrc. I tried google, no result.
Please help.
Well, this doesn't answer your exact question ;) however, it sounds like what you're trying to do is to use the highlight to help you search. Ctrl-p has a helpful keystroke C-\ which brings up a prompt where you can tell it to use the search term as the value for ctrl-p
so.. for you.
/searchforyourterm
C-p to bring up Ctrl-p
C-\ to bring up the special prompt
s to insert the searched word.
I'm not sure how to do what you're actually asking for, but perhaps the above will be what you really need :)
link to the help doc or run :h ctrl-p and search for paste.. :)

Jump to tag (Ctrl-]) stopped working

When I position cursor over a tag name (no matter at what position inside the tag), pressing Ctrl-] should jump to that tag. It used to work before, but now it seems my Vim has some problems in identifying where tagnames start and end.
For example when I position cursor at first character of usr_09.txt and press Ctrl-] it raises error: E426: tag not found: usr_09. When I place the cursor in the middle of {ident} tag and press Ctrl-] it raises: E149: Sorry, no help for {ident}. In both cases when I visually select whole usr_09.txt and only "ident" inside {ident}, pressing Ctrl-] works fine and jumps properly to their definitions.
What could be the source of these problems?
The <C-]> command uses the 'iskeyword' option to determine the characters that a tag is comprised of. It seem like you lost the . and got {} added to it.
You can reset the value to Vim's help default via
:setlocal iskeyword=!-~,^*,^\|,^\"
or retrigger processing of modelines (what Vim's help page use; see the last line) via
:doautocmd FileType
If this permanently affects the Vim help, check where it got last modified via
:verbose setlocal iskeyword?
and change / remove that wrong :set command.

How do you know what VIM help keyword to search?

I was trying to google how to search within a scope in VIM and found this link. Limiting search scope for code in Vim
I wouldn't imagine to come up with the keyword "/\%V" to search help in VIM help. So, I'm wondering how most people get help from VIM help, like in this case.
You did the right thing. Google is probably going to be your best bet. You can also use the vim help site, it has a search function built in: http://vimdoc.sourceforge.net/htmldoc/
General:
:help help-summary is a great document explaining how to search
effectively.
Using wildcard expansion
I've had the same frustration, but I just realised that you can use wildcard
expansion when searching.
In your case it wouldn't have helped, but possibly if you'd started with :h pattern and then searched in that page for key words related to what you were
trying to achieve you might have found it.
In my case I was looking for a page I'd been on before only the other night,
:h ex-cmd-index, (on a different machine, so I couldn't search my command
history) but I could only remember than it had to do with ex commands, and had
the word index in there. I spent 20 mins trying to find it unsuccessfully (I
eventually googled it just so I could then return to vim and look it up...). I
was trying ex + Tab and being shown hundreds of possibilities...
But with wildcard expansion, I can just do :h ex*index* and hit Tab and I
get a shortlist of two possibilities, ('ex-cmd-index' and 'ex-edit-index') one
of which was the one I wanted!
So as long as you have an idea of the order that things should appear in the
help topic, you should be able to narrow down the possibilities much faster.
Using :helpgrep
There is a in-built command :helpgrep which puts every line matching a
pattern into a quickfix list. Then you can jump through each line with the
:cnext and cprev commands.
Fuzzy search the help with fzf (and optionally ag-silver-searcher)
The fzf plugin has the command :Helptags to do a fuzzy search on... the
help tags. This is very nice.
In case all the above fails, I added a few functions to search the entire
contents (not just help-tags) of all the files in the vim's help doc directory
using ag-silver-searcher and fzf.
First is a function that I found on an issue on the fzf github repo. I have no
idea how it works, but the command that calls this function allows me to search
the contents of a directory using ag, which pumps the results into fzf:
" Ag: Start ag in the specified directory e.g. :Ag ~/foo
function! s:ag_in(bang, ...)
if !isdirectory(a:1)
throw 'not a valid directory: ' .. a:1
endif
" Press `?' to enable preview window.
call fzf#vim#ag(join(a:000[1:], ' '),
\ fzf#vim#with_preview({'dir': a:1}, 'right:50%', '?'), a:bang)
endfunction
" Ag call a modified version of Ag where first arg is directory to search
command! -bang -nargs=+ -complete=dir Ag call s:ag_in(<bang>0, <f-args>)
So I wrote a function (Help_Ag) that calls the above command, and supplies
the directory containing the vim help docs (141 .txt files, plus a handful of
other files (a README, the tag files themselves etc).
If we select a help page to open, we set it to non-nomodifiable. I noticed that
not all the tags worked, and some redirected to the wrong page (e.g. a tag like
*help-topic* would take me to a page called help if the cursor was on that
word, and take me to topic if the cursor was on that word, whereas the in
real help, I would be taken to help-topic). I found that if I opened the real
help, the tags would start behaving normally in files selected with ag+fzf. So
I simply open and close the real help to trigger this behaviour (it's too quick
to even see, so I'm happy enough with this hack):
function! Help_Ag()
let orig_file = expand(#%)
let v1 = v:version[0]
let v2 = v:version[2]
" search in the help docs with ag-silver-search and fzf and open file
execute "normal! :Ag /usr/share/vim/vim".v1.v2."/doc/\<cr>"
" if we opened a help doc
if orig_file != expand(#%)
set nomodifiable
" for some reason not all the tags work unless I open the real help
" so get whichever help was found and opened through Ag
let help_doc=expand("%:t")
" open and close that help doc - now the tags will work
execute "normal! :tab :help " help_doc "\<cr>:q\<cr>"
endif
endfunction
" get some help
command! H :call Help_Ag()

How to jump to the next tag in vim help file

I want to learn the vim documentation given in the standard help file. But I am stuck on a navigating issue - I just cannot go to the next tag without having to position the cursor manually. I think you would agree that it is more productive to:
go to the next tag with some
keystroke
press Ctrl-] to read corresponding
topic
press Ctrl-o to return
continue reading initial text
PS. while I was writing this question, I tried some ideas on how to resolve this. I found that searching pipe character with /| is pretty close to what I want. But the tag is surrounded with two pipe '|' characters, so it's still not really optimized to use.
Use the :tn and :tp sequences to navigate between tags.
If you want to look for the next tag on the same help page, try this search:
/|.\{-}|
This means to search for:
The character |
Any characters up to the next |, matching as few as possible (that's what \{-} does).
Another character |
This identifies the tags in the VIM help file.
If you want to browse tags occasionally only, without mapping the search string to keyboard,
/|.*|
also does the trick, which is slightly easier to type in than the suggested
/|.\{-}|
For the case, that the "|" signs for the links in the help file are not visible, you can enable them with
:set conceallevel=0
To establish this setting permanently, please refer to Defining the settings for the vim help file
Well, I don't really see the point. When I want to read everything, I simply use <pagedown> (or <c-f> with some terminals)
" .vim/ftplugin/help/navigate.vim
nnoremap <buffer> <tab> /\*\S\+\*/<cr>zt
?
Or do you mean:
nnoremap <buffer> <tab> /\|\zs\S\{-}\|/<cr><c-]>
?
You could simply remap something like:
nmap ^\ /<Bar><Bslash>zs<Bslash>k<Bslash>+<Bar><CR>
where ^\ is entered as (on my keyboard) Ctrl-V Ctrl-#: choose whatever shortcut you want.
This does a single key search for a | followed by one or more keyword characters and then a |. It puts the cursor on the first keyword character. The and bits are there due to the way map works, see
:help :map-special-chars
As an aside, I imagine that ctrl-t would make more sense than ctrl-o as it's a more direct opposite of ctrl-], but it's up to you. Having said that, ctrl-o will allow you to go back to before the search as well.

Resources