How to put PreserveNoEOL's command to .vimrc - vim

I've installed PreserveNoEOL and I can use :SetNoEOL to remove EOL from current file.
But I do not want type :SetNoEOL every time I edit a file, I want to put it to .vimrc
How can I do that?

So, you always want Vim to leave off the final EOL character, creating what many people consider broken text files?! The plugin (I'm the author) wasn't meant for that, and I wouldn't recommend this unless you have good reasons, but it can surely be done via an autocmd in your ~/.vimrc:
autocmd BufNewFile,BufRead * SetNoEOL

Related

vim execute command depending on file type

How can I get vim to execute a command depending on the file type?
Recently I have been editing a lot of Python files and comments autoindent to the 0th column.
One way to fix this is to run the command inoremap # X#<left><backspace><right>
How can I get this command to run every time I open a python (*.py) file for editing?
TL;DR
Use vim's filetype plugin mechanism.
File-type specific code
There are (at least) two solutions to this, with one preferable to the other.
autocmd FileType
This has already been posted as an answer, but the general form is
augroup your_group
autocmd!
autocmd FileType python your good code
augroup END
This works, and has the advantage of grouping code in your vimrc.
But there is a second, more powerful, probably more performant alternative. It
is my preferred
solution.
after/ftplugin
I've written
extensively about using filetype
plugins in vim.
The gist of it is that you need filetype detection and plugins (filetype plugin
on at a minimum, though you may want filetype plugin indent on). Then you
drop filetype specific code in ~/.vim/after/ftplugin/<filetype>.vim (or a few
variants, see :h add-filetype-plugin, :h ftplugin-name, :h ftplugin).
So ultimately,
" ~/.vim/after/ftplugin/python.vim
inoremap # X#<left><backspace><right>
A few notes
It is a good practice to set b:undo_ftplugin properly: it helps (trust me).
You can borrow my undo script on
Github.
Consider using <buffer>, -buffer, setlocal, and <LocalLeader> where
possible. This is for local stuff, after all.
On power
A script gives you more flexibility than a single line of autocommands. You have
to jump through more hoops to do anything complex in autocommands, where in a
script they can just sit. Besides, you feel more like you're writing a program
(which you are) than a one off config.
The modularity is also perfect for organization: all your python customization
code is in one place.
And it's a lot less typing :)
On performance
I have not measured anything. Don't shoot me.
Having lots of autocommands can slow down vim, because they have to be checked
when their events fire. Rather than clog up the autocommands with filetype
stuff, use a file that is already going to be sourced for you (an ftplugin).
By piggybacking on an existing customization mechanic, you get all the benefit
and none of the risk. Again, it is almost certainly more efficient to let vim
source a file on demand for you then to try to reimplement it's ftplugin wheel.
Does this solve your issue?
au FileType python inoremap # X#<left><backspace><right>
This autocommand runs your mapping for filetype python

What is the correct way in Vim to autocommand ":highlight" after a buffer is loaded?

Due to various reasons, I run Vim at sixteen-colors, synced w/ my terminal's colors. In a recent Vim update, I've had to rework my "~/.vimrc" completely to get it back into running order on Linux.
Initially I was shocked to find that this simple line did not work (even w/ "syntax on" preceding it):
:highlight Comment ctermfg=White
I'm also using a "LineNr" ctermfg. No matter where I placed/stacked the "Comment" ctermfg, it didn't work, or interfered w/ everything else sourcing correctly (ie, placed in the same line w/ "LineNr"). However, I found that calling "Comment" after a buffer had loaded would make the comments appear as intended.
I am new to autocmd in Vim (and want to know how it works, anyways). Is there an "autocmd" call that I can have in my "~/.vimrc" that will run the aforementioned line of code immediately after a buffer has loaded?
I have tried several iterations (BufWritePre, BufWritePost, etc.) and been unsuccessful. This was my previous attempt:
autocmd BufWinPost * :highlight Comment ctermfg=white
Don't resort to :autocmd without reason; search harder for the root cause!
Your description lacks specifics; I guess your chosen colorscheme (or a plugin, but no sane plugin should interfere with the default highlightings) overrides your custom one for Comment. You can check who defined this via
:verbose highlight Comment
If this points to your colorscheme, you simply need to execute your :highlight command after it. For this, you need to understand :help initialization, and maybe check the output of :scriptnames. If you have a :colorscheme foo command in your ~/.vimrc, it should be as simple as putting the :highlight command after it.
You do need an :autocmd if you switch colorschemes on the fly, as most colorschemes override the basic Comment definition. The correct event and pattern for that would be ColorScheme *
If I do a quick :h autocmd-events, I find the the event BufWinPost does not exist. I think, you want BufWinEnter instead. The autocmd you wrote should work, except for the :. HTH

Turn on Vim plugins for some files, not others?

I recently added filetype plugin indent on to my .vimrc in order to enable special indenting and syntax highlighting for Clojure code (*.clj files). However, it's also causing indenting in my LaTeX files (*.tex). This is annoying when I'm editing, and even more annoying, because the tab characters that get inserted confuse a custom program I use to process my LaTeX files. I know I can make indenting use spaces, but I really just want "intelligent" LaTeX indenting to go away. Actually, I want all intelligent indenting to go away, except where I specifically ask for it.
How can I get correct auto-formatting for Clojure code in Vim, but turn off all special handling of LaTeX files (except for syntax highlighting)?
Sorry if this has already been answered; I haven't succeeded in finding the answer yet.
(Irrelevant editorial comment: Sometimes Vim "upgrades" make me want to go back to Unix 'vi'. OK, not really.)
Each proper filetype plugin script has an inclusion guard at its beginning. If you don't want any of the filetype options for Latex files (i.e. filetype of tex), create a file ~/.vim/ftplugin/tex.vim with these contents:
:let b:did_ftplugin = 1
This causes the default ftplugin from $VIMRUNTIME to abort its execution. The same applies to indent: ~/.vim/indent/tex.vim and b:did_indent is the guard variable.
Alternative
On the other hand, if you just want to undo certain options (e.g. :setlocal expandtab to avoid inserting tabs), you'd put those overriding commands into the so-called after directory: ~/.vim/after/ftplugin/tex.vim.

TextMate-style code beautifucation in vim?

I have a bunch of ugly code in vim: it's not indented consistently at all. TextMate has this wonderful "code cleanup" function... and I'm sure vim is just as powerful, I just don't know how to automatically clean up my entire file (putting consistent tabs, with consistent length, after curly braces...and then unindenting after every code block is the main thing I want).
Have you tried gg=G in normal mode?
In addition to Gebb's answer, make sure the following is in your .vimrc:
filetype indent plugin on
Also, to strip out any trailing whitespace automatically, add this:
autocmd BufWritePre * :%s/\s\+$//e
In Vim, indentation is defined by filetype-specific scripts. The default JavaScript indent file is not very up to date and doesn't play very well with "modern" ways of writing JS (lots of nested objects, anonymous functions…). Try other indent files and see if it solves your issue.

Quickest Way to Revert Spaces to TABs in VIM

I have always been one to replace TABs in VIM with x amount of spaces (usually 4).
Four lines I almost always use in my .vimrc config file are:
set tabstop=4
set shiftwidth=4
set expandtab
syntax on
Basically, there are moments when I NEED to use a single TAB (such as Makefiles), and I don't know how else to work around that other than leaving the file, editing my .vimrc, and then reloading the file of interest.
That said, what is the quickest way (from within VIM) to revert it back to using TABS, and then reverting back to my original settings? I'm looking for the least-hassle, least-keystrokes solution.
VIM will automatically enable the TAB for a makefile, assuming you name it "makefile," as opposed to "Makefile." Not sure why VIM still doesn't detect the type with a lower-uppercase difference, but such is life. (#Sedrik)
That aside, other alternative solutions are:
Filetype Binding (#ThorstenS #tungd):
autocmd FileType make setlocal noexpandtab
RealTime Switch (#ThorstenS):
Assuming the .vimrc configuration mentioned in the question, do:
:set noet (to switch from spaces to TAB)
and :set et (to switch back)
Just use the magical escape key available in insert mode.
On the *NIX's it is ^V by default when you are in insert mode. On Windows, you need to find out what the magical escape character is - ^V is taken for something else; I think it may be ^Q or ^S?
So! In your makefile:
this: this.c
<C-V><Tab>cc this.c
where the usual meanings apply:
means hit ctrl-V (you should see a ^ hiding away under the cursor)
- hit the tab key. Bingo.
Works for me.
Note: if you use vim settings or startup code which smashes tabs as you read a file, this obviously is a short-term fix. I prefer to learn how to use the retab command to ensure a file is tab-clean, because I don't like a file to be touched unless I consciously choose to do so.
Just type set noexpandtab . Perhaps you bind this to a function key.
Only this configuration helped me solve this problem.
filetype plugin indent on
filetype detect
autocmd FileType make set noexpandtab
Vim defaults to tabstop=8 and noexpandtab, so the defaults are well suited to working with Makefiles. If your .vimrc uses custom options for tabstop, expandtab, etc., one simple solution is to bypass your .vimrc while working with Makefiles.
From the manpage (emphasis mine):
-u {vimrc} Use the commands in the file {vimrc} for initializations. All the other initializations are skipped. Use this to edit a special kind of files. It can also be used to skip all initializations by giving the name "NONE". See ":help initialization" within vim for more details.
Since I can never remember the necessary flag/value/formatting, I've created a Bash alias which remembers for me: alias vimnone='vim -u NONE'
You can create a custom configuration per file type.
mkdir -p ~/.vim/after/indent
echo 'set noexpandtab' >> ~/.vim/after/indent/make.vim
Basically, here we created a new indent configuration for makefiles that will be loaded after other scripts.
source

Resources