How to apply syntax file for vim? - c++

I've read vim manual pages and also bunch of tutorials and managed to set up my C++ environment in vim, but to apply a syntax file is probably a science fiction.
I downloaded a 'cpp.vim' which is a syntax file, now how do I apply that file so my code start shining??
I tried to place a file into following directories:
~/.vim/after/syntax/
~/.vim/syntax
/usr/share/vim/vim73/syntax
(but under different name, so not replacing old file)
none of that work, and anyway if I place a file into $VIMRUNTIME/syntax under some crazy name, then how do i apply it into vim?
Here is my .vimrc setting:
"
" vim settings
"
set t_Co=256
set nocompatible
set laststatus=2
"set ruler
"set ballooneval
"set balloondelay=400
"set balloonexpr=""
filetype off
"
" vundle settings and plugins
"
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
"Vundle Plugin manager
Plugin 'gmarik/Vundle.vim' "vundle plugin manager
"Core plugins
Plugin 'ervandew/supertab' "completition with tab
Plugin 'scrooloose/syntastic' "error checking
Plugin 'vim-scripts/OmniCppComplete' "auto complete
"Plugin 'octol/vim-cpp-enhanced-highlight' "syntax highlighting
Plugin 'vim-scripts/Cpp11-Syntax-Support'
Plugin 'bling/vim-airline' "statusline and tabline
"Sipet plugins
Plugin 'MarcWeber/vim-addon-mw-utils' "required by snipmate
Plugin 'tomtom/tlib_vim' "required by snipmate
Plugin 'garbas/vim-snipmate' "insert snipets
Plugin 'honza/vim-snippets' "optional snipets
Plugin 'vim-scripts/a.vim' "Switching between source and header file
Plugin 'chazy/cscope_maps' "Allows searching code
Plugin 'vim-scripts/argtextobj.vim' "Text-object like motion for arguments
Plugin 'wincent/Command-T' "Fast file navigation
Plugin 'vim-scripts/taglist.vim' "Source code browser (requires exuberant-ctags > apt-get install)
Plugin 'ddollar/nerdcommenter' "wrangle your code comments, regardless of filetype
Plugin 'scrooloose/nerdtree' "explore your filesystem and to open files and directories
Plugin 'tpope/vim-unimpaired' "provides a lot of useful mappings
Plugin 'majutsushi/tagbar' "browsing the tags of source code files
Plugin 'chrisbra/NrrwRgn' "focussing on a region and making the rest inaccessible
Plugin 'vim-scripts/ZoomWin' "zoom into a window and out again
Plugin 'terryma/vim-multiple-cursors'
Plugin 'jeetsukumaran/vim-buffergator' "listing,navigating, and selecting buffers for edit
Plugin 'bronson/vim-trailing-whitespace' "causes all trailing whitespace to be highlighted in red
Plugin 'mileszs/ack.vim' "uses ack to search inside the current directory for a pattern
Plugin 'myusuf3/numbers.vim' "intelligently toggling line numbers
Plugin 'Lokaltog/vim-easymotion' "provides a much simpler way to use some motions in vim
call vundle#end()
filetype plugin indent on
"
" ariline options
"
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#left_sep = ' '
let g:airline#extensions#tabline#left_alt_sep = '|'
"
" syntastic settings
"
let g:syntastic_enable_signs = 1
let g:syntastic_cpp_checkers = ['gcc']
let g:syntastic_auto_jump = 1
let g:syntastic_enable_balloons = 1
let g:syntastic_cpp_compiler = 'g++'
let g:syntastic_cpp_compiler_options = '-std=c++11'
let g:syntastic_cpp_check_header = 1
let g:syntastic_cpp_auto_refresh_includes = 1
let g:syntastic_always_populate_loc_list = 1
syntax on
colorscheme slate
... etc
other options are plugins and plugin settings..
The Question is simple, how do I apply a syntax file ??

