I am keeping notes in console vim on my laptop, and
I want to add syntax highlighting to my notes in order
to enhance them. However, I don't want to add a
million different filetypes for every area of knowledge
(for example my notes on compilers would have different
keywords than my notes on the FHS), and I also want to
make it easy to share these notes. After doing some
research, I discovered that I can get the behavior I
want, but it doesn't seem like a very elegant solution.
I added the following lines to my .vimrc:
if (filereadable("./.custom_syntax.vim")
let mysyntaxfile = "./.custom_syntax.vim"
syntax enable
else
syntax enable
endif
I don't really like this solution because it still
requires me to ask them to modify their .vimrc, but I
suspect that there's no way to do this without changing
anything on their system. Additionally, if I have any
files in the directory that aren't notes, vim will still
highlight them with the .custom_syntax.vim file because
I don't know what the filetype is.
Is there any better way to accomlish this?
Instead of using the old mysyntaxfile variable, I'd just :syntax enable (once) and then :source the syntax file. You can define an :autocmd that looks for an eponymous Vimscript file next to the original:
" Automatically source an eponymous <file>.vim or <file>.<ext>.vim if it exists, as a bulked-up
" modeline and to provide file-specific customizations.
function! s:AutoSource()
let l:testedScripts = [expand('<afile>') . '.vim']
if expand('<afile>:e') !=# 'vim'
" Don't source the edited Vimscript file itself.
call add(l:testedScripts, expand('<afile>:r') . '.vim')
endif
for l:filespec in l:testedScripts
if filereadable(l:filespec)
execute 'source' fnameescape(l:filespec)
endif
endfor
endfunction
augroup AutoSource
autocmd! BufNewFile,BufRead * call <SID>AutoSource()
augroup END
You do need this or something like this in your Vim configuration, though.
Related
I was testing https://github.com/skwp/dotfiles) and unfortunately it did install a lot of things I do not want.
For example, right now (with empty .vimrc) when I open vim I get
" ============================================================================
" Netrw Directory Listing (netrw v149)
" /Users/user/.vim/bundle
" Sorted by name
" Sort sequence: [\/]$,\<core\%(\.\d\+\)\=\>,\.h$,\.c$,\.cpp$,\~\=\*$,*,\.o$,\.obj$,\.info$,\.swp$,\.bak$,\~$
" Quick Help: <F1>:help -:go up dir D:delete R:rename s:sort-by x:exec
" ============================================================================
thing. I am in the beginning of my journey with VIM so I don't know what is new and right now I don't want to use it.
I am on OSX Mavericks with home-brew installed. Still, I cannot find where is this coming from and how to remove it.
Look at the first few lines of $VIMRUNTIME/plugin/netrwPlugin.vim:
" Load Once: {{{1
if &cp || exists("g:loaded_netrwPlugin")
finish
endif
If you want to disable it, just add
let loaded_netrwPlugin = 1
to your vimrc file.
You can remove the commented section by adding let g:netrw_banner=0 to your .vimrc
Since netrw's pages recommend removing older versions of netrw before updating it, I wound up finding a different way from the answers posted here. This might be especially useful for those of you looking to remove netrw (that is, uninstall netrw) before upgrading:
Download https://www.drchip.org/astronaut/vim/vbafiles/netrwclean.vba.gz, which can also be found under the netrw heading at https://www.drchip.org/astronaut./vim/index.html#NETRW
Open vim and do :so netrwclean.vba.gz, giving the full path to wherever you saved the file if necessary.
:NetrwClean to remove user-local copies or :NetrwClean! to remove both user-local and centrally-installed copies of netrw. Note of course that you'll need the right system-privileges to touch the files that will be removed, so you may wish to start vim as root.
(Optional) download the new version of the netrw vimball and :so netrw.vba.gz to update.
I don’t think that this installed Netrw — it comes bundled with Vim. You can learn more about Netrw on its page at vim.org.
What you’re getting now is (as it says) Netrw’s directory listing, which enables you to navigate within a directory from inside Vim.
Just remove these files (from your vim installation e.g. /usr/share/vim/vim74) and it will be gone
plugin/netrwPlugin.vim
syntax/netrw.vim
autoload/netrw.vim
autoload/netrwSettings.vim
autoload/netrwFileHandlers.vim
nvim:
share/nvim/runtime/autoload/netrw_gitignore.vim
share/nvim/runtime/autoload/netrw.vim
share/nvim/runtime/autoload/netrwFileHandlers.vim
share/nvim/runtime/autoload/netrwSettings.vim
share/nvim/runtime/syntax/netrw.vim
share/nvim/runtime/doc/pi_netrw.txt
share/nvim/runtime/plugin/netrwPlugin.vim
In response to this question on SuperUser, I wrote a small vimscript that will detect the filetype of a symbolic link and change the syntax highlighting:
au BufNewFile,BufRead * if &syntax == '' | silent! execute (':set filetype='.matchstr(resolve(#%),'.[^.]*$')[1:]) | endif
So if I open a symbolic link with no extension, it will look at the extension of the file it points to.
It works, but an unintended consequence is that now the syntax highlighting of my .vimrc file is gone. By default, my .vimrc file has vim syntax highlighting (:echo &syntax returns vim).
But when I add the above line, :echo &syntax returns vimrc (an invalid type).
I don't know why this is happening. Shouldn't &syntax=='' evaluate to false, and thus keep &syntax==vim? I suspect that my code is executing before the syntax highlighting is set to vim. But how (and when) exactly is the syntax highlighting set to vim for .vimrc? Additionally, how can I make my script behave the way it should?
Look in Vim's runtime area for filetype.vim. You can bring it up in vim with:
:e $VIMRUNTIME/filetype.vim
In my version, it looks like this line does the trick:
" Vim script
au BufNewFile,BufRead *vimrc* call s:StarSetf('vim')
Perhaps you want to put your autocmd in ~/.vim/after/filetype.vim. I believe this will cause it to be registered after the system ones, and then &syntax should be set up correctly.
jszakmeister's answer diagnoses the problem accurately: filetype.vim sets the filetype for vimrc.
An alternative solution though, to keep everything contained in in your .vimrc file, is to use:
au BufNewFile,BufReadPre * if &syntax == '' | silent! execute (':set filetype='.matchstr(resolve(#%),'.[^.]*$')[1:]) | endif
Note the change from BufRead to BufReadPre. The change causes this line to execute before filetype.vim. The syntax will be changed to vimrc, but then subsequently changed to vim. The upshot is that the script works as it should, but syntax highlighting for vimrc is preserved.
When the current file is ~/.vimrc, this part of your code
matchstr(resolve(#%),'.[^.]*$')
returns your file name: .vimrc.
I have no idea how you could imagine that /home/username/.vimrc would produce vim.
This is rather obviously a bad approach for the problem you tried to solve but there's no bug: you get exactly what you ask for. Using the filename/extension for filetype can't work reliably (js vs javascript, py vs python…).
You will probably need a filename/filetype list or many if/elseif if you want your code to do what you want.
See :h syntax-loading.
I'm using Vim with a bunch of plugins (pathogen, ctags, snipmate, supertab, ...), and everything works fine for all kinds of file extensions.
However, when I'm try to edit .tex files it presents two problems which seem related. First, Vim starts to work really slow, and second, when I press "any letter + Tab", it tries to auto-complete with words previously written in the text.
One way which I tried to solve those issues, is by removing the supertab plugin from my bundle folder, but it's a not satisfactory solution.
The problem is due to the relativenumber option, when I turned it off the latex edit speed come back to normal.
The following relativenumber, cursorline and MatchParen can slow vim down a lot, especially when dealing with large latex files. When I turn them off then vim becomes much more responsive when dealing with large latex files.
To turn off relative number, type the following in editor mode:
:set nornu
To turn off cursorline, type the following in editor mode:
:set nocursorline
To turn off MatchParen, type the following in editor mode:
:NoMatchParen
If you still want regular line numbering then you can have
:set number
For a more permanent solution, you can also set latex specific settings in your ~/.vimrc file:
" Latex specification
au BufNewFile,BufRead *.tex
\ set nocursorline |
\ set nornu |
\ set number |
\ let g:loaded_matchparen=1 |
The \ and the | are there to allow you add the latex commands over multiple lines.
The other two possible problems are the following being active
cursorline
DoMatchParen
So to make your LᴬTᴇX editing experience much better, you can do something like the following in your ~/.vimrc
au FileType tex setlocal nocursorline
au FileType tex :NoMatchParen
After doing this my Vim is as fast with .tex files as it is with .cpp ones.
I know this is an old issue, but, to anyone seeing this now, it is better to use the ftplugin/ folder in your .vim directory to instruct Vim to enable certain options on a specific file type. Create a file ~/.vim/ftplugin/tex.vim with the following options:
set nocursorline
set nornu
let g:loaded_matchparen=1
This makes Vim load these options only on TeX files (*.tex and related) without resorting to an :autocommand like gloriphobia’s answer does.
Folding is another common source of slowdown. It is normally disabled by default, but perhaps you have enabled it. You can just disable it again:
" (1) if you use the builtin TeX support:
" comment the line in your vimrc that looks like this:
"let g:tex_fold_enabled = ...
" OR, just to be sure, do:
unlet! g:tex_fold_enabled
" (2) if you rather use VimTeX:
" comment the line in your vimrc that looks like this:
"let g:vimtex_fold_enabled = 1
" OR, just to be sure, do:
let g:vimtex_fold_enabled = 0
Note that folding must be enabled/disabled before TeX syntax is loaded in the buffer. Also, Vim option 'foldenable' (toggled by the normal-mode command zi) does not actually clear folding, it just hides it but it’s still there).
However, if you don’t want to give up on folding altogether, I found a single bottleneck in the builtin TeX folding that was responsible for most of the slowdown in my case: the document environment. Simple test: typing stuff just before \begin{document} is reasonably fast, but typing right after it is amazingly laggy. I guess it happens because that environment commonly spans hundreds of lines.
If you use the builtin TeX folding, you can prevent the folding of just the document environment by disabling the texDocZone matchgroup¹. Anyway, why would you want to fold the toplevel contents?
" put this in your vimrc :
au FileType tex :syntax clear texDocZone
" OR put this in ~/.vim/after/syntax/tex.vim :
syntax clear texDocZone
Alternatively, if you have VimTeX, you can replace the builtin TeX folding with the VimTeX’ one. I find it generally better, and it carefully avoids folding the document environment.
" put this in your vimrc :
unlet! g:tex_fold_enabled " just to be sure
let g:vimtex_fold_enabled = 1
VimTeX’ folding is nicely customizable, see :help vimtex-folding.
¹ As of version 121 (April 2022) of the builtin TeX syntax.
In Geany, editing PHP scripts, when you select lines and press control-e, the selected lines are commented by being wrapped in "/* ... */". Is there a way to change this behaviour so that it instead puts a "//" in front of each line?
All the other IDEs that I've used make use of "//" (Eclipse, Netbeans, Textmate, etc).
Settings like comment characters are controlled by filetype definition files. Assuming your scripts end in .php, you should find the default system-wide filetype definition file filetypes.php, and copy it to your filedefs directory in your user configuration directory. Then you can modify it as necessary.
This is all explained in detail in the manual (link above).
I've recently started using ctags on my projects. I currently have the following setup:
root/tags [contains all non-static tags]
root/foo/tags [contains static tags for the foo directory]
root/bar/tags [static]
root/something/else/tags [etc.]
...
I can set tags=./tags,tags,/path/to/root/tags and everything works perfectly.
However, my problem is that I work on several projects at once, so I have, for example, /path/to/root1, /path/to/root2, and /path/to/root3 all at once. I'd rather not manually set the tags each time I open a file; is there any way I can have tags to to the /path/to/rootX based on the file I'm editting? (i.e., if I'm editing /path/to/root3/foo/x.c, use the tags in root3/tags?
In my case, all of my projects share a common parent directory; what I really want is something like:
set tags=./tags,tags,substitute("%:p:h", "\(^\/path\/to\/.*/\).*$", "\1", "")
but I can't seem to get the right vimfu to make it work.
EDIT: I just realized that this won't work; I can't actually write to root*. Instead, I'd like to store my main ctags file in ~/ctags/root*/tags, where there's a 1:1 mapping between the subdirectories of ~/ctags/ and /path/to/ [For those who may be wondering, these are ClearCase UCM dynamic views; neither /view/XXX/ nor /view/XXX/vobs/ is writable]
If what you want is:
set tags=./tags,tags,substitute("%:p:h", "\(^\/path\/to\/.*/\).*$", "\1", "")
Try:
let &tags = './tags,tags,' . substitute(expand("%:p:h"), "\(^\/path\/to\/.*/\).*$", "\1", "")
There's no expansion in a :set command. Also, "%:p:h" won't be expanded automatically, so use expand(). See:
:help :let-option
:help expand()