vim search by syntax-highlighting type - search

I'm adding i18n to an existing project (web application). This involves replacing every bit of static text with calls to an i18n library. It would be convenient to be able to search for this text rather than rely on syntax highlighting to identify it visually.
In vim, is it possible to search within a file for occurrences of a certain highlighting type?
Something like:
/[%type=Boolean]
Sub 'Boolean' with 'Comment', 'htmlTag', or any group defined in your syntax highlighting file.

This plugin will do it for you

Take a look at the answer I gave here:
Vim search in C/C++ code lines
I think my :SearchInside command will do what you want.

Related

How to create a centralized syntax file that be able to recognize multiple parts with different syntaxes?

For i.e: I'd like to have a custom syntax file, may be called sugar.vim that includes multiple other syntax files(?) to have the ability to highlight, maybe a paragraph as python.vim and another paragraph as javascript.vim, may be separated by newline (paragraphs often distinct by newline)
The real case that I often catch myself writing a document (non-extension file) other than real config a specific filetype (specific extension file), but for clear readability in the document filetype (we called sugar above). I'm thinking about a mechanism to recognize and highlight different parts of a filetype as different syntaxes.
To narrow down this case. How would it be to have a syntax file called sugar.vim that would be able to recognize python syntax and javascript syntax in files that have an extension of .sugar then the recognized python text should have highlights applied as a normal python file, same for javascript part. All recognized text must be separated by newline (at least one before and one after that text)
Sample:
# this is a sample text for this question
# i'm writing a document that has an extension of `.sugar`
def py_func1(arg1, arg2) # python.vim and its highlights applied here.
print("bello world!")
square = function(x) { # javascript.vim and its highlights applied here.
return x * x;
};
System: gvim 8.1 / windows10
Thanks in advances.
Vim supports that with the :help :syn-include command. As it's intended for syntax script writers leveraging other syntaxes, its use is somewhat complicated, and it's not really suited for interactive, on-demand use.
My SyntaxRange plugin provides commands and functions to set up regions in the current buffer that either use a syntax different from the buffer's 'filetype', or completely ignore the syntax. With it, it's trivial to dynamically add a particular syntax highlighting for a range of lines, and public API functions also make the programmatic definition easier.
You're looking for :help :syn-include.
Excerpt from vim help :
If top-level syntax items in the included syntax file are to be
contained within a region in the including syntax, you can use the
":syntax include" command:
:sy[ntax] include [#{grouplist-name}] {file-name}
All syntax items declared in the included file will have the
"contained" flag added. In addition, if a group list is specified,
all top-level syntax items in the included file will be added to
that list. >
" In perl.vim:
:syntax include #Pod :p:h/pod.vim
:syntax region perlPOD start="^=head" end="^=cut" contains=#Pod
When {file-name} is an absolute path (starts with "/", "c:", "$VAR"
or "") that file is sourced. When it is a relative path
(e.g., "syntax/pod.vim") the file is searched for in 'runtimepath'.
All matching files are loaded. Using a relative path is
recommended, because it allows a user to replace the included file
with his own version, without replacing the file that does the ":syn
include".
As long as you can clearly define boundaries for your embedded language regions it is fairly straight forward to achieve this.
You can also refer to https://github.com/tpope/vim-markdown/blob/master/syntax/markdown.vim for reference on how tpope embeds other syntax definitions within the markdown syntax, driven by configuration to minimise the number of language syntax's that need embedding for optimal performance.

How to autocomplete in vim based off partial matching via ctags

Example:
In a file in another directory I have a function defined by the following:
def _generator_function_1(self):
passs
In the file of my current directory, I have typed the following:
def test_generI
where I denotes my cursor position.
I would like to use vim's autocompletion functionality (i.e. via ^n or ^p) to autocomplete the function definition to test_generator_function_1. Is there a way of configuring vim autocompletion to match not based off full-prefixes? Or, is there a way in ctags to generate tags based off keywords instead of full function definitions?
EDIT:
To clarify, I am specifically wondering if keyword-based autocompletion exists. I have autocompletion by tags setting up, so if I typed "_gen", then ^n would complete to give me "_generator_function_1". In my example, however, it is because the string is prefixed by "test" that "test_gener" as the starting typed word does not lead to any autocomplete suggestions. So I am wondering if this can somehow be made possible.
Vim doesn't have "autocompletion functionality". It only has "completion", not "autocompletion". You need a plugin for "autocompletion".
No, there's no way to obtain your desired behavior without some serious vimscripting. See :help complete-functions.

How to paste previously prepared code snippets in Vim?

In Windows, using the AutoHotkey utility, it's possible to write simple scripts to expand some text in an editor of choice (e.g. Visual Studio's editor).
For example, if in Visual Studio editor I type:
d1 [TAB]
(i.e. press the keys in sequence: d,1,Tab) the above "d1" text can be replaced with one or more lines of code snippets. The mapping between "d1" and the expanded lines of code is specified in a AutoHotkey script.
This is very convenient e.g. for demos; for example: at some point if I'd like to enter a whole function body, assuming that I associated it to e.g. "d3", I can simply press d3Tab on the keyboard, and I get the function body automatically pasted in the editor in current cursor location; and I can have different code snippets associated to different key combinations, e.g.
d1 --> DoSomething() function definition
d2 --> class Foo definition
d3 --> test code xyz...
Is it possible to achieve the same goal using Vim?
In other words, I'd like to have a set of code snippets previously prepared, and I'd like to paste each one of them in my currently edited source code file in Vim, in a way similar to what I described above.
Basic expansion can be done via the built-in abbreviations, for example:
:inoreabb d1 DoSomething()<CR>{<CR><CR>}<CR><Up><Up>
Read more at :help abbreviations.
snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork. A modern alternative (that requires Python though) is UltiSnips. There are more, see this list on the Vim Tips Wiki.
There are three things to evaluate: First, the features of the snippet engine itself, second, the quality and breadth of snippets provided by the author or others; third, how easy it is to add new snippets.
I have previously used snipMate that does something like what you're describing.
http://www.vim.org/scripts/script.php%3Fscript_id%3D2540

