I am new to Vim and using coc-prettier to format my files. I can use the command :Prettier to format my files, but want to do it automatically for ALL file types. I found this, but I need to specify the file types:
"coc.preferences.formatOnSaveFiletypes": ["css", "markdown"],
I want it to auto format on save for all. How can I do this? Thanks!
Apparently formatting on all file types on save is possible as of the commit #1150
Seems to work for me.
The exact implementation:
"coc.preferences.formatOnSaveFiletypes": ["*"]
Taking a look at the schema.json:
"formatterPriority": {
"type": "number",
"default": 0,
"description": "Priority of this languageserver's fomatter."
also referenced in the coc-prettier GitHub - here:
prettier.formatterPriority (default: 1) - priority of fomatter
Change it to -1 if you don't want prettier to have higher priority than
formatter provided by other language server.
Generally speaking coc-prettier uses prettier which on the other hand makes usage of ESLint. Therefore (also for future problems), I'd suggest expanding your search term to broaden up the results. Anyhow, don't forget to properly declare the priority in the configuration file - as mentioned above in the linked documentation.
A little edit regarding a discussion between me and the bounty awarder #DanMacák:
Hence I wanted to find out if something like
"coc.preferences.formatOnSaveFiletypes": '*' is possible. Which I
checked the schema for back then and found that this option is not
viable.
After doing some further research on the added question, I found out that it isn't possible (at least to my knowledge). coc.preferences.formatOnSaveFiletypes only takes an array as an input - see below:
"coc.preferences.formatOnSaveFiletypes": {
"type": "array",
"default": [],
"description": "Filetypes that should run format on save.",
"items": {
"type": "string"
}
Unless there is a way to include every element into an array, via e.g. the asterisk operator, chances are slim you can parse said information into the function. Or to sum things up, the functionality is unfortunately not provided the way you intend to use it.
Set coc.preferences.formatOnSaveFiletypesto ["*"] to enable format on all filetypes.
Coc-Prettier will follow prettier's configurations settings
prettier.formatterPriority (default: 1)
make sure you add the required conditions in the prettier config.
Vim has excellent built in formatting. So you can leverage that to do the formatting for you. Add this to your ~/.vimrc to autoformat the file on save.
autocmd BufWritePost * execute "normal gg=G"
Add the configs you require like tabstop and shiftwidth to format as you require it.
Related
-Edited-
First of all - thanks for the answers.
settings->quote indeed looks like
the right direction.
That being said.. I understand that there is no reality in which I get to keep BOTH my single, AND double quotes,
and just turn off ONLY the quote thing checker/fixer style.
-Original-
My VSCode changes the ['] char to ["] automatically every time it saves.
I have the autosave feature (out of focus, etc.) turned on.
So.. at this point, writing NodeJS is becoming insufferable.
Tried looking through my extensions, settings, and even deleted all the unused and old ones.
Nothing.
I'm down to these 5, and can't seem to find the answer (Of course, "Prettier" was the first thing I checked. so if it's there, I either didn't get it, or missed it.)
In the settings of VS Code, just search for javascript.preferences.quoteStyle to get the exact result. But this is the setting you need to change in order to tell Prettier that you want single quotes instead of double quotes.
You can tell prettier that you prefer single quotes by having a config file (for example, a .prettierrc at the root of your project that has singleQuote set to true. The default is false.
{
"singleQuote": true
}
from https://stackoverflow.com/a/21666354/433570
It's dos based solution though, can it be done for linux based system?
I'm trying to highlight stuff in my log file.
For instance, I want to highlight the line of nginx log which has slower response time than 1 sec.
** edit **
Currently I'm using hi-lock-mode
eg, I put a mark on a line that shows slow response, then use regex & hi-lock to highlight it.
I guess this is ok solution, for now.
I am wondering if there's a better solution.
hi-lock mode with user-defined function rather than regex is what I would hope for.
I would define functions, and mapping between function-color.
Then I would M-x apply [function]
def slow(line):
if ... :
return True
return False
slow: yellow,
iPhone: blue,
I think this would be useful to inspect logs..
I wonder if there's a similar functionality available out there?
Why don't you write your own major mode for your files?
A basic major mode with font-lock support is not hard to implement. There are plenty of documentation on this on the net. All you need is a syntax table (so that Emacs would know which characters start strings etc.) and some font-lock rules for syntax highlighting.
The easiest, though, is to start with an existing one, for example ini-mode, a small major mode for editing Windows-style ini files.
Unless your files have a specific file extension or otherwise follow a specific naming convention, you might want to add an entry to magic-mode-alist, which provides you with a way to recognize specific files based on the content rather than the file name.
If you would like to see your files colored in a terminal window when viewed using more or less, you can use e2ansi, a package that use Emacs to generate an ANSI version of syntax highlighted files.
For example:
:Kb js
or
:Kb javascript
will lead me to my own javascript page
Yes, you can just add additional files to ~/.vim/doc/. As long as you use Vim's help syntax (*name* for tag definitions and |name| for links), you just need to update the help database via :helptags ~/.vim/doc, and then have your knowledge base available via :help name. I personally use that for "cheat pages" for certain filetypes, and to provide summary information for complex plugins.
It should also be possible to put your knowledge base into a separate directory. Then, you would have to reimplement the :help command, probably by temporarily directing the 'tags' value to it and using :tag.
When I type sup then TAB in Sublime Text 3, I get <sup>. That isn't what I want; I only want tab completions based on what is in my current file.
How can I limit tag style tag-completions to just HTML files?
I was hoping to find a setting called tag_complete_file_extensions or similar.
I don't currently understand the implications of these default preference settings, but they don't look like what I want:
{
// Controls what scopes auto complete will be triggered in
"auto_complete_selector": "source - comment, meta.tag - punctuation.definition.tag.begin",
// Additional situations to trigger auto complete
"auto_complete_triggers": [ {"selector": "text.html", "characters": "<"} ],
}
At the least, I'd appreciate an answer pointing me in the right direction. I don't mind forking a package or writing some custom code.
I believe this behaviour is provided by the Emmet (formerly ZenCoding), or similar package and not sublime directly.
Are you sure syntax is set accordingly for the files you get html tag completion?
By default Emmet detects that you are working on html files and enables tag completion (in my case I did not have to configure anything to get this functionality). You should have a closer look at your packages and your configuration and possibly update existing ones.
If you need any further help with the configuration files, I'm happy to help!
Cheers!
First off, I was about to write a long list of if/else statements in vim and realized that 1) there was a better way to do what I was trying to do and 2) SO would be ripe with help on the subject. So! I have a variety of files spread about like
foo/src/file01.C
foo/src/file02.cc
foo/src/file03.c
foo/include/file01.hh
foo/include/file02.h
foo/include/file03.h
If you notice that the C/H, cc/hh, c/h extension may or may not match then you are keen and I'd like you to please help. I've look at things like the following vim scripts from the Vim wiki for "Easily switch between source and header file" and although I only dumped a few hours into a.vim without success, it doesn't seem that the others would work via the docs on that page. So can anyone help out on how to make this work?
A good lead I had was a quick How to Easily Switch between Header and Source topic, but still couldn't make it work.
I guess what I really want is how to avoid the multiple if statements and use real matching to do what I want. I want to look into another directory and if look for a header file of the same name with any familiar extension if it was a source C/C++ file, or look for a source file of any regular extension if it was a header file. Thanks for your help!
UPDATE: I specifically want to open the file in a new tab. I live on vim tabs!
I recommend using the FSwitch plugin. https://github.com/derekwyatt/vim-fswitch
This does exactly what you need out of the box. It is better than a.vim in more than one way, being a rewrite of the idea behind a.vim.
The link you posted presents it as a solution, too.
I have just installed it to my vim configuration and it does its job well.
Enjoy.
Just to make sure I was using the most current version, I downloaded the latest a.vim script (2.18) and copied it into my ~/.vim/plugin directory.
You can define certain variables in your ~/.vimrc file to get a.vim to recognize alternate file extensions.
To get the files in your example to match their alternates I added the following to my ~/.vimrc:
let g:alternateExtensions_C = "H,hh"
let g:alternateExtensions_hh = "C"
These are global variables that allow you to override what's already defined. You'll have to define
both relationships (they don't work both ways).
You can see what the current mappings are by typing:
:echo g:alternateExtensionsDict
If you need to define other mappings, follow the same pattern. What comes after the underscore is the file extension you're editing. What's in the double quotes is a comma-separated list of alternate extensions.
let g:alternateExtensions_<ext> = "<list,of,alt,ext>"
If you have a different directory structure, you can define what paths to search by overriding the g:alternateSearchPath variable. ../src and ../include are already included by default.
:echo g:alternateSearchPath
To open the alternate file in a new tab:
:AT
By the way, the a.vim script is pretty well documented. You might want to open it up and take a look. I found a setting or two that I didn't know about and I've been using it for years ;o)
I hope that helps.
IMO your best option is to adopt existing scripts to use :tabf instead of :e or whatever the scripts use right now to open the counterpart file. You can also try to make the change configurable and submit it to the script author. (I'm pretty sure many would find the enhancement useful.)
That reminded me of a trick I used very long time ago. Instead of guessing where the corresponding source/header files are, I have used at the top of file special comment containing relative path to the counterpart file. Switching was as simple as finding the special comment, extracting file name and opening it. Problem was similar to yours in that file extensions were not predictable. My rational solution was to stop guessing and denote counterparts explicitly in the source code. (This days I would have probably tried to put the relationship table into an external file with a standard name and look it up in VIM using the upward search.)
Two helpful things
:he 'path'
:he tabfind
So you would do
:set path=../,/usr/include/,/home/buildagent/SDKROOT/mysdk/inc
:tabfind error_codes.h
to open error_codes.h from somewhere exotic without having to specify it. Note how vim globbing is very very flexible, so you might not need mucht
:argadd ./**/*.[h,H] | tab sall
will open all header files under the current directory, regardless of how many levels deep. Be careful running this command on a large tree or with symlinks outside the tree