Vim calling command on save - vim

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?

Related

Keep the format options always set to a particular value

I've noticed that plugins and other things will often modify formatoptions. is the following sufficient to keep them set/static?
" Do not auto-wrap comments and don't insert comments when pressing o/O. See: :h *fo-table* and :h *'formatoptions'*
set formatoptions-=cro
autocmd Filetype * set formatoptions-=cro
Or, is this missing anything? (Is the first line unnecessary as well?)
Yes, what you have is (almost) good. You need to wrap it in proper augroup
augroup FORMATOPTIONS
autocmd!
autocmd filetype * set fo-=c fo-=r fo-=o " Disable continuation of comments to the next line
augroup END
Also notice as I divided -cro into singular option removing, otherwise you won't have guarantee you get the desired effect.

Vim autocmd does not map properly a mapping

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>

Modify vim-fugitive Gstatus mapping

The :Gstatus window has specific mappings for that particular buffer. In my case, I would like to change the cc mapping to not only execute :Gcommit but also go into insert mode afterwards.
It seems like the user robodendron figured out how to do this as shown in https://github.com/tpope/vim-fugitive/issues/647, but I'm don't know what he means when he says "changing the order should be enough." Also, I would ask this on the Git issues page, but it seems like the user NicolasWebDev already tried that and no one got back to him.
I can add mappings by creating a after/ftplugin/gitcommit.vim file, but modifying an existing mapping seems to be more difficult since the mapping is defined after the filetype is set.
Also, I could modify the source code mappings, but we all know that's more of a temporary fix.
I am not sure about trying to alter :Gstatus mappings, but you can start insert mode when entering the commit buffer.
Add the following to your vimrc file:
augroup turbo_commit
autocmd!
autocmd BufEnter COMMIT_EDITMSG startinsert
augroup END
For more help see:
:h :autocmd
:h :augroup
:h BufEnter
:h startinsert

Execute :nohlsearch on InsertEnter

I can do <C-O>:noh<CR> when I'm in insert mode, but it doesn't work when done automatically:
autocmd InsertEnter * :nohlsearch
This works, but it behaves differently:
autocmd InsertEnter * :set nohlsearch
To clarify, what I want is to run :nohlsearch if I enter insert node, but I still want to keep the ability to do /<CR>N to search for another item.
I think, what you want can be accomplished by setting the search register directly:
:autocmd InsertEnter * :let let #/=''
If you want to restore the highlighting when returning from insert mode, you would need to save and restore the pattern, something like this should do it:
:autocmd InsertEnter * :let b:_search=#/|let #/=''
:autocmd InsertLeave * :let #/=get(b:,'_search','')
This saves and restores the current search pattern in the buffer local variable b:_search.
You should write a function calling :nohl and then redraw:
function DisableHL()
nohl
redraw
endfunction
and then autocmd InsertEnter * :call DisableHL()
I think I came up with a smart-ish way of accomplishing exactly the same effect (at least it does exactly what I want and I seem to want what you asked for).
I have this in my vimrc:
Disable highlighting when entering Insert mode
autocmd InsertEnter * set nohlsearch
Re-enable highlighting when pressing any of the nN?/ keys before sending the key, then send the key
for s:k in ['n', 'N', '?', '/']
execute('nnoremap ' . s:k . ' :set hlsearch<cr>' . s:k)
endfor
to hide last search's hightlighting
(I like to be able to quickly disable it without entering insert mode)
nnoremap <silent> <bs> :set nohlsearch<cr>
It basically sets [no]hlsearch on the fly as you use the commands.
I also checked, and at least on my setup it doesn't mess if using nN?/ as normal mode command (for vim's default commands at least) arguments (like dtN to delete until next N), although this might be a concern if you ever remap any of those keys, or if a plugin does it without you noticing it.

Vim jump event?

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.

Resources