how to remove file name from VIM dictionary menu? - vim

I've installed pydiction dictionary in vim so that I'd be able to get a list of python commands when I press tab after partially typed command. Everything is working fine, except every time the menu shows up, there is a file name besides the each command in the list. How do I remove that filename from the menu?
plz, take a look at the picture: http://www.uzbozor.com/uploads/vim.png
(copy and paste the link if clicking doesn't work)
Thanks

I haven't managed to solve this very elegantly, but there's a workaround by writing a custom completion function that simply greps the dictionary file for matches:
function! MyCompleteFunction( findstart, base )
if a:findstart
let line = getline('.')
let start = col('.') - 1
while start > 0 && line[start - 1] =~ '[A-Za-z_]'
let start -= 1
endwhile
return start
else
silent call DictGrep( a:base, 'path\to\dictionary\file' )
let matches = []
for thismatch in getqflist()
call add(matches, thismatch.text)
endfor
return matches
endif
endfunction
Note that I have defined a function DictGrep() that actually performs the vimgrep. This is so I can call it silently and not be troubled by error messages:
function! DictGrep( leader, file )
try
exe "vimgrep /^" . a:leader . ".*/j " . a:file
catch /.*/
echo "no matches"
endtry
endfunction
Then simply define set the completefunc:
setlocal completefunc=MyCompleteFunction()
and then use for insert-mode completion (which could be mapped to replace your current dictionary completion binding).
The vimgrep could be quite a slow operation, but I haven't noticed any problems unless there are hundreds of matches in the dictionary file.
Hope this helps.

Related

How to test if there is only default buffer open?

I'm writing a plugin that loads sessions only if no buffers have been open. So far I'm testing vs argc.
if (argc() != 0)
return
endif
This works fine if I pass in arguments from the command line: vim file1
However, the problem is that I have some scripts that wrap around vim:
function foo {
vim file1
}
$ foo
In the latter case, argc is empty and thus the above if condition fails.
How do you test if vim was invoked with only the default buffer open?
If you pass files to Vim, that will be reflected in argc (also when you wrap the Vim invocation). Rather, the corner case is launching Vim with an edit command, e.g. vim -c "edit foo". If you need to detect that, you need to check two things:
the current buffer is the default blank buffer
no other text buffers have been loaded
Here's a set of functions to implement that:
function! IsBlank( bufnr )
return (empty(bufname(a:bufnr)) &&
\ getbufvar(a:bufnr, '&modified') == 0 &&
\ empty(getbufvar(a:bufnr, '&buftype'))
\)
endfunction
function! ExistOtherBuffers( targetBufNr )
return ! empty(filter(range(1, bufnr('$')), 'buflisted(v:val) && v:val != a:targetBufNr'))
endfunction
function! IsEmptyVim()
let l:currentBufNr = bufnr('')
return IsBlank(l:currentBufNr) && ! ExistOtherBuffers(l:currentBufNr)
endfunction
The best I could come up with was to write a function. I thought bufnr('$') would do it but it lists that highest loaded buffer number. The bufnr('$') function returns 1 even though I have not opened a file (simply launched vim). Also buffers can be unloaded with :bw, which does not change what bufnr('$') returns.
Anyway this is the function:
function! NumBuffers()
let rc = 0
for idx in range(bufnr('$'))
if bufloaded(idx)
let rc += 1
endif
endfor
return rc
endfunction

Yanking all marked lines in vim

Often times when reviewing log files in vim, I'll highlight interesting lines using marks. At some point, I'd like to be able to copy all of the interesting lines (either all marked lines, or a list of marks) to either a register or another file (it doesn't really matter which; the goal is to facilitate writing a summary). I haven't been able to find any built in way to do this; is it possible in vim?
I suppose it's probably a fairly straightforward function; probably looking something like this, but my vimscript abilities are very weak:
for cur_mark in list_of_marks
goto mark
yank current line and append to register
Has anyone ever written anything similar that they can point me to?
Thanks
EDIT: I posted the accepted solution at https://github.com/mikeage/vim-yankmarks
As always, there are few things that are more motivating than asking for help. Here's what I came up with; feedback welcome.
function! Yankmark()
let save_cursor = getpos(".")
let n = 0
" I should really make this a parameter...
let marks_to_yank="abcdefghijklmnopqrstuvwxyz"
let nummarks = strlen(marks_to_yank)
" Clear the a register
let #a=''
while n < nummarks
let c = strpart(marks_to_yank, n, 1)
" Is the mark defined
if getpos("'".c)[2] != 0
" using g' instead of ' doesn't mess with the jumplist
exec "normal g'".c
normal "Ayy
endif
let n = n + 1
endwhile
call setpos('.', save_cursor)
endfunction
Mikeage had a great idea; here's a more refined version of his function turned into a command:
":YankMarks [{marks}] [{register}]
" Yank all marked (with [a-z] / {marks} marks) lines into
" the default register / {register} (in the order of the
" marks).
function! s:YankMarks( ... )
let l:marks = 'abcdefghijklmnopqrstuvwxyz'
let l:register = '"'
if a:0 > 2
echohl ErrorMsg
echomsg 'Too many arguments'
echohl None
return
elseif a:0 == 2
let l:marks = a:1
let l:register = a:2
elseif a:0 == 1
if len(a:1) == 1
let l:register = a:1
else
let l:marks = a:1
endif
endif
let l:lines = ''
let l:yankedMarks = ''
for l:mark in split(l:marks, '\zs')
let l:lnum = line("'" . l:mark)
if l:lnum > 0
let l:yankedMarks .= l:mark
let l:lines .= getline(l:lnum) . "\n"
endif
endfor
call setreg(l:register, l:lines, 'V')
echomsg printf('Yanked %d line%s from mark%s %s',
\ len(l:yankedMarks),
\ len(l:yankedMarks) == 1 ? '' : 's',
\ len(l:yankedMarks) == 1 ? '' : 's',
\ l:yankedMarks
\) . (l:register ==# '"' ? '' : ' into register ' . l:register)
endfunction
command! -bar -nargs=* YankMarks call <SID>YankMarks(<f-args>)
A different way of accomplishing this might be using the :global command. The global command takes the form :g/{pattern}/{cmd}. The command, {cmd}, will be executed on all lines matching {pattern}.
Append lines matching a pattern to a register:
:g/pattern/yank A
Append matching line to a log file:
:g/pattern/w >> file.log
Of course if you want to find line matching a mark you can match it in your pattern. The following pattern matches a line with mark m.
:g/\%'m/w >> file.log
To do something like this. (Note: I am using \v to turn on very magic)
:g/\v(%'a|%'b|%'m)/yank A
Of course if a pattern won't work you can do this by hand. Instead of marking the lines just build up the lines as you go. Just yank a line to an uppercase register to append.
"Ayy
Or do a write append with a range of a single line
:.w >> file.log
For more help see
:h :g
:h :w_a
:h /\%'m
:h /\v
You can do something like:
:redir #a
:silent marks XYZN
:redir END
"ap
That way the output of the :marks command will be redirected to the a register. Note, that it will only lists (in the above case) the X, Y, Z and N marks (as the arguments), and if there was an a register, it will be deleted/overwritten.
Also note, that it might not give the desired output, but gives you a starting point...
I like the solution from Mikeage, though I would probably solve this with the multiselect - Create multiple selections and operate plugin. This also has the benefit that you don't run out of marks.
With the plugin, you can select lines with <Leader>msa or :MSAdd. Finally, yank all lines with:
:let #a=''
:MSExecCmd yank A
If you use an upper-case register name when yanking into a specific register, Vim will append the yanked content instead of overwriting the register's value.
So, for example:
"ayy - yank current line to register a, overwriting
[move]
"Ayy - append this line to register a
[move]
"ap - paste all yanked material
See :help quotea for more details.

How to redirect standard output when a vim command opens a url at cursor in browser?

I'm using a vim plugin and when I want to look up a function under the cursor in the online api docs, I type '\da'
Here's the vimscipt code for the keymapping:
nnoremap <buffer> <LocalLeader>dda :silent call <SID>OpenURL('http://api.drush.ws/api/function/')<CR><C-L>
When the command is run, it writes the standard output from the shell into the current vim buffer, so the phrase:
"Created new window in existing browser session." will be written into the current buffer.
Also here's the openurl function:
function s:OpenURL(base)
let open = b:Drupal_info.OPEN_COMMAND
if open == ''
return
endif
let func = shellescape(expand('<cword>'))
if a:base == 'api.d.o'
if strlen(b:Drupal_info.CORE)
execute '!' . open . ' http://api.drupal.org/api/search/' .
\ b:Drupal_info.CORE . '/' . func
else
execute '!' . open . ' http://api.drupal.org/' . func
endif
else
execute '!' . open . ' ' . a:base . func
endif
endfun
How do I fix this/redirect stdout?
(I'm using ubuntu/gnome.)
Thanks!
I do not see anything that will put command output into the current buffer. But if you don’t want to observe command output at all, you can do one of two things:
Put silent in front of each ! (note: you must have the space after silent) and add redraw! command just before endfunction.
Replace every execute '!'.<...> with call system(<...>) (in this case having newline in arguments is much likely to cause bugs).

Favourite places in vim

Is there a command in vim that can bookmark a place (path to the file, line number in that file), so that I can go to that place easily later?
It would be similar as NERDTree :Bookmark command. You can open your file with NERDTreeFromBookmark. I'm looking for the same functionality with the difference that bookmark is not only a file but file + line number.
Thank you
Yes you can do so with the 'mark' command. There are two types of bookmarks you can create, local and global. You are referring to a global bookmark.
You can type 'mP' to create a bookmark called P. Notice the case, uppercase indicates it is a global bookmark. To go to that bookmark, type `P.
Hope this helps
Source
The viminfo setting can contain the option !, which makes it store any global variables with uppercase letters in the viminfo file. Using this, you can define a variable called g:BOOKMARKS and store your bookmarks in there.
Here's some vimscript you could use to do that:
set viminfo+=!
if !exists('g:BOOKMARKS')
let g:BOOKMARKS = {}
endif
" Add the current [filename, cursor position] in g:BOOKMARKS under the given
" name
command! -nargs=1 Bookmark call s:Bookmark(<f-args>)
function! s:Bookmark(name)
let file = expand('%:p')
let cursor = getpos('.')
if file != ''
let g:BOOKMARKS[a:name] = [file, cursor]
else
echom "No file"
endif
wviminfo
endfunction
" Delete the user-chosen bookmark
command! -nargs=1 -complete=custom,s:BookmarkNames DelBookmark call s:DelBookmark(<f-args>)
function! s:DelBookmark(name)
if !has_key(g:BOOKMARKS, a:name)
return
endif
call remove(g:BOOKMARKS, a:name)
wviminfo
endfunction
" Go to the user-chosen bookmark
command! -nargs=1 -complete=custom,s:BookmarkNames GotoBookmark call s:GotoBookmark(<f-args>)
function! s:GotoBookmark(name)
if !has_key(g:BOOKMARKS, a:name)
return
endif
let [filename, cursor] = g:BOOKMARKS[a:name]
exe 'edit '.filename
call setpos('.', cursor)
endfunction
" Completion function for choosing bookmarks
function! s:BookmarkNames(A, L, P)
return join(sort(keys(g:BOOKMARKS)), "\n")
endfunction
I'm not sure how readable the code is, but basically, the Bookmark command accepts a single parameter to use as a name. It will store the current filename and cursor position to the g:BOOKMARKS dictionary. You can use the GotoBookmark command with a mark name to go to it. DelBookmark works in the same way, but deletes the given mark. Both functions are tab-completed.
Another way to jump through them is by using this command:
" Open all bookmarks in the quickfix window
command! CopenBookmarks call s:CopenBookmarks()
function! s:CopenBookmarks()
let choices = []
for [name, place] in items(g:BOOKMARKS)
let [filename, cursor] = place
call add(choices, {
\ 'text': name,
\ 'filename': filename,
\ 'lnum': cursor[1],
\ 'col': cursor[2]
\ })
endfor
call setqflist(choices)
copen
endfunction
CopenBookmarks will load the bookmarks in the quickfix window, which seems like a nice interface to me.
This solution is similar to Eric's -- it uses the .viminfo file, so if something goes wrong with it, you'll probably lose your marks. And if you save your marks in one vim instance, they won't be immediately available in another.
I don't know how comfortable your are with vimscript, so just in case -- to use this, you can put the code in a file under your plugin vimfiles directory, for example plugin/bookmarks.vim. Should be completely enough. Here's the entire code in a gist as well: https://gist.github.com/1371174
EDIT: Changed the interface for the solution a bit. Original version can be found in the gist history.
I have used this script (number marks). There might be better ones though. Wait for other answers!
This doesn't solve your problem as stated, but you may find it helps.
MRU.vim - Most Recently Used files plugin
Type :MRU and you get a nice searchable list of your most recently used files. Pressing enter on one brings you to it.
" When editing a file, always jump to the last known cursor position.
" And open enough folds to make the cursor is not folded
" Don't do it when the position is invalid or when inside an event handler
" (happens when dropping a file on gvim).
autocmd BufWinEnter *
\ if line("'\"") <= line("$") |
\ exe "normal! g`\"" | exe "normal! zv" |
\ endif

How to make Vim's global command :g/ work on per occurrence basis

Typically Vim's global command :g// works on per line basis. Is it possible to make it work on per occurrence basis as there could be more than one occurrence on a line.
Not a direct answer, but you could use something like :rubydo, which will run some ruby scriptlet per line of code. Combining that with gsub in ruby should get you the ability to do just about anything per occurrence of a match. Of course, you will need to do it with ruby code, which may not give you access to everything that you might need without hassle (register appending would be annoying for instance)
:[range]rubyd[o] {cmd} Evaluate Ruby command {cmd} for each line in the
[range], with $_ being set to the text of each line in
turn, without a trailing <EOL>. Setting $_ will change
the text, but note that it is not possible to add or
delete lines using this command.
The default for [range] is the whole file: "1,$".
You can try:
command! -bang -nargs=1 -range OGlobal
\ <line1>,<line2>call s:Global("<bang>", <f-args>)
function! s:Global(bang, param) range
let inverse = a:bang == '!'
" obtain the separator character
let sep = a:param[0]
" obtain all fields in the initial command
let fields = split(a:param, sep)
" todo: handle inverse
let l = a:firstline
while 1
let l = search(fields[0], 'W')
if l == -1 || l > a:lastline
break
endif
exe fields[1]
endwhile
endfunction
Which you can use as :global, except that the command name is different (and that the bang option as not been implemented yet)

Resources