11

I am trying to create a new text object in Evil. For example, the text object iw will only select subsets of strings containing hyphens. I want the new text object to match words with any non-space characters. What I got so far is:

(evil-define-text-object evil-inner-space (count &optional beg end type)
  "Select inner space."
  :extend-selection nil
  (evil-regexp-range count beg end type "[ \n]" " " t))

(define-key evil-inner-text-objects-map " " 'evil-inner-space)

This code fails to correctly match words at the end of a line. It works for words at the beginning of a line thanks to \n.

I tried many things that did not work. The trouble for me is that when something does not work, I don't know if it is due to a wrong regexp or to limitations in evil-regexp-range. For example, using \s- reports an error, which seems to be a limitation in Evil.

3 Answers 3

21

Update: evil-regexp-range was recently replaced with evil-select-paren. This works on current evil and has the same usage as the old one:

(defmacro define-and-bind-text-object (key start-regex end-regex)
  (let ((inner-name (make-symbol "inner-name"))
        (outer-name (make-symbol "outer-name")))
    `(progn
       (evil-define-text-object ,inner-name (count &optional beg end type)
         (evil-select-paren ,start-regex ,end-regex beg end type count nil))
       (evil-define-text-object ,outer-name (count &optional beg end type)
         (evil-select-paren ,start-regex ,end-regex beg end type count t))
       (define-key evil-inner-text-objects-map ,key (quote ,inner-name))
       (define-key evil-outer-text-objects-map ,key (quote ,outer-name)))))

Original Answer:

If you end up defining more than one new text object, the repetition can get annoying, especially if you want to bind both inner and outer objects. If you hit that barrier, try this:

(defmacro define-and-bind-text-object (key start-regex end-regex)
  (let ((inner-name (make-symbol "inner-name"))
        (outer-name (make-symbol "outer-name")))
    `(progn
      (evil-define-text-object ,inner-name (count &optional beg end type)
        (evil-regexp-range count beg end type ,start-regex ,end-regex t))
      (evil-define-text-object ,outer-name (count &optional beg end type)
        (evil-regexp-range count beg end type ,start-regex ,end-regex nil))
      (define-key evil-inner-text-objects-map ,key (quote ,inner-name))
      (define-key evil-outer-text-objects-map ,key (quote ,outer-name)))))

Usage:

; between dollar signs:
(define-and-bind-text-object "$" "\\$" "\\$")

; between pipe characters:
(define-and-bind-text-object "|" "|" "|")

; from regex "b" up to regex "c", bound to k (invoke with "vik" or "vak"):
(define-and-bind-text-object "k" "b" "c")

(This is more than you wanted, but I'll leave it here in case it helps someone :)

2
  • 1
    Thank you for this! I often found myself wishing I had a text object to e.g. match C++ declaration without chomping parens -- e.g. matching _foo::bar::baz_(), including : but ignoring (). Your solution made this very easy: (define-and-bind-text-object "k" "[[:space:]\(\)]" "[[:space:]\(\)]"). Muchos gracias! Aug 31, 2014 at 6:34
  • 1
    It looks like evil-regexp-range has been removed from the most recent versions of Evil -- I believe we now need to use `evil Dec 1, 2014 at 18:56
10

I don't think you need to create a new text object. Evil includes the symbol text object. The symbol syntax is defined by the major-mode and usually includes hyphens, numbers, underscores and other non-whitespace characters.

The symbol text object is bound to o. The definition is at evil-maps.el at Bitbucket

2
  • 1
    hmm no. The text object that I was looking for is indeed implemented in vim/evil, but as @PythonNut said in a comment, it is bound to W. And for some cases, this implementation works better than the regexp I posted. Aug 4, 2014 at 8:56
  • lionel is correct, this does not exactly answer the question - but o is very useful, thanks JoeS! Jun 26, 2016 at 19:18
1

The correct regexp is " \|.?$". The following code implements a new text object matching any nonspace character.

(evil-define-text-object evil-inner-space (count &optional beg end type)
  "Select inner space."
  :extend-selection nil
  (evil-regexp-range count beg end type "[ \n]" " \\|.?$" t))
3
  • 2
    I'll add that the current evil already has an inner WORD textobject bound to i W.
    – PythonNut
    Nov 4, 2013 at 20:00
  • haha I did not find this with web searches... I should read the vim manual one of these days. Thanks Nov 6, 2013 at 8:03
  • 1
    As the other answer noted: evil-regexp-range was replaced with evil-select-paren
    – Hi-Angel
    Sep 17, 2019 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.