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.
Related
For example if a use ma mb etc to create some markers in buffer. Then i record a macro, then i want to execute macro to all these markers, how can i do to accomplish this goal
Maybe it can be completed by writing a function by lua or viml, or use some plugins or just a vim command is all accepted. I'd like someone can give a example function to make me learn more about neovim or vim
You can use getpos("'".mark_name) to obtain the position of a mark. And a macro can be executed with exe 'normal #'.macro_name.
Which gives:
function! s:exec(macro, marks) abort
for mark in split(a:marks, '\zs\ze')
call setpos('.', getpos("'".mark))
exe 'normal #'.a:macro
endfor
endfunction
command! -nargs=+ RunMacroOnMarks call s:exec(<f-args>)
Then if you've recorded a macro in register m and marked two positions a and b, it can be used with:
:RunMacroOnMarks m ab
Note: I've chosen to be explicit with the list of marks as we are likely to want to restrict the macro to a very limited number of registered positions.
I use marks.nvim to accomplish this.
then set keymap
local keymap = vim.api.nvim_set_keymap
keymap('n', '<leader>xx', '<Plug>(Marks-next)', { noremap = true, silent = true })
now you can combo with macro.
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.
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>
I am doing a vim plugin for personal use, and it triggers from toolbar.
when I click Plugin in GVIM toolbar, it create a sub menu called 'common'.
When I click common, it start check my vim settings and create sub menu, for instance, show/hide line number. But the following code has some problem, and cannot work. What's wrong with it?
amenu &Plugin.&Common :call <SID>createSubMenu()<CR>
fun! s:createSubMenu()
let isNum = &number
if isNum == '1'
amenu &Plugin.&Common.Hide\ Line\ &Number :set nonu
else
amenu &Plugin.&Common.Show\ Line\ &Number :set nu
endif
endfunction
--------------------- Resolved -----------------------
The code above cannot be used. It's completed wrong.
Please refer to Luc Hermitte's answer.
You need to :unmenu the old entry to remove it.
BTW, I already have a fully functional toggling/cycling engine for values, than also updates menus. It's done with a function from lh-vim-lib, see the test file to have examples of use, and the documentation for more explanations.
:amenu works like map commands: you must include <CR> at the end.
:amenu &Plugin.&Common.Hide\ Line\ &Number :set nonu<CR>
Also, <SID> and s: are not the same.
You should also probably scope isNum to your script with s:
An additional remark: why don't you have a single menu entry that proposes to toggle line numbering and calls :set nu!?
If I do either of the following two:
call search("searchString")
exec "/ searchString"
From a script, then vim does the search but does not highlight the results, even though hlsearch. Doing the same searches from outside a script highlights the results.
Just found out the answer myself:
call search(l:searchString)
call matchadd('Search', l:searchString)
The
feedkeys()
function is the key (pun intended):
call feedkeys("/pattern\<CR>")
or cleaner:
" highlights – or doesn’t – according to 'hlsearch' option
function SearcH(pattern)
let #/ = a:pattern
call feedkeys("/\<CR>")
endfunction
I know this is late. However when I searched for the answer to this problem this page came up. So I feel compelled to help fix it.
call search(l:searchString)
call matchadd('Search', l:searchString)
Did not work for me. (when run from inside a function) It did higlight the words I wanted to search for but n/N wouldn't cycle between them. Also when I performed a new search the "l:serachStirng" pattern still remained highlighted. This answer on this link worked much better
Vim search and highlighting control from a script
Which gave me:
let #/ = l:searchString
then run
normal n
outside the funciton (so the highlighting is done immediately without the user needing to press n)
To turn on, press ESC type :set hls
To turn off, press ESC type :set nohls
Found answer here:
http://vim.1045645.n5.nabble.com/highlighting-search-results-from-within-a-function-tt5709191.html#a5709193
```
One solution would be
function! XXXX()
execute '/this'
return #/
endfunction
and to use the following instead of ":call XXXX()".
:let #/ = XXXX()
```
I believe this works from inside a function
(to just enable highlighting and nothing more):
call feedkeys(":\<C-u>set hlsearch \<enter>")
You need to put this in your .vimrc file
" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
syntax on
set hlsearch
endif
The .vimrc file is usually located in your home directory, or you can find it using "locate .vimrc"