sponsor Vim development Vim logo Vim Book Ad

SameSyntaxMotion : Motions to the borders of the same syntax highlighting.

 script karma  Rating 10/7, Downloaded by 1552  Comments, bugs, improvements  Vim wiki

created by
Ingo Karkat
 
script type
utility
 
description
DESCRIPTION
Vim offers many different powerful motions to position the cursor, but none
leverages the syntactic analysis of the built-in syntax highlighting.

This plugin provides mappings to jump to the borders and next [count]
occurrences of text parsed as the same syntax as under the cursor. So when
you're in a large block of comments, a quick [y will take you to the
beginning of the comment. The ay and iy text objects will select the
surrounding text that belongs to the same syntax.

SEE ALSO
RELATED WORKS
- The SyntaxMotion plugin (vimscript #2965) by Dominique Pellé provides very
  similar motions for normal and visual mode, but no operator-pending and text
  objects. It does not skip over contained sub-syntax items and uses same
  color as the distinguishing property.
- textobj-syntax (vimscript #2716) provides similar ay and iy text objects,
  but doesn't support a [count] to select multiple.

USAGE
"Same syntax" in the context of the mappings and text objects means all of the
following:
1. The same syntax ID is used to highlight the text. Transparent syntax groups
   are ignored.
2. The stack of syntax items includes the one found under the cursor. When a
   syntax contains other syntaxes (possibly with different highlighting),
   these are included. So when you have escape sequences in a string
   ("foo\<CR>bar"), quoted words in a comment (# a "foo" piece), or keywords
   in a docstring (@author Ernie), the entire text is treated as one syntax
   area, even though there are different-colored pieces inside.
3. Unhighlighted whitespace between the same syntax items is skipped. So when
   there are multiple keywords in a row (FOO BAR BAZ), they are treated as one
   area, even though the whitespace between them is not covered by the syntax.

]y                      Go to [count] next start of the same syntax.
]Y                      Go to [count] next end of the same syntax.
[y                      Go to [count] previous start of the same syntax.
[Y                      Go to [count] previous end of the same syntax.
                        The 'wrapscan' setting applies.

ay                      "a syntax" text object, select [count] same syntax
                        areas.
iy                      "inner syntax" text object, select [count] same syntax
                        areas within the same line. Whitespace around the
                        syntax area is not included. Unhighlighted whitespace
                        delimits same syntax items here, so this selects
                        individual keywords.
 
install details
INSTALLATION
The code is hosted in a Git repo at
    https://github.com/inkarkat/vim-SameSyntaxMotion
You can use your favorite plugin manager, or "git clone" into a directory used
for Vim packages. Releases are on the "stable" branch, the latest unstable
development snapshot on "master".

This script is also packaged as a vimball. If you have the "gunzip"
decompressor in your PATH, simply edit the *.vmb.gz package in Vim; otherwise,
decompress the archive first, e.g. using WinZip. Inside Vim, install by
sourcing the vimball or via the :UseVimball command.
    vim SameSyntaxMotion*.vmb.gz
    :so %
To uninstall, use the :RmVimball command.

DEPENDENCIES
- Requires Vim 7.0 or higher.
- Requires the ingo-library.vim plugin (vimscript #4433), version 1.044 or
  higher.
- Requires the CountJump plugin (vimscript #3130), version 1.90 or higher.

CONFIGURATION
For a permanent configuration, put the following commands into your vimrc:

To change the default motion mappings, use:
    let g:SameSyntaxMotion_BeginMapping = 'y'
    let g:SameSyntaxMotion_EndMapping = 'Y'
To also change the [ / ] prefix to something else, follow the instructions for
CountJump-remap-motions.

To change the default text object mappings, use:
    let g:SameSyntaxMotion_TextObjectMapping = 'y'
To also change the a prefix to something else, follow the instructions for
CountJump-remap-text-objects.
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
SameSyntaxMotion-1.10.vmb.gz 1.10 2024-11-12 7.0 Ingo Karkat - CHG: Highlight group / same color is not considered any longer; only the syntax ID itself counts. There's now the SameHighlightMotion.vim plugin for moving along pure highlighting, so let's sharpen the focus of this one. In practice, this change likely makes no difference in most cases, as syntax definitions and colors usually are defined in parallel.
SameSyntaxMotion-1.01.vmb.gz 1.01 2018-11-04 7.0 Ingo Karkat - CountJump 1.9 renames g:CountJump_Context to g:CountJump_TextObjectContext. *** You need to update to CountJump.vim (vimscript #3130) version 1.90! ***
SameSyntaxMotion-1.00.vmb.gz 1.00 2012-12-03 7.0 Ingo Karkat Initial upload
ip used for rating: 18.97.14.85

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github