I edit the same files often, so I have assigned some global marks to make it easier to load them up. For example, I jump to my resume (a TeX file) by typing 'R in normal mode.
I have also set up autocommands for TeX files, triggered by the BufWinEnter event. If I type :e /path/to/resume.tex in normal mode, everything works great. Yay!
However, the BufWinEnter event does not seem to be triggered when I jump to the file using the global mark 'R. Is there some other event I should use instead? What is the right way to set this up?
Here is the relevant line of ~/.vim/ftplugin/tex.vim
au BufWinEnter <buffer> silent loadview
Consider using :au Syntax tex silent loadview in .vimrc, instead.
Related
I want to create a plugin which maps a certain localleader mapping to a function call.
I have a ftplugin/javascript.vim file with the following content:
augroup javascript_pluginName
au!
echom "The plugin is loaded for JS"
au FileType javascript nnoremap <buffer> <localleader>j :call pluginName#pluginName#funName()<cr>
augroup END
When I open a javascript file, the message is printed, but then checking the mapped combinations shows that there is no such mapping.
I understand, that nnoremap is simply not going to map on top of other mappings. However, if I only leave the nnoremap command, without putting it in an autocmd, it would map properly.
Simply executing the call in command mode also works fine.
I looked into the vim help to see how to use autocmd, and I can't see any difference between the way I use it and what is explained there. The "Learn Vim the Hard Way" book also didn't help.
Is there something I'm missing? Should I frame the autocmd somehow differently?
I finally found out what the problem is!
Apparently, according to this article: https://vimways.org/2018/from-vimrc-to-vim/ if you put a filetype specific code into a filetype plugin, there is no need to create an autocmd for it.
The boilerplate is all made redundant by the general behaviour of vim setting the filetype on open and then running the ftplugin scripts, which are relevant for the file.
This means that in my ftplugin/javascript.vim file I only need the mapping:
nnoremap <buffer> <localleader>j :call pluginName#pluginName#funName()<cr>
I am trying to call this autoformatting plugin on save
Here is my autocommand:
autocmd BufWrite *.css,*.html,*.js,*.py :Autoformat<CR>
When I save nothing happens, if I manually call :Autoformat then the autoformatter runs.
What am I doing wrong?
You've already found the solution - here's the explanation:
The <CR> is for mappings, which work like a recorded sequence of typed keys, so you need to start command-line mode with : and conclude with <CR>. An autocmd takes an Ex command, so the <CR> is taken as an (invalid) argument. You also don't need the :, but that doesn't do harm.
As :help BufWrite shows, this is a synonym for BufWritePre.
BufWrite or BufWritePre Before writing the whole buffer to a file.
So, this is the recommended form:
autocmd BufWritePre *.css,*.html,*.js,*.py Autoformat
From what I've experienced, you sometimes need to surround it in an augroup
augroup autoFormat
autocmd BufWrite *.css,*.html,*.js,*.py :Autoformat<CR>
augroup END
I don't know why but it works for me! Technically just the autocmd should work on its own but sometimes it doesn't. Also, on the GitHub page it says to use :Autoformat<CR><CR>, maybe try that.
For some reason i had to get rid of the carriage return and change to BufWritePre (although the CR is the main issue, the BufWritePre just makes sure it gets changed before the buffer is written instead of after so it gets saved):
autocmd BufWritePre *.css,*.html,*.js,*.py :Autoformat
Why, I don't know?
I have the following problem, or lets say idea, with vim. When I am writing latex documents I want automatically open a file ~/.vim/latex_hints, where I collected some hints, shortcuts, workarounds,..., in vsplit on the right side. The hint file should be loaded read only and automatically closing when I close the latex document.
After a few experiments I added the following commands to my vimrc:
function Handletexfile()
setlocal cc=80
setlocal wrap
setlocal textwidth=80
belowright vsplit +setl\ ro\ nomodifiable ~/.vim/latex_hints
endfunction
autocmd BufRead,BufNewFile *.tex call Handletexfile()
and
function Handletexfileexit()
let tablist = []
call extend(tablist, tabpagebuflist(tabpagenr()))
for b in tablist
echo b . " ". bufname(b)
if bufname(b) =~ "vim/.*_hints"
echo "Close buffer..". b
execute "bdelete! ".b
endif
endfor
endfunction
autocmd BufWinLeave *.* call Handletexfileexit()
When I open a tex file, my hint file is displayed on the right side as read only and not modifiable. But when I close using :q or :wq the buffers open in the current tab are listed and the one matching to the hint file is selected by the if statement. But I get the following output
1 abstract.tex
2 ~/.vim/latex_hints
Close buffer..2
and my vim crashes with an segfault.
The first part of your requirement translates pretty straightforward into Vimscript:
autocmd BufWinEnter <buffer> belowright vsplit +setl\ ro ~/.vim/latex_hints
Put this into ~/.vim/after/ftplugin/tex.vim, or prepend :autocmd FileType tex to the above command.
The latter part is more complex; on BufWinLeave, you'd have to check all other windows for the opened cheat file with bufwinnr(), go to it (:wincmd w), and :close it.
As an alternative use the preview window with your hint file. The preview window gives you some advantages:
It is small and out of the way
Can close it easily via <c-w>z from any other window
Can jump to the preview window from any window easily via <c-w>P (jump back via <c-w>p)
You can do this via the :pedit command. For example:
autocmd BufWinEnter <buffer> pedit +setl\ ro ~/.vim/latex_hints
Personally I feel like it would be better to create command or mapping to open your hints files as you may eventually out grow the file as time goes on. I would also set the 'bufhidden' to wipe and un-list the buffer with 'nobuflisted'.
You may also want to look into getting a nice snippet plugin.
I have turned off line wrap in Vim by adding this in my vimrc:
set nowrap
But, I would like to have line wrap turned on automatically when I am editing *.tex files. So, I added this to my vimrc:
augroup WrapLineInTeXFile
autocmd!
autocmd FileType tex set wrap
augroup END
This should enable line wrap when the filetype is detected to be TeX. This works as expected for tex files, but if I open a non-tex file in the same Vim session, it has line wrap turned on!
Enabling and disabling word wrap automatically on different file extensions on Vim suggests using BufRead instead. But, even that has the same effect: if I first open a TeX file, followed by a non-TeX file, the non-TeX file has line wrap turned on.
How do I correctly enable line wrapping only for a certain filetype?
You can use setlocal for this. It'll only affect the current buffer.
augroup WrapLineInTeXFile
autocmd!
autocmd FileType tex setlocal wrap
augroup END
That'll be applied to every new TeX buffer.
The 'wrap' option is local to window. When you use :set, it will also apply to any newly opened windows. You want to use :setlocal.
Also, though your :augroup WrapLineInTeXFile works, it is cumbersome and doesn't scale to many settings. If you have :filetype plugin on in your ~/.vimrc, you can place filetype-specific settings (like :setlocal wrap) in ~/.vim/after/ftplugin/tex.vim (use of the after directory allows you to override any default filetype settings done by $VIMRUNTIME/ftplugin/tex.vim).
In my .vimrc I've put set foldmethod=syntax to enable folding of methods etc. However, I don't like the default that everytime I open a file, the whole thing is folded. Is there a way to enable foldmethod, yet have files unfolded when I open them?
set foldlevel=99
should open all folds, regardless of method used for folding. With foldlevel=0 all folded, foldlevel=1 only somes, ... higher numbers will close fewer folds.
You can put this in your .vimrc:
au BufRead * normal zR
It declares an automatic command (au), triggered when a buffer is read (BufRead), matching all files (*) and executes the zR (opens all folds) command in normal mode.
set nofoldenable
Adding this to your .vimrc will temporarily disable folding when you open the file, but folds can still be restored with zc
In .vimrc add an autocmd for BufWinEnter to open all folds automatically like this:
autocmd BufWinEnter * silent! :%foldopen!
That tell vim to execute the silent :%foldopen! after opening BunWinEnter event (see :h BufWinEnter). The silent %foldopen! will execute foldopen on the whole buffer thanks to the % and will open all folds recursively because of the !. Any eventual error message will be suppressed by silent. (You could get error messages like E490: No fold found if the buffer actually didn't contain any fold yet)
Note: You could use BufRead instead of BufWinEnter but then if the file has a modeline that enables the folding that will override this autocmd. I mean BufRead autocmds run before the modeline is processed and BufWinEnter will run them after. I find the later to be more useful
You can add
set foldlevelstart=99
to your .vimrc file, and it will start editing any new file with all folds open.
If you want a way to have it display unfolded as soon as it is opened, you can use set foldlevelstart=99 as a lot of answers explained.
But, if you just want to see them unfolded, you can just press zi and it will unfold everything. Another, zi will close them back.
You could map it to keys to enable it.
For example,
nmap ,f :set foldmethod=syntax<CR>
Then while in normal mode hit the ",f" key combination
You can open unfolded file when you put set nofoldenable into your .vimrc file.
autocmd BufReadPost * silent! :%foldopen!
This worked best for me. After a buffer gets opened all folds are opened. This opens them to the correct level.
The set foldenable method was not good, because if I choose to close one fold level, it enabled folding again, and folded every thing to 0 level, instead of just going down one level on the one I activated.