Use Autosave in Vim using no plugin? - vim

How to autosave in Vim using no plugin?
I have found the following post:
Auto-save in VIM as you type
which gives the following answer:
autocmd TextChanged,TextChangedI <buffer> silent write
which works perfectly except that when I put that in my .vimrc it doesn't work, so how can I put that line in my .vimrc such that it works everytime I open Vim ?
EDIT: It seems putting that line in .vimrc works WHEN I DON'T USE A SESSION !
So I will rephrase my question:
How can I get the above work in combination with mksession to open preconfigured session in Vim using vim -S ?

As stated in the comments by D. Ben Knoble, you need to use * instead of <buffer>:
autocmd TextChanged,TextChangedI * silent write

Related

Prevent Vim warning after autocommand changes file being edited

I run a command every time a save a file which, among other things, lints the current file. Vim does two annoying things which I wanted to avoid:
It asks me to press enter after the command was executed.
It notifies me that the current file has been modified outside of Vim.
How to get rid of this? I just want this to behave unobtrusively as ALE linters. This is my code:
autocmd BufWritePost *.prisma :execute '!npx prisma format'
#romainl's comment gave me an idea on how to do it:
autocmd BufWritePre *.prisma silent write | silent :execute '!npx prisma format' | edit! %
It's not the cleanest way to do it but it works. silent hides the command output and edit! forces Vim to open the modified file.

force vim to overwrite external changes

I use Vim 7.4 (Mac OS) to edit and run Lua scripts. I've mapped a key in my .vimrc to save the current buffer and run an external script.
The key map in .vimrc:
map V :w!<CR> :!python "$HOME/tools/client/concli.py" --lua %<CR>
It works fine but every once in a while the files are 'touched' by Xcode (touch shell command). Then when I hit the mapped key vim warns me that the file has been changed externally and I have to confirm to write to it.
This is quite annoying since the files are often touched. How could I force vim to overwrite external changes without prompting? I tried 'w!' without success.
Thank you, Laurent
Indeed, the overwrite confirmation cannot be turned off with :w!, and :set autoread doesn't help in this case, neither. What does work is instructing Vim to explicitly check for changes before the write:
:checktime | w
I believe
set autoread
should do it. It tells Vim to automatically re-reads the file changed outside Vim.
I saw this in a mailing list. Apparently it is called if the file has changed timestamp, after a call to an external shell command.
function! ProcessFileChangedShell()
if v:fcs_reason == 'mode' || v:fcs_reason == 'time'
let v:fcs_choice = ''
else
let v:fcs_choice = 'ask'
endif
endfunction
autocmd FileChangedShell call ProcessFileChangedShell()
But it did not consistently fire for me. (Depending whether or not I had edited the file since the change, which in my case was external.)
There are some more tricks on the VimTips wiki which may help.
Add this to your ~/.vimrc file:
set autoread
nnoremap <C-u> :checktime<CR>
Now whenever you want vim to reload external changes, just click CTRL-U :)

Why won't vim recognise a plugin command in the vimrc, but it will recognise it when running?

I've installed the vim-gitgutter plugin with pathogen.
I can type :GitGutterLineHighlightsEnable from inside vim and line highlights are turned on, great.
But I want line highlights to be automatically enabled at startup, so I added the command to my ~/.vimrc. However when I start vim, I get "E492: Not an editor command: GitGutterLineHighlightsEnable". Once vim has started up, I can run the command.
My vimrc looks like this:
execute pathogen#infect()
colorscheme railscasts
.. snip tabs and colors etc ..
GitGutterLineHighlightsEnable
hi GitGutterAddLine guibg=#222F22
hi GitGutterChangeLine guibg=#222239
hi GitGutterDeleteLine guibg=#2F2222
Figured it out.
.vimrc is executed before plugins are loaded. From this related question, I changed the commands to:
autocmd VimEnter * GitGutterLineHighlightsEnable
This executes the command after vim has started up.
Use
let g:gitgutter_highlight_lines = 1
instead of
GitGutterLineHighlightsEnable
As you determined yourself, plugins are processed after the .vimrc.
What you can do if you don't like using a VimEnter autocmd, is put a file in your ~/.vim/after/plugin directory for any commands that should run after plugins are loaded.

vim shortcut to open a file with current directory populated

I'm trying to add a key mapping so that it opens up command and populate it with :e /path/to/current/file
I can get the current directory using :pwd but I'm having a trouble to use it in the mapping
I think it will be along the lines of setting pwd to a variable and use that variable as such:
noremap <C-q> <C-o>:e *pwdvariable*<Space>
Should I create a function to perform this?
I guess you need
nnoremap <C-q> <C-\><C-n>:e <C-r>=fnameescape(expand('%:p:h'))<CR>
%:p:h will get the full path of the current file (without trailing slash). Read more in :help filename-modifiers.
Not exactly, what you have asked for, but maybe more helpful: Vim tip 64: Set working directory to the current file: In short, add the following line to .vimrc:
autocmd BufEnter * silent! lcd %:p:h
Interesting for you would be also Easy edit of files in the same directory.

How do I make vim-latex compile correctly without having to save?

I just started using Vim-LaTeX.
The default dvi output didn't work for me, so I changed the default to pdf by adding "let g:Tex_DefaultTargetFormat = 'pdf'" to my tex.vim.
The only problem is that I need to save my document first (:w) before compiling it (\ll) and viewing in in Evince (\lv). If I do not save it, and run \ll and \lv, latex is run on the saved file before I started editing it, and not on the buffer containing my edited file.
How do I make it so that vim saves my file and compiles my document when I hit \ll? Thanks!
this is not the perfect solution but it will work,
define a new map in your .vimrc:
map <f2> :w<cr><leader>ll
this works but there should exist a cleaner way of doing this.
This does specifically what you asked. Add the following to your .vimrc or equivalent:
autocmd FileType tex call Tex_MakeMap("<Leader>ll", ":w <CR> <Plug>Tex_Compile", 'n', '<buffer>')
autocmd FileType tex call Tex_MakeMap("<Leader>ll", "<ESC> :w <CR> <Plug>Tex_Compile", 'v', '<buffer>')
I have since moved on from latex suite and i am now using latexmk which works really well.
#dc46and2 Your command should be:
autocmd FileType tex call Tex_MakeMap('<leader>ll', ':update!<CR>:call Tex_RunLaTeX()<CR>', 'n', '<buffer>')
autocmd FileType tex call Tex_MakeMap('<leader>ll', '<ESC>:update!<CR>:call Tex_RunLaTeX()<CR>', 'v', '<buffer>')
AFAIK, " are used for comments in vim.
You can try:-
:w | !pdflatex "filename"
I believe this will work fine in your case.

Resources