Using a minimal vimrc to remove trailing whitespace.
set list " show invisible characters
set listchars=trail:· " display extra whitespace
autocmd BufWritePost <buffer> :%s/\s\+$//e
When I open a project and start working on a file with trailing white space when I save the file I expect it to be removed, but it doesn't get removed.
When I source .vimrc manually and save the file it suddenly works.
What is causing this and how do I solve it?
BufWritePost is executed after writing. You don't need to source .vimrc — you can just write the second time.
But to really fix the problem use BufWritePre.
Related
How do I remove the right margin in Vim? It seems to be adding a carriage return and line feed (column 80?) whenever I save a file as .txt or .md. This doesn't happen if I save the file with no extension, but I want to be able to edit .txt files.
I've tried :set nowrap but this doesn't have the desired effect. I also commented out everything in my .gvimrc file in case I inadvertently did something there but no luck with that approach.
The text filetype in Vim sets the textwidth option to 78 by default, which has the effect of splitting lines. If you remove the textwidth option by setting it to zero, Vim will stop splitting lines. You can remove textwidth by hand with
:set textwidth=0
or you could add something like
autocmd FileType text setlocal textwidth=0
to your .vimrc.
I was able to resolve this by commenting out the following line in the vimrc_example.vim file:
" autocmd FileType text setlocal textwidth=78
Here's the path to the file on my machine:
C:\Program Files (x86)\Vim\vim80\vimrc_example.vim
I'm using gVim.
With the current monitor sizes (4K and bigger) the 78 character rule is not valid anymore (valid in the era before the 21" CRT monitors)
I tried the solution by Jon, but it did not work for git commit messages.
I had to change the .vimrc line to
autocmd FileType * setlocal textwidth=0
Now it works for any file type.
when I run
vim good.html
and
:verbose set et?
expandtab
Last set from ~/.vimrc
but when I run
vim good.py
and
:verbose set et?
expandtab
Last set from /usr/share/vim/vim74/ftplugin/python.vim
I want apply ~/.vimrc file in .py, not python.vim
Yesterday I all is fine but today suddenly path was changed
please someone teach me how can I change the path
Put this into your .vimrc:
autocmd VimEnter *.py set expandtab
or if you want to have the configuration of .vimrc to be executed after all plugins being loaded - in case they have changed some settings -, you can add this line:
autocmd VimEnter * source ~/.vimrc
Note: It could have a side-effect depending on the content of your .vimrc because the latter actually will be executed twice (at the begining and at the end of vim startup) so you need to consider that.
Concerning Plugins now, if they are logged in some specific folders like .vim or vim installation path they will be loaded automatically unless you removed them or run some specific commands to be ignored.
Vim has also the ability to detect the type of file which is being edited, and this occurs when the option filetype is activated and probably this is what happened to you.
So typing :filetype will confirm that. Maybe you can desactivate it for some specific files if you wish. It is up to you !
:help VimEnter
VimEnter
VimEnter After doing all the startup stuff, including
loading .vimrc files, executing the "-c cmd"
arguments, creating all windows and loading
the buffers in them.
When I write to a file with :w, vim sometimes (NOT ALWAYS) jumps to the end of the file after the write operation is complete. I don't understand why this happens. I've been going through my .vimrc to see if I have some kind of bug. My .vimrc is quite large so I don't include the full source here, I think the only parts of my .vimrc which are perhaps relevant to this question are the following parts:
nore ; :
inoremap jj <Esc>
" Automatically remove all trailing whitespace.
" Every time the user issues a :w command, Vim will automatically remove all
" trailing whitespace before saving
autocmd BufWritePre * :%s/\s\+$//e
" Restore cursor position
au BufReadPost *
\ if line("'\"") > 0|
\ if line("'\"") <= line("$")|
\ exe("norm '\"")|
\else|
\exe "norm $"|
\endif|
\endif
However I don't see how these parts of my .vimrc can cause the jump behavior after writing, a full source of my .vimrc is available here. I hope somebody has an idea about what is causing the unwanted jump.
Here is a command from my ~/.vimrc:
command! -range=% TR mark `|execute <line1> . ',' . <line2> . 's/\s\+$//'|normal! ``
The trick is to create mark ` before the trimming and jump back to it afterward.
You can change your autocmd to:
autocmd BufWritePre * :mark `|%s/\s\+$//e|normal! ``
Even with #romainl's addition of the mark, this still isn't fully transparent:
the view (of displayed lines) may still change (winsaveview() instead of a mark would fix that)
the :s command clobbers the last search pattern
A plugin (like my DeleteTrailingWhitespace plugin) would provide a more robust solution. (The plugin page has links to alternative plugins.)
I want to change the filetype based on file extension in vim.
I have the following code in the my .vimrc
autocmd BufNew,BufNewFile,BufRead *.txt,*.text,*.md,*.markdown setlocal ft=markdown
But when I open a file with the extention .md file, the filetype is not changed. I run :set ft command and it shows the output as filetype=modula2.
Am I doing anything wrong?
Edit:
I started to debug by renaming my old .vimrc file and created a new one with just this line. It was working properly. Then I replaced my old .vimrc file and everything seems to be working fine. Guess it was because of some issues in some addon which I am using.
But accepting ZyX's answer, since it thought me an alternate way to do this.
I created a ~/vim/ftdetect/markdown.vim file with this line
autocmd BufNewFile,BufRead *.md,*.mkdn,*.markdown :set filetype=markdown
Reading the docs for filetype, setfiletype only sets if filetype is unset. So you need to use set for an unconditional change to a filetype.
Wondering whether this line goes before or after filetype … on. In the former case you should try putting it (your autocommand) after this line. Better if you put it into ~/.vim/ftdetect/markdown.vim and use setfiletype markdown instead of setlocal ft=markdown:
augroup filetypedetect
autocmd BufNew,BufNewFile,BufRead *.txt,*.text,*.md,*.markdown :setfiletype markdown
augroup END
: it is the default way of doing such things. ~/.vim must go before /usr/share/vim/* paths in 'runtimepath' option in this case (it does by default).
I was able to get syntax highlighting for alternate file extensions by creating renamed copies of the target syntax file in the Vim\vim74\syntax directory.
To make *.md open as a .markdown:
copy markdown.vim md.vim
or paste a copy of markdown.vim to the syntax folder, then rename the copy md.vim.
(Running vim74 on win7)
I'm using QuickCursor for entering text to forms.
My problem with that is I always have MacVim open, and with hidden enabled, so when I :wq from the temp file QuickCursor make, the buffer stays in MacVim, so I have to delete it to get QuickCursor paste back to the window.
I wanted to solve this with an autocommand in my vimrc:
autocmd BufRead "/private/var/folders/fg/gv_*/T/*" set bufhidden="delete" | startinsert!
but this never run. What could be the problem ? What is the right event to use ? I tried BufWinEnter, BufNewFile, neither of them works, or maybe something else is the problem.
Ok, after several hours of try, I finally found out.
I had added quotes to the bufhidden setting and the filename. It should be:
autocmd BufRead /private/var/folders/fg/gv_*/T/* set bufhidden=delete | startinsert!
With the extra quotes it doesn't work:
"delete" is an invalid option value (see :he bufhidden)
quotes around a filename prevent the wildcards (glob characters) from matching (see doc)
If anybody else using QuickCursor, you can fine-tune it:
autocmd BufWinEnter /private/var/folders/fg/gv_*/T/* set bufhidden=delete |
exe "normal G$" | startinsert!
So it changes to insert mode at the end of the text