Vim Tips Wiki
(Change to TipImported template + severe manual clean)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{deprecated|This is now part of Vim 7(.1?)}}
 
{{TipImported
 
{{TipImported
 
|id=901
 
|id=901
 
|previous=900
 
|previous=900
 
|next=902
 
|next=902
|created=March 24, 2005
+
|created=2005
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=joern_h
 
|author=joern_h
 
|version=6.0
 
|version=6.0
 
|rating=14/5
 
|rating=14/5
  +
|category1=Map
  +
|category2=
 
}}
 
}}
 
This function adds support for common operations on string text objects like:
 
This function adds support for common operations on string text objects like:
*<tt>di"</tt> Delete Inner String.
+
*<code>di"</code> Delete Inner String.
*<tt>ca'</tt> Change A String.
+
*<code>ca'</code> Change A String.
   
 
and so on. Strings have to be on one line. See {{help|text-objects}} for a description of other predefined text objects.
 
and so on. Strings have to be on one line. See {{help|text-objects}} for a description of other predefined text objects.
Line 29: Line 32:
 
endif
 
endif
 
endfunction
 
endfunction
nnoremap di" :call JHStringObject('d','"','i')&lt;CR&gt;
+
nnoremap di" :call JHStringObject('d','"','i')<CR>
nnoremap di' :call JHStringObject('d',"'",'i')&lt;CR&gt;
+
nnoremap di' :call JHStringObject('d',"'",'i')<CR>
nnoremap da" :call JHStringObject('d','"','a')&lt;CR&gt;
+
nnoremap da" :call JHStringObject('d','"','a')<CR>
nnoremap da' :call JHStringObject('d',"'",'a')&lt;CR&gt;
+
nnoremap da' :call JHStringObject('d',"'",'a')<CR>
nnoremap ci" :call JHStringObject('c','"','i')&lt;CR&gt;
+
nnoremap ci" :call JHStringObject('c','"','i')<CR>
nnoremap ci' :call JHStringObject('c',"'",'i')&lt;CR&gt;
+
nnoremap ci' :call JHStringObject('c',"'",'i')<CR>
nnoremap ca" :call JHStringObject('c','"','a')&lt;CR&gt;
+
nnoremap ca" :call JHStringObject('c','"','a')<CR>
nnoremap ca' :call JHStringObject('c',"'",'a')&lt;CR&gt;
+
nnoremap ca' :call JHStringObject('c',"'",'a')<CR>
nnoremap yi" :call JHStringObject('y','"','i')&lt;CR&gt;
+
nnoremap yi" :call JHStringObject('y','"','i')<CR>
nnoremap yi' :call JHStringObject('y',"'",'i')&lt;CR&gt;
+
nnoremap yi' :call JHStringObject('y',"'",'i')<CR>
nnoremap ya" :call JHStringObject('y','"','a')&lt;CR&gt;
+
nnoremap ya" :call JHStringObject('y','"','a')<CR>
nnoremap ya' :call JHStringObject('y',"'",'a')&lt;CR&gt;
+
nnoremap ya' :call JHStringObject('y',"'",'a')<CR>
 
</pre>
 
</pre>
   
Line 58: Line 61:
 
execute c
 
execute c
 
endfunction
 
endfunction
omap a" :call StringObject('"','a')&lt;cr&gt;
+
omap a" :call StringObject('"','a')<CR>
omap i" :call StringObject('"','i')&lt;cr&gt;
+
omap i" :call StringObject('"','i')<CR>
omap a' :call StringObject("'",'a')&lt;cr&gt;
+
omap a' :call StringObject("'",'a')<CR>
omap i' :call StringObject("'",'i')&lt;cr&gt;
+
omap i' :call StringObject("'",'i')<CR>
 
</pre>
 
</pre>
   
 
Using this method allows you to use all the built-in Vim operators on StringObjects without defining four mappings for each operator.
 
Using this method allows you to use all the built-in Vim operators on StringObjects without defining four mappings for each operator.
   
To get equivalent visual mode mappings add &lt;esc&gt; to the start:
+
To get equivalent visual mode mappings add <Esc> to the start:
   
 
<pre>
 
<pre>
vmap a" &lt;esc&gt;:call StringObject('"','a')&lt;cr&gt;
+
vmap a" <Esc>:call StringObject('"','a')<CR>
vmap i" &lt;esc&gt;:call StringObject('"','i')&lt;cr&gt;
+
vmap i" <Esc>:call StringObject('"','i')<CR>
vmap a' &lt;esc&gt;:call StringObject("'",'a')&lt;cr&gt;
+
vmap a' <Esc>:call StringObject("'",'a')<CR>
vmap i' &lt;esc&gt;:call StringObject("'",'i')&lt;cr&gt;
+
vmap i' <Esc>:call StringObject("'",'i')<CR>
 
</pre>
 
</pre>
   

Latest revision as of 05:55, 13 July 2012

This tip is deprecated for the following reasons:

This is now part of Vim 7(.1?)

Tip 901 Printable Monobook Previous Next

created 2005 · complexity intermediate · author joern_h · version 6.0


This function adds support for common operations on string text objects like:

  • di" Delete Inner String.
  • ca' Change A String.

and so on. Strings have to be on one line. See :help text-objects for a description of other predefined text objects.

function! JHStringObject(cmd,delim,mode)
  if a:mode == 'i'
    let c = 'normal T' . a:delim . a:cmd . 't' . a:delim
  elseif a:mode == 'a'
    let c = 'normal F' . a:delim . a:cmd . 'f' . a:delim
  endif
  execute c
  if a:cmd == 'c'
    normal l
    startinsert
  endif
endfunction
nnoremap di" :call JHStringObject('d','"','i')<CR>
nnoremap di' :call JHStringObject('d',"'",'i')<CR>
nnoremap da" :call JHStringObject('d','"','a')<CR>
nnoremap da' :call JHStringObject('d',"'",'a')<CR>
nnoremap ci" :call JHStringObject('c','"','i')<CR>
nnoremap ci' :call JHStringObject('c',"'",'i')<CR>
nnoremap ca" :call JHStringObject('c','"','a')<CR>
nnoremap ca' :call JHStringObject('c',"'",'a')<CR>
nnoremap yi" :call JHStringObject('y','"','i')<CR>
nnoremap yi' :call JHStringObject('y',"'",'i')<CR>
nnoremap ya" :call JHStringObject('y','"','a')<CR>
nnoremap ya' :call JHStringObject('y',"'",'a')<CR>

Comments[]

I think the tips must begin with a problem definition, not a defintion of the solution. Usually, we look for a tip that is solving a problem we faced. By looking at a snazzy solution, we have no way of telling what context would it apply to us. An example would help.


Another way to define text objects is using the omap command like this:

function! StringObject(delim,mode)
  if a:mode == 'i'
    let c = 'normal T' . a:delim . 'vt' . a:delim
  elseif a:mode == 'a'
    let c = 'normal F' . a:delim . 'vf' . a:delim
  endif
  execute c
endfunction
omap a" :call StringObject('"','a')<CR>
omap i" :call StringObject('"','i')<CR>
omap a' :call StringObject("'",'a')<CR>
omap i' :call StringObject("'",'i')<CR>

Using this method allows you to use all the built-in Vim operators on StringObjects without defining four mappings for each operator.

To get equivalent visual mode mappings add <Esc> to the start:

vmap a" <Esc>:call StringObject('"','a')<CR>
vmap i" <Esc>:call StringObject('"','i')<CR>
vmap a' <Esc>:call StringObject("'",'a')<CR>
vmap i' <Esc>:call StringObject("'",'i')<CR>