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 |
+ | |created=March 24, 2005 |
|complexity=intermediate |
|complexity=intermediate |
||
− | |author=joern_h |
+ | |author=joern_h |
|version=6.0 |
|version=6.0 |
||
|rating=14/5 |
|rating=14/5 |
||
⚫ | |||
− | |text= |
||
− | This |
+ | This function adds support for common operations on string text objects like: |
⚫ | |||
⚫ | |||
⚫ | |||
+ | <pre> |
||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
+ | endif |
||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
+ | startinsert |
||
− | |||
+ | endif |
||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
− | endif |
||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | startinsert |
||
− | |||
− | endif |
||
− | |||
⚫ | |||
− | |||
− | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
nnoremap ya' :call JHStringObject('y',"'",'a')<CR> |
nnoremap ya' :call JHStringObject('y',"'",'a')<CR> |
||
+ | </pre> |
||
⚫ | |||
− | == |
+ | ==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' |
|
− | + | let c = 'normal T' . a:delim . 'vt' . a:delim |
|
− | + | elseif a:mode == 'a' |
|
− | + | let c = 'normal F' . a:delim . 'vf' . a:delim |
|
− | endif |
||
+ | endif |
||
− | + | execute c |
|
− | endfunction |
+ | endfunction |
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | </pre> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | <pre> |
||
⚫ | |||
⚫ | |||
− | |||
− | vmap |
+ | vmap i" <esc>:call StringObject('"','i')<cr> |
− | vmap |
+ | vmap a' <esc>:call StringObject("'",'a')<cr> |
− | vmap |
+ | vmap i' <esc>:call StringObject("'",'i')<cr> |
+ | </pre> |
||
⚫ | |||
− | |||
− | |||
− | |||
− | 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
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>