Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full-line completion in Emacs?

Tags:

emacs

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:

vim line completion popup

Which shows all the lines matching that prefix in open buffers.

like image 705
David Wolever Avatar asked Jul 29 '13 15:07

David Wolever


People also ask

How do I Autocomplete in Emacs?

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.

Does Emacs have code completion?

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.


4 Answers

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)
like image 148
mike3996 Avatar answered Nov 13 '22 03:11

mike3996


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)?

like image 29
jfsantos Avatar answered Nov 13 '22 03:11

jfsantos


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

like image 22
event_jr Avatar answered Nov 13 '22 04:11

event_jr


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.

like image 40
nandkeypull Avatar answered Nov 13 '22 02:11

nandkeypull