All you need to do is make sure the filetype is loaded properly. So if the syntax file is called cpp.vim make sure set ft? returns cpp. You should place it in $HOME/.vim/syntax. It should get loaded automatically. Assuming filetype on and syntax on is set.
The only way to make sure the syntax file isn't loaded properly is to use :scriptnames. Make sure the file isn't listed if it is your problem is something different then what you have described.

Ok, I managed to get things work :)
I downloaded two syntax files, one of which does do the job by highlighting the code and other which for some unknown reason does not.
The fact that I have Vundle installed as a plugin manager there was no need to place a syntax file anywhere because that part handles Vundle it self...
All I had to do is is to remove the syntax file that does not work. and also make several tests to see the difference between default syntax highlighting and custom with file.
to enable syntax highlighting only one option is needed placed anywhere in the .vimrc file:
syntax on
or
syntax enable
difference between these two is that 'on' switch does search for first cpp file it find inside $VIMRUNTIME directory while enable switch searches in .vim directory too.
more info about that here
To make syntax highlighting more 'nice' I also downloaded color schemes which makes code much more readable.
And if you just want to know how to apply that piece of file here is how:
Download it with Vundle and place this at the end of your .vimrc file:
colorscheme molokai
Where molokai is the name of colorscheme downloaded with Vundle :D
YES!!!
thanks you FDinoff for trying to help! you were right about :scriptnames which actually helped me to figure out that my file isn't loaded ;)

Related

Syntax highlighting of unsupported language in WebStorm with TextMate bundle

The usual answer to this question is, "download and add a suitable TextMate bundle." I have done that.
I'm working in WebStorm on a React project using GraphQL. It has a lot of .vtl (Velocity Template Language) files that do not have syntax highlighting. I found out that I can add a TextMate bundle for VTL in order to get highlighting. I got this bundle from https://github.com/textmate/java-velocity.tmbundle. After adding it to WebStorm, there still isn't an appropriate file type, and .vtl files still don't have syntax highlighting.
So, I tried to create a new custom file type, manually supplying lists of keywords to highlight, which make the code slightly more readable. However, every .vtl file is now entirely marked with squiggly red, saying, "Unexpected tokens." I just wanted keyword highlighting, not incorrect assumptions about grammar.
Is this a problem with WebStorm, the bundle, or me? How can I get some basic syntax highlighting specifically for .vtl files?
Note that the TextMate plugin is installed.
The bundle only provides highlighting for *.vm files. Please check if the issue persists if you open java-velocity.tmbundle\Syntaxes\Velocity.tmLanguage file in some text editor and add vtl to the list of supported extensions:
<key>fileTypes</key>
<array>
<string>vm</string>
<string>vtl</string>
</array>
and re-start the IDE?
Note that you need to remove your custom file type to get the extension recognized

Using custom fonts on shinyapps.io

