Keep marks across file reloads in vim - vim

I have an autocommand for source files that runs an autoformatter on save. If the autoformatter modifies the file, all my marks are lost. Is there a way to preserve the marks in this case? My relevant .vimrc configuration is this:
function! s:FormatCode()
if (s:formatOnSave == 1)
Autoformat
endif
endfunction
Then in an augroup:
if (executable("clang-format"))
autocmd FileType c,cpp,objc,objcpp autocmd BufWritePre <buffer> silent! call s:FormatCode()
endif

Assuming that the autoformatter is using the filter command, this can be done by prepending :help :keepmarks to the command:
keepmarks Autoformat
Read the caveats in the help. If that doesn't work, you'd have to manually save and restore the marks, e.g. using getpos() and setpos().

Related

Why vim returns E488: Trailling characters to my function?

I am trying to execute a yapf command to format my python file when I save it, so I created a function to call this command:
function Format_python_file()
silent :!yapf --style="{based_on_style: pep8, indent_width: 4}" -i %
silent :e %
endfunction
autocmd BufWritePost *.py call Format_python_file() <afile>
The problem is with your autocmd line, you have a trailing <afile> there.
In fact, the message I see is quite explicit about that:
Error detected while processing BufWritePost Autocommands for "*.py":
E488: Trailing characters: <afile>
You should just drop the <afile>, the function itself already works on the current buffer, doesn't need any argument or other reference to the current file.
Also note that it's a good practice to put your autocmds inside an augroup which gets cleared first. That way, if you reload your source file (vimrc or otherwise), it won't create duplicated autocmds.
The cleaner way to set up this autocmd would be:
augroup python_yapf
autocmd!
autocmd BufWritePost *.py call Format_python_file()
augroup END

How to negate an autocmd pattern [duplicate]

I have a Vim autocmd that removes trailing whitespace in files before write. I want this almost 100% of the time, but there are a few filetypes that I'd like it disabled. Conventional wisdom is to list the filetypes you want an autocmd to run against in a comma-separated list, eg:
autocmd BufWritePre *.rb, *.js, *.pl
But in this case that would be onerous.
Is there a way to match an autocmd pattern against all files EXCEPT those matching the pattern? I cannot find the equivalent to a NOT matcher in the docs.
*.rb isn't a filetype. It's a file pattern. ruby is the filetype and could even be set on files that don't have a .rb extension. So, what you most likely want is a function that your autocmd calls to both check for filetypes which shouldn't be acted on and strips the whitespace.
fun! StripTrailingWhitespace()
" Don't strip on these filetypes
if &ft =~ 'ruby\|javascript\|perl'
return
endif
%s/\s\+$//e
endfun
autocmd BufWritePre * call StripTrailingWhitespace()
Building on evan's answer, you could check for a buffer-local variable and determine whether to do the strip using that. This would also allow you to do one-off disabling if you decided that you don't want to strip a buffer that's a filetype you normally would strip.
fun! StripTrailingWhitespace()
" Only strip if the b:noStripeWhitespace variable isn't set
if exists('b:noStripWhitespace')
return
endif
%s/\s\+$//e
endfun
autocmd BufWritePre * call StripTrailingWhitespace()
autocmd FileType ruby,javascript,perl let b:noStripWhitespace=1
Another choice of one line way:
let blacklist = ['rb', 'js', 'pl']
autocmd BufWritePre * if index(blacklist, &ft) < 0 | do somthing you like | endif
Then you can do something you like for all filetypes except those in blacklist.
A good way would be to set a local variable for the one filetype to true. Then set the automcommand if that variable is false (if set for everything else) or if it exists at all (no need to preset it).
autocmd BufWritePre *.foo let b:foo=true
if !exists("b:foo")
autocmd ...
endif
changed variable prefixes based on comment
You can do the except on the same regexp:
autocmd BufWritePre *\(.out\|.diffs\)\#<! <your_command>
That will do <your_command> for all files extensions except for .out or .diffs.
This works for Syntax autocommands, where the pattern (<match>) is just the filetype. It excludes any rst files:
au Syntax *\(^rst\)\#<! …
Our .vimrc config file runs only once on startup. So if you put an if test at this time, it won't work, because no python file is then currently being edited.
But you can use .vimrc to set up an automatic behaviour: something that vim will do each time it encounters a special condition. The condition can be in your case: "A new file is being editing, and its file type is 'python'". See :h :au

