├── README ├── autoload └── camelcasemotion.vim ├── doc └── camelcasemotion.txt └── plugin └── camelcasemotion.vim /README: -------------------------------------------------------------------------------- 1 | This is a mirror of http://www.vim.org/scripts/script.php?script_id=1905 2 | 3 | DESCRIPTION 4 | Vim provides many built-in motions, e.g. to move to the next word, or end of 5 | the current word. Most programming languages use either CamelCase 6 | ("anIdentifier") or underscore_notation ("an_identifier") naming conventions 7 | for identifiers. The best way to navigate inside those identifiers using Vim 8 | built-in motions is the [count]f{char} motion, i.e. f{uppercase-char} or f_, 9 | respectively. But we can make this easier: 10 | 11 | This plugin defines motions ',w', ',b' and ',e' (similar to 'w', 'b', 'e'), 12 | which do not move word-wise (forward/backward), but Camel-wise; i.e. to word 13 | boundaries and uppercase letters. The motions also work on underscore notation, 14 | where words are delimited by underscore ('_') characters. From here on, both 15 | CamelCase and underscore_notation entities are referred to as "words" (in double 16 | quotes). Just like with the regular motions, a [count] can be prepended to move 17 | over multiple "words" at once. Outside of "words" (e.g. in non-keyword 18 | characters like // or ;), the new motions move just like the regular motions. 19 | 20 | Vim provides a built-in 'iw' text object called 'inner word', which works in 21 | operator-pending and visual mode. Analog to that, this plugin defines inner 22 | "word" motions 'i,w', 'i,b' and 'i,e', which select the "word" (or multiple 23 | "words" if a [count] is given) where the cursor is located. 24 | 25 | USAGE 26 | Use the new motions',w',',b' and ',e' in normal mode, operator-pending mode (cp. 27 | :help operator), and visual mode. For example, type 'bc,w' to change 'Camel' in 28 | 'CamelCase' to something else. 29 | 30 | EXAMPLE: motions 31 | Given the following CamelCase identifiers in a source code fragment: 32 | set Script31337PathAndNameWithoutExtension11=%~dpn0 33 | set Script31337PathANDNameWITHOUTExtension11=%~dpn0 34 | and the corresponding identifiers in underscore_notation: 35 | set script_31337_path_and_name_without_extension_11=%~dpn0 36 | set SCRIPT_31337_PATH_AND_NAME_WITHOUT_EXTENSION_11=%~dpn0 37 | 38 | ,w moves to ([x] is cursor position): [s]et, [s]cript, [3]1337, [p]ath, 39 | [a]nd, [n]ame, [w]ithout, [e]xtension, [1]1, [d]pn0, dpn[0], [s]et 40 | ,b moves to: [d]pn0, [1]1, [e]xtension, [w]ithout, ... 41 | ,e moves to: se[t], scrip[t], 3133[7], pat[h], an[d], nam[e], withou[t], 42 | extensio[n], 1[1], dpn[0] 43 | 44 | EXAMPLE: inner motions 45 | Given the following identifier, with the cursor positioned at [x]: 46 | script_31337_path_and_na[m]e_without_extension_11 47 | 48 | v3i,w selects script_31337_path_and_[name_without_extension_]11 49 | v3i,b selects script_31337_[path_and_name]_without_extension_11 50 | v3i,e selects script_31337_path_and_[name_without_extension]_11 51 | Instead of visual mode, you can also use c3i,w to change, d3i,w to delete, 52 | gU3i,w to upper-case, and so on. 53 | 54 | Source: Based on http://vim.wikia.com/wiki/Moving_through_camel_case_words by 55 | Anthony Van Ham. 56 | -------------------------------------------------------------------------------- /autoload/camelcasemotion.vim: -------------------------------------------------------------------------------- 1 | " camelcasemotion.vim: Motion through CamelCaseWords and underscore_notation. 2 | " 3 | " DEPENDENCIES: 4 | " - Requires Vim 7.0 or higher. 5 | " 6 | " Copyright: (C) 2007-2011 Ingo Karkat 7 | " The VIM LICENSE applies to this script; see ':help copyright'. 8 | " 9 | " Maintainer: Ingo Karkat 10 | " REVISION DATE REMARKS 11 | " 1.52.002 18-Oct-2011 FIX: Correct forward-to-end motion over 12 | " lowercase part in "lowerCamel". Found this by 13 | " chance in GitHub fork by Kevin Lee (bkad). 14 | " BUG: Correct wrong stop on second letter of 15 | " ACRONYM at the beginning of a word "AXBCText". 16 | " 1.50.001 05-May-2009 Do not create mappings for select mode; 17 | " according to|Select-mode|, printable character 18 | " commands should delete the selection and insert 19 | " the typed characters. 20 | " Moved functions from plugin to separate autoload 21 | " script. 22 | " file creation 23 | 24 | "- functions ------------------------------------------------------------------" 25 | function! s:Move( direction, count, mode ) 26 | " Note: There is no inversion of the regular expression character class 27 | " 'keyword character' (\k). We need an inversion "non-keyword" defined as 28 | " "any non-whitespace character that is not a keyword character" (e.g. 29 | " [!@#$%^&*()]). This can be specified via a non-whitespace character in 30 | " whose place no keyword character matches (\k\@!\S). 31 | 32 | "echo "count is " . a:count 33 | let l:i = 0 34 | while l:i < a:count 35 | if a:direction == 'e' 36 | " "Forward to end" motion. 37 | "call search( '\>\|\(\a\|\d\)\+\ze_', 'We' ) 38 | " end of ... 39 | " number | ACRONYM followed by CamelCase or number | lowercase followed by CamelCase, ACRONYM, or number | CamelCase | underscore_notation | non-keyword | word 40 | " Note: Branches are ordered from specific to unspecific so that 41 | " in case of multiple matches, the more specific (and usually 42 | " longer) one it used. 43 | call search( '\d\+\|\u\+\ze\%(\u\l\|\d\)\|\l\+\ze\%(\u\|\d\)\|\u\l\+\|\%(\a\|\d\)\+\ze_\|\%(\k\@!\S\)\+\|\%(_\@!\k\)\+\>', 'We' ) 44 | " Note: word must be defined as '\k\>'; '\>' on its own somehow 45 | " dominates over the previous branch. Plus, \k must exclude the 46 | " underscore, or a trailing one will be incorrectly moved over: 47 | " '\%(_\@!\k\)'. 48 | if a:mode == 'o' 49 | " Note: Special additional treatment for operator-pending mode 50 | " "forward to end" motion. 51 | " The difference between normal mode, operator-pending and visual 52 | " mode is that in the latter two, the motion must go _past_ the 53 | " final "word" character, so that all characters of the "word" are 54 | " selected. This is done by appending a 'l' motion after the 55 | " search for the next "word". 56 | " 57 | " In operator-pending mode, the 'l' motion only works properly 58 | " at the end of the line (i.e. when the moved-over "word" is at 59 | " the end of the line) when the 'l' motion is allowed to move 60 | " over to the next line. Thus, the 'l' motion is added 61 | " temporarily to the global 'whichwrap' setting. 62 | " Without this, the motion would leave out the last character in 63 | " the line. I've also experimented with temporarily setting 64 | " "set virtualedit=onemore" , but that didn't work. 65 | let l:save_ww = &whichwrap 66 | set whichwrap+=l 67 | normal! l 68 | let &whichwrap = l:save_ww 69 | endif 70 | else 71 | " Forward (a:direction == '') and backward (a:direction == 'b') 72 | " motion. 73 | 74 | let l:direction = (a:direction == 'w' ? '' : a:direction) 75 | 76 | " CamelCase: Jump to beginning of either (start of word, Word, WORD, 77 | " 123). 78 | " Underscore_notation: Jump to the beginning of an underscore-separated 79 | " word or number. 80 | "call search( '\<\|\u', 'W' . l:direction ) 81 | "call search( '\<\|\u\(\l\+\|\u\+\ze\u\)\|\d\+', 'W' . l:direction ) 82 | "call search( '\<\|\u\(\l\+\|\u\+\ze\u\)\|\d\+\|_\zs\(\a\|\d\)\+', 'W' . l:direction ) 83 | " beginning of ... 84 | " word | empty line | non-keyword after whitespaces | non-whitespace after word | number | start of ACRONYM followed by CamelCase or number | CamelCase | underscore followed by ACRONYM, Camel, lowercase or number 85 | " Note: Branches are ordered from unspecific to specific, so that 86 | " the cursor moves the least amount of text. 87 | call search( '\<\D\|^$\|\%(^\|\s\)\+\zs\k\@!\S\|\>\S\|\d\+\|\u\@ 77 | vim camelcasemotion.vba.gz 78 | :so % 79 | To uninstall, use the |:RmVimball| command. 80 | 81 | DEPENDENCIES *camelcasemotion-dependencies* 82 | 83 | - Requires Vim 7.0 or higher. 84 | 85 | ============================================================================== 86 | CONFIGURATION *camelcasemotion-configuration* 87 | 88 | By default, all mappings start with |,| (comma) as the map leader instead of 89 | using . I personally find the default key too far off the 90 | keyboard to be useful for custom motions (which also cannot be repeated via 91 | the |.| (dot) command, so they should be very fast to type repeatedly), but 92 | quite suitable for general, less frequently used custom mappings. 93 | To avoid losing the (rarely used) |,| mapping (which repeats latest f, t, F or 94 | T in opposite direction), you can remap it to ,,: > 95 | nnoremap ,, , 96 | xnoremap ,, , 97 | onoremap ,, , 98 | 99 | If you want to use different mappings, map your keys to the 100 | CamelCaseMotion_? mapping targets _before_ sourcing this script (e.g. in 101 | your |vimrc|). 102 | 103 | EXAMPLE: Replace the default |w|, |b| and |e| mappings instead of defining 104 | additional mappings |,w|, |,b| and |,e| : > 105 | map w CamelCaseMotion_w 106 | map b CamelCaseMotion_b 107 | map e CamelCaseMotion_e 108 | sunmap w 109 | sunmap b 110 | sunmap e 111 | 112 | EXAMPLE: Replace default |iw| text-object and define |ib| and |ie| motions: > 113 | omap iw CamelCaseMotion_iw 114 | xmap iw CamelCaseMotion_iw 115 | omap ib CamelCaseMotion_ib 116 | xmap ib CamelCaseMotion_ib 117 | omap ie CamelCaseMotion_ie 118 | xmap ie CamelCaseMotion_ie 119 | 120 | ============================================================================== 121 | KNOWN PROBLEMS *camelcasemotion-known-problems* 122 | 123 | - A degenerate CamelCaseWord containing '\U\u\d' (e.g. "MaP1Roblem") confuses 124 | the operator-pending and visual mode ,e mapping if 'selection' is not set to 125 | "exclusive". It'll skip "P" and select "P1" in one step. As a workaround, 126 | use ',w' instead of ',e'; those two mappings have the same effect inside 127 | CamelCaseWords, anyway. 128 | - The operator-pending and visual mode ,e mapping doesn't work properly when 129 | it reaches the end of the buffer; the final character of the moved-over 130 | "word" remains, and a beep is issued. As a workaround, use the default 'e' 131 | motion instead of ',e'. 132 | - When the Vim setting 'selection' is not set to "exclusive", a 133 | forward-backward combination in visual mode (e.g. 'v,w,b') selects one 134 | additional character to the left, instead of only the character where the 135 | motion started. Likewise, extension of the visual selection from the front 136 | end is off by one additional character. 137 | 138 | ============================================================================== 139 | TODO *camelcasemotion-todo* 140 | 141 | IDEAS *camelcasemotion-ideas* 142 | 143 | - Use search('\%#.\%$', 'cnW') to detect the operator-pending motion to the 144 | end of the buffer and temporarily append a new line / space character to 145 | work around the end-of-buffer problem. Unfortunately, the addition cannot be 146 | undone as part of the motion (won't work then), so we'd have to use an 147 | autocmd to remove it later. 148 | 149 | - Make separator character (currently underscore) configurable so that dashes, 150 | asterisks, etc. can be added as well. Idea is to replace the single /_/ with 151 | a collection /[-_*]/ built from g:CamelCaseMotion_SeparatorCharacters. Check 152 | out interference with containment in 'iskeyword' and think through need to 153 | buffer-local setting. (Submitted anonymously 13-Nov-2010 on Vim Tips Wiki.) 154 | 155 | ============================================================================== 156 | HISTORY *camelcasemotion-history* 157 | 158 | 1.52 12-Nov-2011 159 | - FIX: Correct forward-to-end motion over lowercase part in "lowerCamel". 160 | Found this by chance in GitHub fork by Kevin Lee (bkad). 161 | - BUG: Correct wrong stop on second letter of ACRONYM at the beginning of a 162 | word "AXBCText". 163 | - The motion functionality is automatically tested via a runVimTests 164 | (vimscript #2565) test suite. 165 | 166 | 1.51 30-Sep-2011 167 | Use for mapping instead of default mapping. 168 | 169 | 1.50 05-May-2009 170 | - Do not create mappings for select mode; according to |Select-mode|, 171 | printable character commands should delete the selection and insert the 172 | typed characters. Now using :xmap to only target visual mode. 173 | - Moved functions from plugin to separate autoload script. 174 | - Split off documentation into separate help file. Now packaging as VimBall. 175 | 176 | 1.40 30-Jun-2008 177 | BF: Now using :normal! to be independent from any user mappings. Thanks to 178 | Neil Walker for the patch. 179 | 180 | 1.40 19-May-2008 181 | BF: Wrong forward motion stop at the second digit if a word starts with 182 | multiple numbers (e.g. 1234.56789). Thanks to Wasim Ahmed for reporting this. 183 | 184 | 1.40 24-Apr-2008 185 | ENH: Added inner "word" text objects 'i,w' etc. that work analoguous to the 186 | built-in |iw| text object. Thanks to David Kotchan for this suggestion. 187 | 188 | 1.30 20-Apr-2008 189 | The motions now also stop at non-keyword boundaries, just like the regular 190 | motions. This has no effect inside a CamelCaseWord or inside 191 | underscore_notation, but it makes the motions behave like the regular motions 192 | (which is important if you replace the default motions). Thanks to Mun Johl 193 | for reporting this. 194 | 195 | 1.30 09-Apr-2008 196 | - Allowing users to use mappings different than ,w ,b ,e by defining 197 | CamelCaseMotion_? target mappings. This can even be used to replace 198 | the default 'w', 'b' and 'e' mappings, as suggested by Mun Johl. 199 | - Now requires VIM 7.0 or higher. 200 | 201 | 1.20 29-May-2007 202 | ENH: The visual mode motions now also (mostly) work with the (default) setting 203 | 'set selection=inclusive', instead of selecting one character too much. 204 | 205 | 1.10 28-May-2007 206 | Incorporated major improvements and simplifications done by Joseph Barker. 207 | 208 | 1.00 22-May-2007 209 | First published version. 210 | 211 | 0.01 11-Oct-2005 212 | Started development based on vimtip #1016 by Anthony Van Ham. 213 | 214 | ============================================================================== 215 | Copyright: (C) 2007-2011 Ingo Karkat 216 | The VIM LICENSE applies to this script; see |copyright|. 217 | 218 | Maintainer: Ingo Karkat 219 | ============================================================================== 220 | vim:tw=78:ts=8:ft=help:norl: 221 | -------------------------------------------------------------------------------- /plugin/camelcasemotion.vim: -------------------------------------------------------------------------------- 1 | " camelcasemotion.vim: Motion through CamelCaseWords and underscore_notation. 2 | " 3 | " DEPENDENCIES: 4 | " - Requires Vim 7.0 or higher. 5 | " - camelcasemotion.vim autoload script. 6 | " 7 | " Copyright: (C) 2007-2011 Ingo Karkat 8 | " The VIM LICENSE applies to this script; see ':help copyright'. 9 | " 10 | " Source: Based on vimtip #1016 by Anthony Van Ham. 11 | " Maintainer: Ingo Karkat 12 | " REVISION DATE REMARKS 13 | " 1.51.020 30-Sep-2011 Use for mapping instead of 14 | " default mapping. 15 | " 1.50.019 05-May-2009 Do not create mappings for select mode; 16 | " according to|Select-mode|, printable character 17 | " commands should delete the selection and insert 18 | " the typed characters. 19 | " Moved functions from plugin to separate autoload 20 | " script. 21 | " Split off documentation into separate help file. 22 | " Now cleaning up Create...Mappings functions. 23 | " 1.40.018 30-Jun-2008 Minor: Removed unnecessary