I would like to use a custom font in my shiny app (on plots) on shinyapps.io. I have my Roboto-Regular.ttf in the ./www/ directory. And this is the upper portion of my app.R file:
dir.create('~/.fonts')
system("chmod +x ./www/Roboto-Regular.ttf")
system("cp ./www/Roboto-Regular.ttf ~/.fonts/")
system('fc-cache -f -v ~/.fonts/')
system('fc-match Roboto')
library(ggplot2)
library(shiny)
library(shinythemes)
library(extrafont)
font_import(pattern="Roboto",prompt=FALSE)
loadfonts()
print(fonts())
Upon deploying the app, I end up with an error that looks like this:
Registering fonts with R
Scanning ttf files in /usr/share/fonts/, ~/.fonts/ ...
Extracting .afm files from .ttf files...
/home/shiny/.fonts/Roboto-Regular.ttfWarning in gzfile(dest, "w") :
cannot open compressed file '/opt/R/3.5.1/lib/R/library/extrafontdb/metrics/Roboto-Regular.afm.gz', probable reason 'Permission denied'
Error in value[[3L]](cond) : cannot open the connection
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
Execution halted
Does anyone see what might be wrong?
After a bit of struggle I found an even simpler solution that works on shinyapps.io:
Here we go:
Place custom font in www directory: e.g. IndieFlower.ttf from here
Follow the steps from here
This leads to the following upper part of the app.R file:
dir.create('~/.fonts')
file.copy("www/IndieFlower.ttf", "~/.fonts")
system('fc-cache -f ~/.fonts')
Since Linux looks into the .fonts directory to search fonts, you don't need the extrafont package, but you can directly use those fonts like:
ggplot(mapping=aes(x=seq(1,10,.1), y=seq(1,10,.1))) +
geom_line(position="jitter", color="red", size=2) + theme_bw() +
theme(text=element_text(size = 16, family = "IndieFlower"))
This is the answer I received from RStudio regarding this. I haven't tested this out myself.
Hi,
Our developer was able to advise this is due to a possibly unfortunate design choice made when they created extrafont and the associated extrafontdb package. The extrafont font database is stored in the extrafontdb package directory -- that's essentially all that the extrafontdb package is used for.
This means that the extrafontdb directory needs to be user-writable. If the user installs the package, this will work fine, but if root installs the package (as is the case on shinyapps.io), then it won't work.
One potential workaround is to install the extrafontdb package to library that is in subdirectory of the app.
To do it: create an r-lib/ subdir, and download the extrafontdb source package there:
dir.create('r-lib')
download.file('https://cran.r-project.org/src/contrib/extrafontdb_1.0.tar.gz','r-lib/extrafontdb_1.0.tar.gz')
When deployed, the app will include this r-lib/ subdirectory and the extrafontdb source package.
Then, at the top of the app, install the extrafontdb package from the source package, into the r-lib directory.
.libPaths(c('r-lib', .libPaths()))
install.packages('r-lib/extrafontdb_1.0.tar.gz',type = 'source',repos = NULL)
They deployed an app on shinyapps.io that does the extrafontdb installation, and it works fine. The libpath is set so so that install.packages() will install from the provided source package to the r-lib/ subdirectory of the app.
Please let us know if you're able to implement the above or have any additional questions.
Thanks,
Adding an alternative answer to symbolrush's answer which I found did not work. Here was the code I used initially:
# Add fonts to shiny linux server
if (Sys.info()[['sysname']] == 'Linux') {
dir.create('~/.fonts')
fonts = c(
"www/IBMPlexSans-Regular.ttf",
"www/IBMPlexSans-Bold.ttf",
"www/IBMPlexSans-Medium.ttf"
)
file.copy(fonts, "~/.fonts")
system('fc-cache -f ~/.fonts')
}
# Load fonts and set theme
font_paths("fonts")
font_add("IBMPlexSans", regular = "IBMPlexSans-Regular.ttf")
font_add("IBMPlexSans-Bold", regular = "IBMPlexSans-Bold.ttf")
font_add("IBMPlexSans-Medium", regular = "IBMPlexSans-Medium.ttf")
showtext_auto()
The bizarre thing is that the first instance of the app on shinyapps.io worked, including the custom fonts. However when the app went to sleep and was opened a second time, I get this error in the log:
Error in value[[3L]](cond) : font file not found for 'regular' type
I was never able to debug why this was the case, but I tried a simpler solution that has worked perfectly so far. I moved my fonts to a /font folder in the app folder (I don't think using the /www folder is necessary) and added the /font folder using path_folder():
library(showtext)
# Load fonts and set theme
font_paths("fonts")
font_add("IBMPlexSans", regular = "IBMPlexSans-Regular.ttf")
font_add("IBMPlexSans-Bold", regular = "IBMPlexSans-Bold.ttf")
font_add("IBMPlexSans-Medium", regular = "IBMPlexSans-Medium.ttf")
showtext_auto()
I hope this helps anyone who is having problems with their app not running after the first instance, as I could not find the same situation anywhere on stackoverflow.

Why isn't vim-snipmate UltiSnips for Django not working?

I'm running Vim 8.0.124 and I've installed the vim-snipmate plugin to use in my Python and Django development. I followed the instructions by creating a .vimrc file that contains the following:
# ~/.vimrc
set nocompatible " Required by Vundle
filetype off " Required by Vundle
" Begin Vundle settings ==========================================================
"
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'MarcWeber/vim-addon-mw-utils'
Plugin 'tomtom/tlib_vim'
Plugin 'garbas/vim-snipmate'
# Optional
Plugin 'SirVer/ultisnips'
Plugin 'honza/vim-snippets'
call vundle#end()
filetype plugin indent on
"
" End Vundle settings ==========================================================
" SnipMate
autocmd FileType python set ft=python.django
autocmd FileType html set ft=htmldjango.html
" UltiSnips
let g:UltiSnipsExpandTrigger="<tab>"
let g:UltiSnipsJumpForwardTrigger="<c-b>"
let g:UltiSnipsJumpBackwardTrigger="<c-z>"
let g:UltiSnipsEditSplit="vertical"
Installing vim-snipmate includes the creation of these four files:
~/.vim/bundle/vim-snippets/snippets/django.snippets
~/.vim/bundle/vim-snippets/snippets/htmldjango.snippets
~/.vim/bundle/vim-snippets/UltiSnips/django.snippets
~/.vim/bundle/vim-snippets/UltiSnips/htmldjango.snippets
I have two questions. First, why are the UltiSnip Django snippets not working? The snippets in snippets/django.snippets work but those in the UltiSnips django.snippets file don't. If I open a file test.py and type "fdate" where fdate should expand into a Django DateField, nothing happens (other than a tab is entered). Initially, when the UltiSnips weren't working, I went to its Github page and read the instructions which seemed to indicate I should add the SirVer plugin so I did. Even then, then don't seem to work. I should add that what you see above is my entire .vimrc file. Also, I created a completely new ~/.vim directory which only contains the Vundle and vim-snipmate bundles so there shouldn't be any other conflicts.
My second less important question is, when I view any of these snippet files, most of the lines are folded. Is there any way I can configure Vim so that when I open any of these .snippet files, all the folds will be open? They would be easier to search that way.
Did you try with ":setfiletype htmldjango" ? or ":selfiletype django" (https://www.vim.org/scripts/script.php?script_id=1487)
Take a look in the docs, especially under section 4.1.1 How snippets are loaded UltiSnips-how-snippets-are-loaded
I had similar issue, I've created separate directory for snippets, at $HOME/UltiSnips. There I've created directory python, where I've moved files python.snippet and django.snippet. Similarly, you may create directories for another languages and use snippets for another frameworks.
From the docs:
UltiSnips iterates
over the snippet definition directories looking for files with names of the
following patterns: ft.snippets, ft_.snippets, or ft/, where "ft" is the
'filetype' of the current document and "*" is a shell-like wildcard matching
any string including the empty string.

Qt Ubuntu SDK failed to parse plugins.qmltypes

I have a basic App with QML Extension Library app setup with no changes except package identifiers and the similar (email, description, name).
Every time I open app/testApp.qml I get the following error:
Warnings while parsing QML type information of /var/lib/schroot/chroots/click-ubuntu-sdk-14.10-armhf/usr/lib/arm-linux-gnueabihf/qt5/qml/Ubuntu/Components:
Failed to parse '/var/lib/schroot/chroots/click-ubuntu-sdk-14.10-armhf/usr/lib/arm-linux-gnueabihf/qt5/qml/Ubuntu/Components/plugins.qmltypes'.
Error: /var/lib/schroot/chroots/click-ubuntu-sdk-14.10-armhf/usr/lib/arm-linux-gnueabihf/qt5/qml/Ubuntu/Components/plugins.qmltypes:632:19: Expected string literal to contain 'Package/Name major.minor' or 'Name major.minor'.
/var/lib/schroot/chroots/click-ubuntu-sdk-14.10-armhf/usr/lib/arm-linux-gnueabihf/qt5/qml/Ubuntu/Components/plugins.qmltypes:633:36: Expected array literal with only number literal members.
In addition, and this may be entirely different, when I click the Design button, the system doesn't work and there's an error partially hidden by the right menu. The error is:
The problem is the qml plugin files required are not in the currently specified include directories listed on [projectName].qmlproject in the app project root directory.
I was testing on another project of a different type and I followed these instructions (the project was a QML project with a .qmlproject file).
It was near the bottom of the page, like this:
/* List of plugin directories passed to QML runtime */
importPaths: [ "." ,"/usr/bin","/usr/lib/x86_64-linux-gnu/qt5/qml"]
I ran find / -name *qml* and found something that looked to be a suitable directory, with many items that had a good chance of being qml plugins, so I copied that directory, went to [projectName].qmlproject to the statement posted above and added the directory. The final importPaths statement looks like this:
/* List of plugin directories passed to QML runtime */
importPaths: [ "." ,"/usr/bin","/usr/lib/x86_64-linux-gnu/qt5/qml","/var/lib/schroot/chroots/click-ubuntu-sdk-14.10-armhf/usr/lib/arm-linux-gnueabihf/qt5/qml/Ubuntu/Components/" ]
After doing that, I could open the qml file in designer.
The effect also transferred to my other project, which does not have a .qmlproject file. I'm not sure why. I assume there's some kind of memory of the proper file, although I do not know the specifics.

Vim YouCompleteMe configuration

i just installed YouCompleteMe for Vim through vundle. It works, but it shows only the words contained in the current file. I want to use it to develop c++ programs, how can i configure it to show autocompletion from c++ headers file in /usr/include for example? Thanks a lot.
You need to navigate to ~/.vim/bundles/YouCompleteMe and run the installation script with --clang-completer, so do ./install.sh --clang-completer. After it finishes you should have support for C like languages.
You may also need to place let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py' in your ~/.vimrc.
I have installed with pathogen. I tried the above instructions with ./install.sh --clang-complete. After this, it did not work, and I indeed had to add the path. But it was different than in another reply here, namely
let g:ycm_global_ycm_extra_conf = '.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
so there is an extra "third_party/ycmd" in the path.
While the suggestions here might work in the beginning, I am not sure it's the proper way to go. According to YCM developer, whenever you start a project, you need a new .ycm_extra_conf.py file
From https://valloric.github.io/YouCompleteMe/#ubuntu-linux-x64-super-quick-installation
YCM looks for a .ycm_extra_conf.py file in the directory of the opened file or in any directory above it in the hierarchy (recursively); when the file is found, it is loaded (only once!) as a Python module. YCM calls a FlagsForFile method in that module which should provide it with the information necessary to compile the current file. You can also provide a path to a global .ycm_extra_conf.py file, which will be used as a fallback. To prevent the execution of malicious code from a file you didn't write YCM will ask you once per .ycm_extra_conf.py if it is safe to load. This can be disabled and you can white-/blacklist files. See the Options section for more details.
While you might only need to modify the compile flags from the vanilla .ycm_extra_conf.py, I feel it is advisable to create a new file for every project you start.
Everything that the folks here have said is correct. I just want to add that as of 2017, the "install.sh" script is deprecated. Now, you have to use the install.py script instead by typing
./install.py --clang-completer
Also, in your .vimrc file, instead of ".vim/bundle/blahblahblah", you'll need to add a "~/" in front of the address by adding:
let g:ycm_global_ycm_extra_conf = "~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py"
to your .vimrc file, to give it an absolute path from the Home directory so that Vim can find the ".ycm_extra_conf.py" file. Otherwise, you might experience some funny behavior.
I just wanted to add if you don't want to manually define a config file there is this neat little repository that will auto generate it. https://github.com/rdnetto/YCM-Generator