Run selected text through filter and reinsert

I like using code cleanup scripts; perldidy; uglifierjs, etc. I previously ran them all like this in my vimrc...
map <leader>pt :!perltidy<CR>
map <leader>jt :!uglifyjs -b<CR>
map <leader>pjt :!python -mjson.tool<CR>
map <leader>ct :!column -t<CR>
How this functionally works; it runs currently selected text throguh the CLI program, and replaces the selection with the output. Works wonderfully; but as you can see I now have to stop and think what beautifier I want to run and remember the nemonic I have for it. This led me to think there has to be a better way. So I tried did this...
map <leader>jt :call RunTidy()<cr>
function! RunTidy()
if (&ft == "javascript")
echo 'is js..'
:'<,'>!ulifyjs -b
endif
if (&ft == "json")
echo 'is json'
:'<,'>!python -mjson.tool
endif
endfunction
The problem being this just doesn't work; executing once for each line and not replacing contents.. Anyone aware of a better way to do this? I feel like this should be a solved problem...
augroup filters
autocmd!
autocmd FileType perl map <buffer> <leader>t :!perltidy<CR>
autocmd FileType javascript map <buffer> <leader>t :!uglifyjs -b<CR>
autocmd FileType json map <buffer> <leader>t :!python -mjson.tool<CR>
augroup END
or put those mappings, all with the same lhs, in different ~/.vim/after/ftplugin/{filetype}.vim.
Use ftplugins. You need to have filetype plugin indent on in your vimrc.
Create files in ~/.vim/ftplugin/<filetype>.vim. These files will be sourced when the file type is set.
For example using javascript put the following in ~/.vim/ftplugin/javascript.vim
noremap <buffer> <leader>jt :!uglifyjs -b<CR>
to map <leader>jt to :!uglifyjs -b<CR> in all javascript buffers. This will not show up in other filetypes.
You would do the same for all other filetypes.
You can do the same for file type specific settings by using setlocal.
Take a look at :h ftplugin

Vim remove whitespace for specific files

