Vagrant files, for instance, contain code like this:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Is there a way to get Sublime to do something useful with this?
Sublime automatically chooses the syntax highlighting mode based on the file's extension. If you would like a certain extension to be associated with a particular language, just open the file, then choose View -> Syntax -> Open all with current extension as... and pick the language you'd like.
If you are working with files without an extension, or would just like finer-grained control, check out the ApplySyntax plugin. It allows you to define regexes based on file name and file contents, including shebang lines. It could very easily be modified in the settings to read Vim setting strings. It already includes a setting for Vagrantfile, so for your particular example you wouldn't need to modify anything.
Related
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.
I'm not sure if this is something that can be done in the Linux/Unix terminal, so I thought I'd ask.
For certain programs (unfortunately I can't think of any besides bibtex off the top of my head, which on tab auto-'extends' the .aux extension) when I'm finished typing out the file name (minus dot and extension) in front of it, I've noticed that if I hit tab it will 'auto-complete' the dot + file extension.
Is this something you can actually control somehow ? For example, if I wrote a bash script (we'll call it compile.sh) that only worked with *.tex files, I could do the following:
$ ./compile.sh nameOfProgram -- tab and autocomplete .tex extension --
Certainly it's not inherent to the bash script itself, but by some other means that recognizes I'm using said executable.
Sometimes, as is often the case while using LaTeX, there are multiple files with the same base name but different extensions in the same directory - I would like it to only accept that which has the predefined extension.
It is certainly possible to define a custom completion function for compile.sh which only matches *.tex files. Writing a good completion function is a broad task, but you can do something very simple with
# Add to your .bashrc file to enable in every shell, not just
# the current shell
complete -G '*.tex' compile.sh
which will only treat files matching *.tex as valid completions for any argument to compile.sh.
I have build_config (and other *_config files), GdbRun and build.txt files which are basically bash shell scripts.
How could I associate these files with shell syntax ? To place a pattern like
'if filename is *_config or GdbRun or build.txt' somewhere.
BufferScroll plugin can remember most of view settings for a particular file including syntax.
Just install BufferScroll and change the syntax manually using the status bar or the command palette and it'll remember it next time you open that file.
In vim, how to I find all occurrences of a variable in files under a certain directory?
I know vimgrep works sometimes, but it looks for text only and doesn't work if other classes have variables of the same name and I only want the variable under a specific class.
What should I do? Or should I get an IDE instead?
Why would you want to use another IDE when you already have one? Vim is an IDE that is configurable and usable for different languages..
You could use cscope to build a database of your code. This database
Allows searching code for:
all references to a symbol
global definitions
functions called by a function
functions calling a function
text string
regular expression pattern
a file
files including a file
Further features of Cscope:
Curses based (text screen)
An information database is generated for faster searches and later reference
The fuzzy parser supports C, but is flexible enough to be useful for C++ and Java, and for use as a generalized 'grep database' (use it to browse large text documents!)
Has a command line mode for inclusion in scripts or as a backend to a GUI/frontend
Runs on all flavors of Unix, plus most monopoly-controlled operating systems.
Once your database is created, you could browse through the usages of your variables, functions, etc.
Edit (slightly off-topic):
another cool thing that's quite handy when working with Vim on code is the taglist plugin that uses Ctags:
The "Tag List" plugin is a source code browser plugin for Vim and
provides an overview of the structure of source code files and allows
you to efficiently browse through source code files for different
programming languages.
cscope step by step example
Go to the base directory of your project, and run:
cscope -Rb
This generates a cscope.out file which contains the parsed information. Generation is reasonably fast, even for huge projects like the Linux kernel.
Note that cscope is not designed to work with other languages other than C. Sometimes it does work for other C-like syntax languages like Python, and you can force it to recognize those files with hacks such as cscope -Rb -s * and others mentioned at: Using cscope to browse Python code with VIM? but it won't work as well as for C.
Open vim, and run:
:cs add cscope.out
:cs find s my_func
s is a mnemonic for symbol. The other cscope provided queries are also possible.
The cscope interface (ouside Vim) also has a variable assignment query (subset of symbol occurrences) which Vim does not seem to offer (?)
This adds a list of the callers to the quickfix list, which you can open with:
:copen
Go to the line that interests you and hit enter to jump there.
See also:
automatically add the nearest database (parent directories) when you enter a file: how to auto load cscope.out in vim
for function calls: How to find the callers and callee of a function in C code in vi/vim?
I just started using the spell checking feature in vim. According to the documentation, zg adds the current word to the first spellfile.
Is it recommended to create an own, empty spellfile, so that the English one doesn't get littered? If so: How do I create an empty spell list? An empty appropriately named file yields "E757: This does not look like a spell file".
Using your own additional spell files is both the recommended and default way of doing custom spelling. In Vim you have a 'root' spell file based on the current language and encoding, and an optional quantity of 'additional' spell files.
The easiest way to create an additional spell file (if you haven't already) is simply to add or remove a word using zg or zw - If 'spellfile' is empty, an new additional spell file will be created in the first writeable directory in 'runtimepath'.
The naming of spell files can be a bit tricky. Firstly, Vim takes a text file containing a list of words and converts it into a binary file which is a lot quicker to work with internally. The reason you're getting an error E757 is that Vim is attempting to load the binary spell file, but it's not finding the marker text at the start of the file, so it's bailing out.
To create your own spell files the filename needs to be in the format "mySpellFile.en.utf-8.add" where spelllang=en and encoding=utf-8.