Vim function doesn't seem to be recognised - vim

I copied this in to .vimrc:
function! ClearAllButMatches()
let old = #c
let #c=""
%s//\=setreg('C', submatch(0), 'l')/g
%d _
put c
0d _
let #c = old
endfunction
vnoremap <leader>c :<c-u>call g:ClearAllButMatches()<cr>
It doesn't seem to work. When I use <leader>c, I am getting this:
E117: Unknown function: g:ClearAllButMatches
Any ideas why its happening?
note:
I have sourced my .vimrc several times.

Remove g: from the mapping or add it to the function name

Related

How to toggle (all) line numbers on or off

Let's say I have some combination of:
" one if not both is usually on
set number " could be on or off
set relativenumber " could be on or off
Is there a way to toggle these on/off without losing information (not knowing what is set -- i.e., I would like to make a simple keyboard shortcut to toggle the visibility of the current line-number selection)? For example if I have only rnu set and I do:
:set number!
It really doesn't help me at all, since I'll still have rnu set and there will still be a line-number column on the left. If so, how could this be done?
give this a try:
currently, I am mapping it to <F7> you can change the mapping if you like
I am using the global variable, you can change the scope if it is required
This function will disable all line-number displays and restore to the old line number settings.
function! MagicNumberToggle() abort
if &nu + &rnu == 0
let &nu = g:old_nu
let &rnu = g:old_rnu
else
let g:old_nu = &nu
let g:old_rnu = &rnu
let &nu = 0
let &rnu =0
endif
endfunction
nnoremap <F7> :call MagicNumberToggle()<cr>
The one liner solution
:nnoremap <silent> <C-n> :let [&nu, &rnu] = [!&rnu, &nu+&rnu==1]<cr>
To understand what happens try:
:echo [&nu, !&rnu]
&nu ............. gets the value of number
!&rnu ........... the oposite value of relative number
For more :h nu

Can't use ENTER in vim?

In my vimrc, I use ENTER to expand UltiSnips:
let g:UltiSnipsExpandTrigger = '<CR>'
But this cause I can't use ENTER in insert mode, how to solve it?
I created a solution that works for me based on some tricks described here: https://github.com/SirVer/ultisnips/issues/376
let g:ulti_expand_or_jump_res = 0
function! ExpandSnippetEx(val)
let snippet = UltiSnips#ExpandSnippetOrJump()
if g:ulti_expand_or_jump_res > 0
return snippet
else
return a:val
endif
endfunction
inoremap <expr> <CR> \<C-R>=ExpandSnippetEx('<C-V><CR>')
This attempts to expand the snippet, and if no snippet is found, instead of doing nothing, it inserts an ENTER.

vim: mapping key to comment with indentation