MacVim - edit color theme for javascript

I am using MacVim with the Cobalt theme. I found it very nice, however, coming from Sublime Text, I feel there aren't enough different colours which makes my javscript code hard to read.
For example, I'd like the function name to be coloured to make them stand out a bit more:
myClass.prototype.myFunction = function myFunction() {
// here, I'd like "myClass" to have a different color from the text
// same for "prototype" and "myFunction"
}
Another example is the use of methods:
myArray.pop();
// I'd like to change the color of ".pop()" for more visibility
How can I add these types of patterns?
A syntax script parses the programming language into different groups (which can be listed via :syntax list). A colorscheme then prescribes how to color and format each individual group.
So, if there are distinct groups, but your colorscheme just assigns the same color to it, that can be easily changed by putting
:hi link <syntaxGroup> <highlightGroup>
commands into your ~/.vimrc.
The detail of parsing depends on the language and syntax script. Extending an existing syntax (to parse out more details) is possible, but complex. For JavaScript, there exist some alternatives (like this) to the built-in syntax script; you might want to give those a try.
PS: :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin.
I use the plugin vim-javascript-syntax.

Generally, how do I "go to definition" in VIM? Then how do I with golang?

Two part question:
First, when using VIM what process do I take and what keys do I type to "go to definition" or "go to declaration" etc.? This document might be the answer to my question, but I can't get it to work, so I'm unsure. It looks like its merely text matching the string rather than finding the true definition. If I can get this to work, then will I be able to jump outside of the current document to a definition/declaration? Or does this only work within a single document?
Second, how do I make this work specifically with the Go programming language? It sure would be nice to "click" the Client in
clnt := &http.Client{Transport: tr}
And be taken to the actual code that defines an http.Client.
Possible? How?
As you guess, gd (and other commands) is merely text matching, vim doesn't understand the syntax as it is just a text editor, :h gd will explain how gd works.
Usually, 'go to definition' is brought by using CTRL-] and tag files. A user manual about this topic can be read by :h 29.1.
First you need to generate a tags file for your project, as latest Exuberant Ctags has supported golang (from here), command
cd /path/to/your/project
ctags -f tags -R --fields=+K+a
will do the job.
Second, open vim, by default vim will find tag files under working directory (according to 'tags' option), if the tag file is found successfully, then CTRL-]` should works well.
Also check two useful plugins Tagbar and Easytags.
For golang, you can use the application godef to do it. The pluging vim-go helps you on setting everything, so, you just type 'gd' in a definition and it goes to the exact definition.
https://github.com/fatih/vim-go/blob/master/doc/vim-go.txt

Resources