I know I can write a buffer to an external program with :w !<command>
, but is there a way to pipe the contents of the spelling correction list I get with z=
into an external program (i.e. dmenu)? If the function(s) behind z=
are written in vimscript, can I modify them or is it hard coded ?
2 Answers
I don't have dmenu
installed, but I tried with zenity
:
:echo system("zenity --list --column=Suggestions 2>/dev/null", spellsuggest(expand("<cword>")))
expand("<cword>")
expands to the word under the cursor.spellsuggest(...)
returns a list of spelling suggestionssystem("zenity...", ...)
callszenity
and pipes the the list fromspellsuggest(...)
as input.
If I then select one of the suggestions, it is echoed by Vim.
See the help for spellsuggest (:h spellsuggest()
), as it can take additional parameters.
-
-
@Galilean Well, that was not the question :-). You would need additional scripting. Or maybe another question.– RalfMay 16, 2019 at 6:35
My solution (as per the answer @Ralf gave) is this:
" Dmenu integration for searching spelling correction. Replaces `z=`.
function DmenuCorrect()
let word=system("$HOME/.bin/dmenuw -i -l 8 2>/dev/null", spellsuggest(expand("<cword>")))
if len(word) != 0
exe "normal ciw". word[0:-2]
endif
endfunction
nnoremap <silent>z= :call DmenuCorrect()<Cr>
Replacing the word under the cursor is done with exe "normal ciw" . word [0:-2]
. ciw
puts vim into replace mode within the current word boundaries. word [0:-2]
is just to account for the newline dmenu prints, and inserts the chosen solution into the buffer in place of the now deleted misspelt word.