Before I was using this in .vimrc to use '+' and '-' to comment the code has been highlighted:
noremap <silent>+ :s/^/\/\/ /<CR>:noh<CR>
noremap <silent>- :s/^\/\/ //<CR>:noh<CR>
so when I comment the code it becomes:
int main() {
// int x = 0;
// int y = 0;
return 0;
}
I want to comment codes with indents, like the following:
int main() {
// int x = 0;
// int y = 0;
return x;
}
However I try to use:
noremap <silent>+ :le<CR>:s/^/\/\/ /<CR>==:noh<CR>
noremap <silent>- :le<CR>:s/^\/\/ //<CR>==:noh<CR>
The commenting result looks like:
int main() {
// int x = 0;
int y = 0;
return x;
}
I was wondering what is wrong with my mapping and how I can fix it...
In addition, is there a "smarter" way to do this?
(I am willing to learn to write a mapping instead of installing some plugins such as NERDcommenter)
Thanks,
Update:
Maybe I didn't put my questions clear so there are some answers below didn't get what I meant...
Many thanks to everyone who attempted to answer my question, I found Ben's solution is easiest to understand for beginners and rc0r's has less lines as well as it works with multiple level of indentation (even though I don't know what it really does, I will do some self-studying later).
So now the code I use looks like:
if has("autocmd")
autocmd FileType c,cpp,java,verilog noremap <silent><Leader>. :s:^\(\s*\):\1// :<CR>:noh<CR>
autocmd FileType c,cpp,java,verilog noremap <silent><Leader>, :s:^\(\s*\)// :\1:<CR>:noh<CR>
autocmd FileType sh,zsh,python,perl,ruby noremap <silent><Leader>. :s:^\(\s*\):\1# :<CR>:noh<CR>
autocmd FileType sh,zsh,python,perl,ruby noremap <silent><Leader>, :s:^\(\s*\)# :\1:<CR>:noh<CR>
autocmd FileType vim noremap <silent><Leader>. :s:^\(\s*\):\1" :<CR>:noh<CR>
autocmd FileType vim noremap <silent><Leader>, :s:^\(\s*\)" :\1:<CR>:noh<CR>
autocmd FileType asm noremap <silent><Leader>. :s:^\(\s*\):\1; :<CR>:noh<CR>
autocmd FileType asm noremap <silent><Leader>, :s:^\(\s*\); :\1:<CR>:noh<CR>
autocmd FileType vhdl,sql noremap <silent><Leader>. :s:^\(\s*\):\1-- :<CR>:noh<CR>
autocmd FileType vhdl,sql noremap <silent><Leader>, :s:^\(\s*\)-- :\1:<CR>:noh<CR>
endif
and it works well enough to me.
I'm not an expert for setting up vim maps, but with a slightly modified search and replace pattern your initial map should do the trick:
noremap <silent>+ :s:^\(\s*\):\1// :<CR>:noh<CR>
noremap <silent>- :s:^\(\s*\)// :\1:<CR>:noh<CR>
The above regex used for commenting searches for a variable number of space characters at the beginning of a line (^\(\s*\)) and replaces the found pattern with itself (using a back reference \1) followed by the characters for the comment (\1//). The second regex shown above removes the comment chars in a similar way: search for variable number of spaces followed by comment characters (^\(\s*\)//) and replace all that with just the spaces (back reference \1).
You say you're using the map on a visual selection.
The problem, then, is that your initial :left command in your mapping ends visual mode, so that the next :s command only acts on the current line instead of the entire visual selection.
You have a couple options:
Use gv to return to visual mode on the last visual selection
Use :'<,'>s instead of plain :s to explicitly set the range to the last visual selection
Either way, you should probably change your noremap to xnoremap instead, so that it only applies in visual mode. You can make another separate nnoremap mapping for normal-mode on a single line without the gv or '<,'>
There's no doubt a better way to do it :) but the following
noremap <silent>+ :le<CR>gv:'<,'>s/^\/\/ ../\/\/ /<CR>gv==:noh<CR>
changes
int main() {
// int x = 0;
// int y = 0;
return 0;
}
to
int main() {
// int x = 0;
// int y = 0;
return 0;
}
Didn't have time to work out the uncomment one yet nor work out how best not to repeat the bits in the substitue
Your command adds // to the begining of the line because you forgot to take whitespace into consideration:
:s/^\s*/\/\/ /
But you should use a plugin instead.

Show function name in status line

I edit a large C, C++, or Java file, say, about 15000 lines, with pretty long function definitions, say, about 400 lines. When the cursor is in middle of a function definition, it would be cool to see the function name in Vim status line.
When we set :set ls=2 in Vim, we can get the file path (relative to the current directory), line number, etc. It would be really cool if we could see the function name too. Any ideas how to get it?
Currently I use [[ to go to start of the function and Ctrl-O to get back to the line I'm editing.
To show current function name in C programs add following in your vimrc:
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
map f :call ShowFuncName() <CR>
Or if you need the "f" key, just map the function to whatever you like.
You can use ctags.vim for this, it will show the current function name in the title or status bar.
SOURCE: https://superuser.com/questions/279651/how-can-i-make-vim-show-the-current-class-and-method-im-editing
Based on #manav m-n's answer
The 'n' flag in search() won't move the cursor, so a shorter version of this with the same functionality would be:
fun! ShowFuncName()
echohl ModeMsg
echo getline(search("^[^ \t#/]\\{2}.*[^:]\s*$", 'bWn'))
echohl None
endfun
map f :call ShowFuncName() <CR>
Reference: run :help search()
Having investigated this and the accepted solution, I believe the simplest solution is:
Install Universal Ctags https://ctags.io/
Install Tagbar https://github.com/preservim/tagbar
call tagbar#currenttag() when setting your statusline, e.g. in .vimrc:
:set statusline=%<%f\ %h%m%r%=%{tagbar#currenttag('%s\ ','','f')}%-.(%l,%c%V%)\ %P
Note that:
Spaces must be slash-escaped in the strings you pass to currenttag()...and it's even different between running the command inside vim and putting it in your .vimrc?? Anyway, spaces can be weird, and they're probably something you want when outputting the function name.
It took me some digging but the default statusline is
:set statusline=%<%f\ %h%m%r%=%-14.(%l,%c%V%)\ %P
There are several plugins for status line or on-demand with a mapping, e.g.:
http://www.vim.org/scripts/script.php?script_id=1094
http://www.vim.org/scripts/script.php?script_id=2805
http://www.vim.org/scripts/script.php?script_id=1553
My solution is as follows:
set stl=%f%h%m%r\ %{Options()}%=%l,%c-%v\ %{line('$')}
fu! PlusOpt(opt)
let option = a:opt
if option
return "+"
else
return "-"
endif
endf
fu! Options()
let opt="ic".PlusOpt(&ic)
let opt=opt." ".&ff
let opt=opt." ".&ft
if &ft==?"cpp" || &ft==?"perl"
let text = " {" . FindCurrentFunction() . "}"
let opt= opt.text
endif
return opt
fu! FindCurrentFunction()
let text =''
let save_cursor = getpos(".")
let opening_brace = searchpair('{','','}','bWr', '', '', 100)
if opening_brace > 0
let oldmagic = &magic
let &magic = 1
let operators='operator\s*\%((\s*)\|\[]\|[+*/%^&|~!=<>-]=\?\|[<>&|+-]\{2}\|>>=\|<<=\|->\*\|,\|->\|(\s*)\)\s*'
let class_func_string = '\(\([[:alpha:]_]\w*\)\s*::\s*\)*\s*\%(\~\2\|'.operators
let class_func_string = class_func_string . '\|[[:alpha:]_]\w*\)\ze\s*('
let searchstring = '\_^\S.\{-}\%('.operators
let searchstring = searchstring.'\|[[:alpha:]_]\w*\)\s*(.*\n\%(\_^\s.*\n\)*\_^{'
let l = search(searchstring, 'bW', line(".")-20 )
if l != 0
let line_text = getline(l)
let matched_text = matchstr(line_text, class_func_string)
let matched_text = substitute(matched_text, '\s', '', 'g')
let text = matched_text
endif
call setpos('.', save_cursor)
let &magic = oldmagic
endif
return text
endfunction
I'm actually attempting to match the C/C++/Java allowed names for functions. This generally works for me (including for overloaded operators) but assumes that the opening { is at column 0 on a line by itself.
I just noticed today that it fails if included in a namespace {}, even if otherwise formatted as expected.
I use https://github.com/mgedmin/chelper.vim for this. It doesn't needs a tags file, instead it parses the source code on the fly.
Based on #solidak solution (which was already based on another one :)
I wanted to always show the function name in the bottom of the terminal.
But I had some problems with very large function which I solved that way:
fun! ShowFuncName()
echohl ModeMsg
echo getline(search("^[^ \t#/]\\{2}.*[^:]\s*$", 'bWn'))[:winwidth('%')-3]
echohl None
endfun
augroup show_funcname
autocmd CursorMoved * :call ShowFuncName()
augroup end

How to use abbreviations in Vim with arguments?

In Vim, you can make it so each time you write "FF" on insert mode changes to some code by using:
:iab FF for ( int i = 0 ; i < n ; i++ )
But is there any way to use this with arguments? Something like C's #defines, so if I write
FF(e, 10)
It becomes:
for ( int e = 0 ; e < 10 ; e++ )
Take a look at SnipMate (a vim plugin). You won't get arguments, but upon expansion of an abbreviation, it allows you to tab through modifiable areas. In the for example, you'll be brought to the i first, can edit it to be e, and it will change it to e in all areas of the for declaration. Then simply tab to the next area you'd like to change.
From the docs:
snipMate.vim aims to be an unobtrusive, concise vim script that implements some of TextMate's snippets features in Vim. A snippet is a piece of often-typed text that you can insert into your document using a trigger word followed by a .
For instance, in a C file using the default installation of snipMate.vim, if you type "for" in insert mode, it will expand a typical for loop in C:
for (i = 0; i < count; i++) {
}
To go to the next item in the loop, simply over to it; if there is repeated code, such as the "i" variable in this example, you can simply start typing once it's highlighted and all the matches specified in the snippet will be updated.
The following is a nice helpful change to remap tab and s-tab to c-d and c-a, in case you don't want to lose the functionality of tab (in ~/.vim/after/plugin/snipMate.vim):
"""ino <silent> <tab> <c-r>=TriggerSnippet()<cr>
"""snor <silent> <tab> <esc>i<right><c-r>=TriggerSnippet()<cr>
"""ino <silent> <s-tab> <c-r>=BackwardsSnippet()<cr>
"""snor <silent> <s-tab> <esc>i<right><c-r>=BackwardsSnippet()<cr>
"""ino <silent> <c-r><tab> <c-r>=ShowAvailableSnips()<cr>
ino <silent> <c-d> <c-r>=TriggerSnippet()<cr>
snor <silent> <c-d> <esc>i<right><c-r>=TriggerSnippet()<cr>
ino <silent> <c-a> <c-r>=BackwardsSnippet()<cr>
snor <silent> <c-a> <esc>i<right><c-r>=BackwardsSnippet()<cr>
ino <silent> <c-r><tab> <c-r>=ShowAvailableSnips()<cr>
You can include function definitions in abbreviations, but they cannot take arguments. This is an example from the vimdocs:
func Eatchar(pat)
let c = nr2char(getchar(0))
return (c =~ a:pat) ? '' : c
endfunc
iabbr <silent> if if ()<Left><C-R>=Eatchar('\s')<CR>
I guess you could maybe parse the abbreviation expression in the function, but I'm not sure if you can also include characters like parenthesis in the abbreviation. Maybe something here will give you an idea.
Edit: You can always do something like this:
:iab for() for(int i = 0; i < ; i++)<C-o>T<
Which lacks the argument autocompletion of course but lets you start typing it immediately.
It worked for me:
iab FF <c-o>:FF
com -nargs=* FF call s:FF(<f-args>)
fu s:FF(i, n)
let t = "for (int a = 0; a < b; ++a) {\e"
let t1 = substitute(t, 'a', a:i, 'g')
exe 'normal! A'.substitute(t1, 'b', a:x, 'g')
exe "normal o\<space>\<BS>\e"
endf
at insert mode FF e 10<cr> will be for (int e = 0; e < 10; ++e) {<cr>.
mu-template support interactive templates. With it, you can either ask something to the user, or reuse any variable, apply computation on it if you which (detecting that i is already use in the current scope is doable), and use the result in the text you will expand.

Resources