Vim Tips Wiki
m (Text objects for strings in vim 6.x moved to Text objects for strings: Page moved by JohnBot to improve title)
(Change to TipImported template + severe manual clean)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=901
 
|id=901
  +
|previous=900
|title=Text objects for strings in vim 6.x
 
  +
|next=902
|created=March 24, 2005 10:12
+
|created=March 24, 2005
 
|complexity=intermediate
 
|complexity=intermediate
|author=joern_h--AT--gmx.net
+
|author=joern_h
 
|version=6.0
 
|version=6.0
 
|rating=14/5
 
|rating=14/5
 
}}
|text=
 
This little 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.
 
*<tt>ca'</tt> 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.
   
  +
<pre>
 
 
function! JHStringObject(cmd,delim,mode)
di" Delete Inner String
 
 
if a:mode == 'i'
 
 
let c = 'normal T' . a:delim . a:cmd . 't' . a:delim
ca' Change A String
 
 
elseif a:mode == 'a'
 
 
let c = 'normal F' . a:delim . a:cmd . 'f' . a:delim
 
  +
endif
 
 
execute c
and so on. Strings have to be on one line. See [http://vimplugin.sf.net/cgi-bin/help?tag={{urlencode:text-objects}} :help text-objects] for a description of other predefined text objects.
 
 
if a:cmd == 'c'
 
 
normal l
 
  +
startinsert
 
  +
endif
function! JHStringObject(cmd,delim,mode)
 
 
endfunction
 
 
nnoremap di" :call JHStringObject('d','"','i')&lt;CR&gt;
if a:mode == 'i'
 
 
nnoremap di' :call JHStringObject('d',"'",'i')&lt;CR&gt;
 
 
nnoremap da" :call JHStringObject('d','"','a')&lt;CR&gt;
let c = 'normal T' . a:delim . a:cmd . 't' . a:delim
 
 
nnoremap da' :call JHStringObject('d',"'",'a')&lt;CR&gt;
 
 
nnoremap ci" :call JHStringObject('c','"','i')&lt;CR&gt;
elseif a:mode == 'a'
 
 
nnoremap ci' :call JHStringObject('c',"'",'i')&lt;CR&gt;
 
 
nnoremap ca" :call JHStringObject('c','"','a')&lt;CR&gt;
let c = 'normal F' . a:delim . a:cmd . 'f' . a:delim
 
 
nnoremap ca' :call JHStringObject('c',"'",'a')&lt;CR&gt;
 
 
nnoremap yi" :call JHStringObject('y','"','i')&lt;CR&gt;
endif
 
 
nnoremap yi' :call JHStringObject('y',"'",'i')&lt;CR&gt;
 
 
nnoremap ya" :call JHStringObject('y','"','a')&lt;CR&gt;
execute c
 
 
if a:cmd == 'c'
 
 
normal l
 
 
startinsert
 
 
endif
 
 
endfunction
 
 
 
 
nnoremap di" :call JHStringObject('d','"','i')&lt;CR&gt;
 
 
nnoremap di' :call JHStringObject('d',"'",'i')&lt;CR&gt;
 
 
nnoremap da" :call JHStringObject('d','"','a')&lt;CR&gt;
 
 
nnoremap da' :call JHStringObject('d',"'",'a')&lt;CR&gt;
 
 
nnoremap ci" :call JHStringObject('c','"','i')&lt;CR&gt;
 
 
nnoremap ci' :call JHStringObject('c',"'",'i')&lt;CR&gt;
 
 
nnoremap ca" :call JHStringObject('c','"','a')&lt;CR&gt;
 
 
nnoremap ca' :call JHStringObject('c',"'",'a')&lt;CR&gt;
 
 
nnoremap yi" :call JHStringObject('y','"','i')&lt;CR&gt;
 
 
nnoremap yi' :call JHStringObject('y',"'",'i')&lt;CR&gt;
 
 
nnoremap ya" :call JHStringObject('y','"','a')&lt;CR&gt;
 
 
 
nnoremap ya' :call JHStringObject('y',"'",'a')&lt;CR&gt;
 
nnoremap ya' :call JHStringObject('y',"'",'a')&lt;CR&gt;
  +
</pre>
}}
 
   
== Comments ==
+
==Comments==
I think the tips must begin with a problem definition,
+
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.
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.
 
   
Sunita
 
, March 24, 2005 12:08
 
 
----
 
----
Another way to define text objects is using the omap command like this:
+
Another way to define text objects is using the omap command like this:
   
  +
<pre>
function! StringObject(delim,mode)
+
function! StringObject(delim,mode)
if a:mode == 'i'
+
if a:mode == 'i'
let c = 'normal T' . a:delim . 'vt' . a:delim
+
let c = 'normal T' . a:delim . 'vt' . a:delim
elseif a:mode == 'a'
+
elseif a:mode == 'a'
let c = 'normal F' . a:delim . 'vf' . a:delim
+
let c = 'normal F' . a:delim . 'vf' . a:delim
endif
 
  +
endif
execute c
+
execute c
endfunction
+
endfunction
 
omap a" :call StringObject('"','a')&lt;cr&gt;
 
omap i" :call StringObject('"','i')&lt;cr&gt;
 
omap a' :call StringObject("'",'a')&lt;cr&gt;
 
omap i' :call StringObject("'",'i')&lt;cr&gt;
  +
</pre>
   
 
Using this method allows you to use all the built-in Vim operators on StringObjects without defining four mappings for each operator.
omap a" :call StringObject('"','a')&lt;cr&gt;
 
omap i" :call StringObject('"','i')&lt;cr&gt;
 
omap a' :call StringObject("'",'a')&lt;cr&gt;
 
omap i' :call StringObject("'",'i')&lt;cr&gt;
 
   
 
To get equivalent visual mode mappings add &lt;esc&gt; to the start:
Using this method allows you to use all the builtin vim operators on StringObjects without defining four mappings for each operator.
 
   
  +
<pre>
To get equivalent visual mode mappings add &lt;esc&gt; to the start:
 
 
vmap a" &lt;esc&gt;:call StringObject('"','a')&lt;cr&gt;
 
vmap a" &lt;esc&gt;:call StringObject('"','a')&lt;cr&gt;
+
vmap i" &lt;esc&gt;:call StringObject('"','i')&lt;cr&gt;
vmap i" &lt;esc&gt;:call StringObject('"','i')&lt;cr&gt;
+
vmap a' &lt;esc&gt;:call StringObject("'",'a')&lt;cr&gt;
vmap a' &lt;esc&gt;:call StringObject("'",'a')&lt;cr&gt;
+
vmap i' &lt;esc&gt;:call StringObject("'",'i')&lt;cr&gt;
  +
</pre>
vmap i' &lt;esc&gt;:call StringObject("'",'i')&lt;cr&gt;
 
 
 
 
benc at bur dot st
 
, March 24, 2005 14:04
 
----
 
this is great function, but when I use it, after command cursor jumps at the start of current line..
 
maybe it is posible to fix it?
 
   
sirex--AT--takas.lt
 
, March 29, 2005 3:09
 
 
----
 
----
<!-- parsed by vimtips.py in 0.914097 seconds-->
 

Revision as of 09:58, 9 December 2007

Tip 901 Printable Monobook Previous Next

created March 24, 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>