I’m trying to write a few functions that will let me move around easier in JavaScript source files. The first thing that I realize is that I need to be aware of what scope my cursor is in. Looking through Vim’s built in functions, I don’t see anything that that does this for me automatically.
My question is, do I need to attack this problem as if Vim has no concept of scope or block structure? That is, find the first ‘{‘ and it’s matching pair, define everything inbetween as scopeLevel0, find the next ‘{‘ and it’s matching pair and call it scopeLevel1… etc. This seems naive to me and am wondering if Vim can allow me to access and navigate between blocks automatically.
You should explain the kind of navigation you want to achieve.
Vim has the concept of scope which is defined in the syntax file for your language. There are a number of functions displaying the scope of your cursor floating here and elsewhere. The one I have in my .vimrc is:
Navigating inside a document, specifically code, is already a great strength of Vim with text-object motions like
(and)or[(,[{,])and]}or[mand]m… See:help navigation. Coupled with other motions like*,#,/and?or tags you already have a lot of power in your fingers.Do you want more granularity?
For more specific needs you might want to define your own text-objects. There is a script called textobj-user designed to assist in that task. Many special text-objects have been created with or without this tool and made available on vim.org.