├── README ├── autoload └── camelcasemotion.vim ├── plugin └── camelcasemotion.vim └── doc └── camelcasemotion.txt /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\@ 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