How can I get full-line completion in Emacs, similar to Vim's CTRL-x l
?
For example, in Vim, if I type:
from d<CTRL-X>-l
I will get a popup like this:
Which shows all the lines matching that prefix in open buffers.
The key to triggering the auto-completion in emacs is the Tab key. You will get a list of suggestions from the compiler. To select something from the list of suggestions, we recommend you to use C-n and C-p, but the down and up arrow keys can be used as well.
Auto-Complete is an intelligent auto-completion extension for Emacs. It extends the standard Emacs completion interface and provides an environment that allows users to concentrate more on their own work. Its features are: a visual interface, reduce overhead of completion by using statistic method, extensibility.
This doesn't give a list to choose from (try tuning autocomplete
for that) but otherwise gives the "feeling".
(defun my-expand-lines ()
(interactive)
(let ((hippie-expand-try-functions-list
'(try-expand-line)))
(call-interactively 'hippie-expand)))
(define-key evil-insert-state-map (kbd "C-x C-l") 'my-expand-lines)
hippie-expand does that, you just have to configure it as described on the last answer here: Does Emacs has word and line completion (like Vim's insert mode completion)?
Emacs comes with multi-occur
if you are comfortable navigating the list as a buffer.
Otherwise you should get to know helm
. See this answer https://stackoverflow.com/a/14731718/903943
It takes some doing, but you can combine company completion and hippie-expand to give you a popup menu of lines from the current buffer.
First, you need to define a function to call try-expand-line (modified to return the expansion instead of calling he-expand and returning t) from hippie-exp.el and collect all the expansions:
(defun get-hippie-expand-lines ()
(let (completions-list candidate)
(setq candidate (my-try-expand-line nil))
(if candidate
(progn
(push candidate completions-list)
(while (progn
(setq candidate (my-try-expand-line t))
(if candidate
(push candidate completions-list)
nil))))
nil)
completions-list))
Then, you can write a small company backend to give you the completion candidates through a company popup.
(defun company-hippie-line (command &optional arg &rest ignored)
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'company-hippie-line))
(prefix (and (not (looking-at "[:word:]"))
(let (p1 p2)
(save-excursion
(end-of-line)
(setq p2 (point))
(back-to-indentation)
(setq p1 (point)))
(buffer-substring-no-properties p1 p2))))
(candidates (get-hippie-expand-lines))))
;; Uncomment if you want to get line completion popups whenever found
;; (add-to-list 'company-backends 'company-hippie-line)
(global-set-key (kbd "C-x l") 'company-hippie-line)
See further discussion at this post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With