I've enjoyed using # to highlight and search for a word (variable) but I would love if I could hit a single command that would simply tell me how many occurrences are in the current file. I've found the following
:%s/dns_name_change_flag/&/gn
But that's too much typing. Is there anyway to maybe map the above one-liner to use the word under cursor?
I don't know how to do this without execute. The following maps F5 to count occurrences of the word under the cursor using <cword> and word boundary patterns (\\< and \\>):
:map <f5> :execute ":%s#\\<" . expand("<cword>") . "\\>\#&#gn"<CR>
:map <F2> "zyiw:exe "%s/".#z."//gn"<CR>
add this line (without the ":") to your .vimrc and F2 will be mapped every time you start vim.
It yanks the 'inner word' to the z register and then performs a search in the whole buffer outputting the number of appearances.
This approach differs to the one given by Thor in that way, that it also counts appearances of the word that are not a word themselves, but only part of a word. For example: looking for 'an' will also count 'and'.
This might be helpful too:
"A quick way to list all occurrences of the word under the cursor it to type [I (which displays each line containing the current keyword, in this file and in included files when using a language such as C)."
source: http://vim.wikia.com/wiki/Word_count
Map Execute to Grep
You can map a sequence to an external grep command. For example:
:nmap fg :execute '!fgrep --count <cword> %'<CR>
This maps a normal-mode command to fg. The command will run fgrep on the current file in an external process, counting the instances of the word under the cursor.
Minor Caveat
This operates on the current file, not the current buffer. You need to make sure the file is written to disk (e.g. :write) before the word count will be accurate.
I have written a plugin for that: SearchPosition; it provides mappings for the current search pattern and current word / selection, and also lists where the matches occur:
1 match after cursor in this line, 8 following, 2 in previous lines;
total 10 for /\<SearchPosition\>/
:nmap <Leader>* *<C-o>:%s///gn<CR><C-o>
I have a function that does not change search register #/
fun! CountWordFunction()
try
let l:win_view = winsaveview()
exec "%s/" . expand("<cword>") . "//gn"
finally
call winrestview(l:win_view)
endtry
endfun
command! -nargs=0 CountWord :call CountWordFunction()
nnoremap <F3> :CountWord<CR>
If you alredy have searched for the word you can just type
:%~n
Related
Just another vim source code comment question here. I have this mapping for my python source code files:
map <C-C> <Home>i#<Esc>
imap <C-C> <Home>#<Esc> i
On Ctrl-C it puts # in the beginning if the line to comment it out. This improves productivity a lot. But when I want to uncomment lines, I have to do this manually, meaning going to the first character of each commented line and remove it. This is very annoying. At the first glance, I can just bind Home-x to some key, but I can occasionally remove an innocent space or something else in case I misshit and do this on line that has no # character at the beginning of it. I first try to do some replacement with :%s// for a single line, but that has an unwanted affect - it triggers a search and highlights 'pattern' in other lines. In case of a single # character it is a fail.
Can anybody suggest how to remove a specified character in the beginning of current line in case it present and do nothing, if not, without using pattern replacement?
I have created a simple function to Toggle comment in the line:
function! ToggleComment()
let l:pos = col('.')
if getline('.') =~ '\v(\s+|\t+)?#'
exec 'normal! _"_x'
let l:pos -= 1
else
exec 'normal! I#'
let l:pos += 1
endif
call cursor(line("."), l:pos)
endfunction
nnoremap <Leader>t :call ToggleComment()<CR>
inoremap <Leader>t <C-o>:call ToggleComment()<CR>
I recommend Tim Pope's plugin vim-commentary because is way more complete. But of course our idea gives you guys a glimpse how far we can get with vimscript.
Another approach, which does not need to save windowview and toggles comments in other languages can be seen here
Can anybody suggest how to remove a specified character in the beginning of current line in case it present and do nothing, if not, without using pattern replacement?
A solution would be (assuming your cursor is anywhere to the right of # when using the map):
map <c-c> mmF#x`m
A more general solution would be to use a substitution and histdel() to delete the last search pattern:
function! DelComment()
s/^\( *\)#/\1/
call histdel("search", -1)
let #/ = histget("search", -1)
endfunction
After executing the function (by selecting it and typing :#") you can map it to <c-c>:
map <silent> <c-c> mm:silent! call DelComment()<cr>`m
I like using marks around functions to retain the cursor position after executing the map. Feel free to remove mm and `m in the above map.
I have this script to search for the word under cursor in the current project:
nnoremap <leader>K :grep! "\b<C-R><C-W>\b"<CR>:cw<CR>
It works just fine except when the word starts with b.
This is due to <C-R><C-W> only completing the remaining of the word. For example, if I'm searching for "branch", my pattern gets something like this:
\branch\b
Which is equivalent to search for the work "ranch".
Any thoughts on how to figure this out?
Try this: nnoremap <leader>K :execute 'grep! "\b"'.expand("<cword>").'"\b"'<CR>:cw<CR>.
<cword> will expand to the current word under the cursor, as :help :<cword> explains, along with others:
<cword> is replaced with the word under the cursor (like |star|)
<cWORD> is replaced with the WORD under the cursor (see |WORD|)
<cfile> is replaced with the path name under the cursor (like what|gf| uses)
Check the help for more info.
Is there a equivalent to search the string in the current line similar to * or # which search the current word?
No, but it's easy to make it.
:nnoremap <silent> <Leader>f :execute '/\V\^' . escape(getline('.'), '\\/') . '\$'<CR>
This should give you \f (or whatever you remapped as leader instead of the backslash) that should start the search for the next instance of the current line.
When searching string with notepad++, new window opens and shows find
results. I want to use this feature in vim. After googling I found out some suggestions:
vimgrep /<pattern>/ %
copen
It is possible to create mapping which do those two commands. Pattern should be the current word: may be cword keyword in vim?
The requirement is actually easy. but to get user inputted pattern, you need a function.
function! FindAll()
call inputsave()
let p = input('Enter pattern:')
call inputrestore()
execute 'vimgrep "'.p.'" % |copen'
endfunction
if you want to have a mapping, add this line:
nnoremap <F8> :call FindAll()<cr>
but as I commented under your question. % may not work for unamed buffer.
I suggest lvimgrep (so you can use quickfix for :make)
:nnoremap <F6> :lvimgrep /\M\<<C-R><C-W>\m\>/ **/*.[ch]pp **/Makefile | lopen<CR>
Also, if you just wanted to find in the current file:
:g/<pattern>/
will invoke 'print' (default command) on each matching line.
:v// " non-matching lines
:g//-1 " lines preceding the matching line
:g//-1,+1 " lines around the matching line
etc.
:global is far more useful:
:g/foo/ join " join all lines containing foo
etc.
Those two commands can be shortened and chained: :vim foo %|co. You can pull the word under the cursor like this: :vim <C-r><C-w> %|co.
Here is a quick normal mode mapping that you can use to list all the occurrences of the word under your cursor in the quickfix window:
nnoremap <F6> :vimgrep /<C-r><C-w>/j % <bar> cwindow<cr>
You can also use :il[ist] foo to display a list of all the occurrences of foo or [I to display the same list for the word under your cursor.
When the list is displayed, use :{line number} to jump to the corresponding line.
I'm using vim to maintain a weblog of what I do through the day (what commands I used to generate output, etc..), and sometimes I need to copy-paste strings that have special html characters in them. Is there a way to make an "html-paste" mode that will (for instance) convert < to <?
There are a few small functions here if someone feels like modifying them to accept a range, then provide a mapping which passes the [ and ] marks to act on the last pasted text.
Actually, after looking a bit, you don't need to modify the functions from the vim tip at all. If a function doesn't explicitly pass the range option, the function is called once for each line of the given range. This means that all you need to do is call the function with a range.
A couple useful examples are below. The first calls HtmlEscape() for each line in the last pasted text, the second does the same but for each line in a visually selected block. Add this to your .vimrc:
nnoremap <Leader>h :'[,']call HtmlEscape()<CR>
vnoremap <Leader>h :call HtmlEscape()<CR>
function HtmlEscape()
silent s/&/\&/eg
silent s/</\</eg
silent s/>/\>/eg
endfunction
Obviously if you want more things replaced you'd have to add them; there are many on the linked wiki page.
For simple XML encoding I use Tim Pope's unimpaired.vim.
[x followed by a motion
visually select the text then [x
[xx to encode the current line
To encode the just pasted text:
`[[x`]
Explanation:
`[ and `] are marks set by the boundaries of a change or a yank. `[ at the beginning and `] at the end.
First move to the start of the just pasted text with `[
Next execute [x to encode the following motion
By using `] the motion will be all the text between the current cursor location to the end of the pasted text.
Optionally you can wrap this up into a mapping
nmap <leader>x `[[x`]
More information:
:h mark-motion
:h `[
You can use a mapping that will convert register value and then paste it:
python import xmlrpclib, vim
function! EscapeHTML(str)
try " Force any error to be an exception "
let d={}
python vim.eval("extend(d, {'xml': '"+xmlrpclib.escape(vim.eval("a:str")).replace("'", "''")+"'})")
return d.xml
endtry
endfunction
function! s:PasteHTML()
return "\"=EscapeHTML(getreg(".string(v:register)."))\np"
endfunction
nnoremap <expr> ,p <SID>PasteHTML()
Requires vim with python support and python installed. xmlrpclib packages comes with python. If you don't want python, replace EscapeHTML function. With this script ,p will work just as p except for converting its input.