I've been using the following technique in my .vimrc to remove extra whitespace at the end of a line...
autocmd BufWritePre * :%s/\s\+$//e
But I realised I didn't want that to happen with Markdown files (e.g. either .md or .markdown) so I have the following VimScript...
fun! StripTrailingWhiteSpace()
" don't strip on these filetypes
if &ft =~ 'md\|markdown'
return
endif
%s/\s\+$//e
endfun
autocmd bufwritepre * :call StripTrailingWhiteSpace()
But that still removes the whitespace for all files.
So I then tried the following (which seemed better as it was shorter)...
let blacklist = ['md', 'markdown']
autocmd BufWritePre * if index(blacklist, &ft) < 0 | :%s/\s\+$//e
But, again, that still removes the whitespace for all files?
Neither of these techniques seem to work? They leave the whitespace still in the file?
Any ideas on how I can do this (at the moment I'm having to edit Markdown files in a separate writing app rather than Vim and that's quite annoying).
The first function should work except you should not be looking for md. ft is short for filetype which is markdown for Markdown files.
By changing it as follows it works fine. (Tested on Vim 7.4)
fun! StripTrailingWhiteSpace()
" don't strip on these filetypes
if &ft =~ 'markdown'
return
endif
%s/\s\+$//e
endfun
autocmd bufwritepre * :call StripTrailingWhiteSpace()
Thanks Kevin Sjoberg for the initial response, but it turns out I have a bigger issue.
Which is if I run :set filetype? from within my Markdown file then it reports back that Vim thinks the filetype is modula2
I'm not sure how I can fix this, so if any one knows then please do comment!
As a temp fix I've used the following work around...
fun! StripTrailingWhitespace()
" don't strip on these filetypes
if &ft =~ 'modula2\|markdown'
return
endif
%s/\s\+$//e
endfun
autocmd BufWritePre * call StripTrailingWhitespace()
...so it checks for both modula2 (whatever that is?) AND markdown files.
UPDATE: well it seems this is a known issue https://github.com/tpope/vim-markdown/issues/15 so I tried the suggested fix...
au BufRead,BufNewFile *.md set syntax=markdown
...but that didn't help, Vim still interpreted the file as modular2
Also tried adding the following into my vimrc file...
au! BufNewFile,BufRead *.md setf markdown
...but that didn't work to change the format.
UPDATE 2
Fixed it, the suggestion by both https://github.com/tpope/vim-markdown/issues/15 and Kevin Sjoberg were almost there.
I just added into my vimrc file a modified version of their suggestion au Bufread,BufNewFile *.md set filetype=markdown
Your implementation is simple, but has some shortcomings (e.g. it clobbers the last search pattern / search history). If you're not against installing a plugin, you can try my DeleteTrailingWhitespace plugin. With it, you can exclude certain buffers by setting a flag, e.g.:
autocmd FileType markdown let b:DeleteTrailingWhitespace = 0
I realize you solved the problem which was more than just the function you were trying to setup.
However, I liked your initial "blacklist" approach with a filetypes list. So for anyone else landing here that likes that, this is what I have.
It also saves your cursor position, and puts it back after running the removal of whitespace.
function! StripTrailingWhitespace()
" Preparation: save last search, and cursor position.
let _s=#/
let l = line(".")
let c = col(".")
" do the business:
%s/\s\+$//e
" clean up: restore previous search history, and cursor position
let #/=_s
call cursor(l, c)
endfunction
let noStripWhiteSpaceTypes = ['markdown']
autocmd BufWritePre * if index(noStripWhiteSpaceTypes, &ft) < 0 | call StripTrailingWhitespace() | endif

Function to source .vimrc and .gvimrc

I generally use GVim, but most of my configuration is done via .vimrc (like keymappings) because I want them in vim and gvim. So when I edit my vimrc and then source it from gvim, I have to source my .gvimrc after that in order to get my colorscheme back (since it's gvim only). I tried to write a function to do this, and ran into the problems described in the comments below:
function ReloadConfigs()
:source ~/.vimrc
if has("gui_running")
:source ~/.gvimrc
endif
endfunction
command! Recfg call ReloadConfigs()
" error: function already exists, add ! to replace it
function! ReloadConfigs()
:source ~/.vimrc
if has("gui_running")
:source ~/.gvimrc
endif
endfunction
command! Recfg call ReloadConfigs()
" error: cannot replace function, it is in use
Is it possible to do something like this? Or, since my .gvimrc only has a few lines, should I just put its contents into an if has("gui_running") block?
You've put your function somewhere in your .vimrc. This means that, while it's being executed, the :source .vimrc is trying to redefine it, which is a problem. You could try doing this:
if !exists("*ReloadConfigs")
function ReloadConfigs()
:source ~/.vimrc
if has("gui_running")
:source ~/.gvimrc
endif
endfunction
command! Recfg call ReloadConfigs()
endif
If the function is already defined, this should skip redefining it, avoiding the issue.
I would say that whatever you have in your .vimrc that's messing up gvim settings should be surrounded by an if !has("gui_running") block.
An autocmd seems to be the easiest way of handling what you're trying to do:
autocmd BufWritePre .gvimrc,.vimrc source <amatch>
This way you get your configuration file automatically reloaded when you save it without having to mess around with functions. Alternatively, you could use a mapping to trigger :source $MYVIMRC or :source $MYGVIMRC.

Resources