Override a builtin command with cabbrev - vim

I'm trying to override 'w' in vim so it would call an external program and filter the buffer instead of writing to a file. There are very good examples across the internet about how to do that. I tried one from vim.wikia.com, but vim always complains with E488: Trailing characters. This is the command in my vimrc:
cabbrev w <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'W' : 'w')<CR>
I'm not very familiar with vim script. I tried removing <CR> from the end of the line with no luck.
UPDATE
Since I want to run vim as customized as possible I run it with the -u flag. I noticed that vim behaves differently when using that flag compared to running it without it
With the -u flag the expanded abbreviation is what needs to be evaluated as code.
Without the flag, the abbreviation is what it is intended to be (here I enter the cabbrev rule from vim's prompt)

Regarding the -u flag vim's man page says this:
-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.
Apparently when this flag is used the initialization from
vim /etc/vimrc is not performed and there I found this option:
set nocompatible
vim's help about compatible option:
This option has the effect of making Vim either more Vi-compatible, or
make Vim behave in a more useful way. This is a special kind of
option, because when it's set or reset, other options are also changed
as a side effect. CAREFUL: Setting or resetting this option can have
a lot of unexpected effects: Mappings are interpreted in another way,
undo behaves differently, etc. If you set this option in your vimrc
file, you should probably put it at the very start.
...
When a vimrc or gvimrc file is found while Vim is starting up,
this option is switched off, and all options that have not been
modified will be set to the Vim defaults. Effectively, this means
that when a vimrc or gvimrc file exists, Vim will use the Vim
defaults, otherwise it will use the Vi defaults. (Note: This doesn't
happen for the system-wide vimrc or gvimrc file, nor for a file given
with the -u argument).
set nocompatible makes the cabbrev syntax from the question work

Related

Make .vimrc changes being picked up by Vim only and for me only

I have a $HOME/.vimrc file which I have configured for many options like below but not limited to.
set foldmethod=indent
set foldnestmax=10
set mouse=a
set number
These all work fine if I open a file with vim however if I open with vi or view the file none of the config work, which is fine but I get errors for some.
line 104:
E538: No mouse support: mouse=a
line 205:
E518: Unknown option: foldmethod=indent
Press ENTER or type command to continue
After searching quite a while I was able to remove some of these.
if has('mouse')
set mouse=a
endif
silent! set foldmethod=indent
However this is just suppressing them. And the linenumbers are still being displayed with vi or view which look pretty bad and many people login to the box will be suddenly mystified by the yellow line numbers.
How to make vi not take up these config.
I powerbroker to the linux box. Can I make .vimrc setting just for me.
vi and vim are the same executable.
I'd suggest you change the name of your vimrc:
$ mv ~/.vimrc ~/.myvimrc
and start Vim with:
$ vim -Nu ~/.myvimrc
You could add an alias to ~/.bashrc (or whatever works in that system) to ease your workflow:
alias myvim='/usr/bin/vim -Nu ~/.myvimrc'
As vi does not support all options of vim, What I do is to set alias for vi to vim in my rc file
alias vim='vim -p'
alias vi='vim -p'
The -p is not really required. but I kinda like tabbing enabled by default on vim. So every time, if you hit vi or vim, it behaves the same.
You can make this permanent, if you like, by adding the lines to ~/.bashrc, assuming you are using bash. You need to source ~/.bashrc to take immediate effect without logging off.

ctags not working as expected with Vim plus general setup problems (C programming)

I have installed cvim and NodeTree plugins and generated an exuberant ctags file for my build tree.
This is what my ~/.vim/.vimrc file looks like:
:noremap :TlistToggle
:let Tlist_Show_One_File = 1
:let Tlist_Exit_OnlyWindow = 1
:let Tlist_Use_Right_Window = 1
set tags=./tags;/
set number
set tabstop=4
set incsearch
When I start editing a file, I notice that Ctrl ] does not work and I have to resort to typing ta: funcname - which gets tiring after a while. Interestingly enough, Ctrl T pops me off the tag stack as expected - I don't understand whats going on - how do I fix this?
Incidentally, vim (appears to) completely ignores the contents of my .vimrc file and I always have to type the same commands in the editor, so as to get the settings I want - very annoying.
Last but not the least, I used to be able to type :make in the editor window, drop to the console and then have the build results displayed in a little window which I can then go to and select a line (with an error or warning say), and then have the editor automagically take me to the offending line - unfortunately, I don't remember the plugin (or commands) I used to allow me to build from within vim.
So, how do I:
Fix my vim setup so that I can move to definitions/declarations using Ctrl-]
Fix my .vimrc file so that contents are actually applied to my vim session.
Find the appropriate plugin to install to allow builds (using make) from within vim
You're asking about a weird mix of problems.
Fix my vim setup so that I can move to definitions/declarations using Ctrl-]
The tags functionality is working; I suspect that you have a mapping blocking Ctrl-]. Try
:verbose nmap <C-]>
and
:nunmap <C-]>
Fix my .vimrc file so that contents are actually applied to my vim session.
:echo $MYVIMRC
will tell you the location of the .vimrc that Vim uses. Also, check the output of :scriptnames which scripts get loaded, and read :help vimrc to understand the logic Vim applies.
Find the appropriate plugin to install to allow builds (using make) from within vim
That's built into Vim. With the appropriate 'makeprg' set (it defaults to make), you can run :make. Vim parses the output (through the 'errorformat' option), and you can open the quickfix list via :copen.
Your vimrc is:
~/.vim/.vimrc
If you run Vim 7.4, it should be:
~/.vim/vimrc
or
~/.vimrc
If you run Vim 7.3 or older, it should be:
~/.vimrc
And... what Ingo said.

How to tell Vim not to use ~/.vim

By default Vim is looking for plugins and other stuff in ~/.vim.
Is there any way to tell Vim to search for plugins, etc. in ~/.other_folder and force it to ignore ~/.vim entirely?
Vim uses the comma-separated paths from 'runtimepath' to determine where to look for :runtime'd files.
You can change that option either in ~/.vimrc (which is sourced as the very first script), or by passing the set rtp=... commands on Vim's command-line via --cmd (the commands passed with -c are only applied after startup).

How to highlight Bash scripts in Vim?

My Vim editor auto highlights PHP files (vim file.php), HTML files (vim file.html) and so on.
But when I type: vim file and inside it write a Bash script, it doesn't highlight it.
How can I tell Vim to highlight it as a Bash script?
I start typing #!/bin/bash at the top of the file, but it doesn't make it work.
Are you correctly giving the shell script a .sh extension? Vim's automatic syntax selection is almost completely based on file name (extension) detection. If a file doesn't have a syntax set (or is the wrong syntax), Vim won't automatically change to the correct syntax just because you started typing a script in a given language.
As a temporary workaround, the command :set syn=sh will turn on shell-script syntax highlighting.
The answers so far are correct that you can use the extension (like .sh) or a shebang line (like #!/bin/bash) to identify the file type. If you don't have one of those, you can still specify the file type manually by using a modeline comment at the top or bottom of your file.
For instance, if you want to identify a script without an extension as a shell script, you could add this comment to the top of your file:
# vim: set filetype=sh :
or
# vim: filetype=sh
That will tell vim to treat the file as a shell script. (You can set other things in the modeline, too. In vim type :help modeline for more info.)
Actually syntax highlighting is a feature of vim not vi.
Try using vim command and then do
:syntax on.
I came to this answer looking for specifically how to highlight bash syntax, not POSIX shell. Simply doing a set ft=sh (or equivalent) will result in the file being highlighted for POSIX shell, which leaves a lot of syntax that's valid in bash highlighted in red. To get bash highlighting:
" Set a variable on the buffer that tells the sh syntax highlighter
" that this is bash:
let b:is_bash = 1
" Set the filetype to sh
set ft=sh
Note that if your ft is already sh, you still need the set command; otherwise the let doesn't take effect immediately.
You can make this a global default by making the variable global, i.e., let g:is_bash = 1.
:help ft-sh-syntax is the manual page I had to find; it explains this, and how to trigger highlighting of other flavors of shell.
Vim can also detect file types by inspecting their contents (like for example if the first line contains a bash shebang), here is a quote from filetype.txt help file:
If your filetype can only be detected by inspecting the contents of the file
Create your user runtime directory. You would normally use the first item of the 'runtimepath' option. Example for Unix:
:!mkdir ~/.vim
Create a vim script file for doing this. Example:
if did_filetype() " filetype already set..
finish " ..don't do these checks
endif
if getline(1) =~ '^#!.*\<mine\>'
setfiletype mine
elseif getline(1) =~? '\<drawing\>'
setfiletype drawing
endif
See $VIMRUNTIME/scripts.vim for more examples.
Write this file as "scripts.vim" in your user runtime directory. For
example, for Unix:
:w ~/.vim/scripts.vim
The detection will work right away, no need to restart Vim.
Your scripts.vim is loaded before the default checks for file types, which
means that your rules override the default rules in
$VIMRUNTIME/scripts.vim.
Vim can detect the file type reading the first line. Add the following line as first line.
#!/bin/sh
For those who have a variant of this question i.e. how to enable syntax highlighting on bash files without .sh extension automatically when opened...
Add filetype on in your .vimrc. This enables file type detection by also considering the file's contents. For example, bash scripts will be set to sh file-type. However, typing the #! won't trigger file type detection on a new file created with vim and you will need to use set ft=sh in that case. For more info, type :h filetype in vim.
As mentioned in the comments, you will need to use this in conjuction with syntax enable to turn on highlighting.
Or you could use :filetype detect.
From the doc:
Use this if you started with an empty file and typed text that makes
it possible to detect the file type. For example, when you entered
this in a shell script: "#!/bin/csh".
Once you add the shebang at the top of the file, save it and reload it (e.g. :w|e) and syntax coloring can kick in.
See also Vim inconsistently syntax highlighting bash files, the accepted answer may help as well.
vim already recognizes many file types by default. Most of them work by file extensions, but in a case like this, vim will also analyze the content of the file to guess the correct type.
vim sets the filetype for specific file names like .bashrc, .tcshrc, etc. automatically. But a file with a .sh extension will be recognized as either csh, ksh or bash script. To determine what kind of script this is exactly, vim reads the first line of the file to look at the #! line.
If the first line contains the word bash, the file is identified as a bash script. Usually you see #!/bin/bash if the script is meant to be executed directly, but for a shell configuration file using a simple # bash would work as well.
If you want to look at the details, this is implemented in $VIMRUNTIME/filetype.vim.
Probably the easiest way to get syntax highlighting on a new file, is to just reload it after writing the shebang line. A simple :w :e will write out the file, reload it, and interprete the shebang line you have just written to provide you with the appropriate syntax highlighting.
If you already know the file-type before opening the script or if you're creating a new script without an extension which is common, you can pass it to vim on the command-line like so:
vim -c 'setfiletype sh' path/to/script
vim -c 'setfiletype python' path/to/script
To toggle syntax highlight on/off while you're inside the editor.
Turn on
:syntax on
Turn off
:syntax off
Run this to always have syntax highlighting on when opening vim.
echo ":syntax on" >> ~/.vimrc
When you create a new file, only the filename detection comes into play; content detection (#!/bin/bash) doesn't apply if you type it after creating a new buffer.
The sensible thing is to just do :set ft=bash the first time around, and the next time you edit it, the #!/bin/bash will set the right filetype automatically.

How to turn-off a plugin in Vim temporarily?

I have multiple plugins in Vim and some of them modify the default behavior of Vim. For example I use Vimacs plugin, which makes Vim behave like emacs in the insert mode alone. Sometime I want to turn off the Vimacs plugin without moving the vimacs.vim out of the plugins directory. Is there a way to do it?
You can do this if you use a plugin manager like Vundle or Pathogen, which will keep the plugin in its own directory underneath the ~/.vim/bundle/ directory.
In that case, just find out the runtimepath of the vimacs plugin with the following command:
set runtimepath?
Let's say it's ~/.vim/bundle/vimacs.
Then, put this command in your .vimrc:
set runtimepath-=~/.vim/bundle/vimacs
To load vimacs, just comment that line out and relaunch Vim (or source your .vimrc).
See which variable vimacs check on start. On the begin of the script file find something Like if exists('g:vimacs_is_loaded").... Then set this variable in your .vimrc or while start vim with vim --cmd "let g:vimacs_is_loaded = 1".
In case you are using pathogen, this post gives a better answer, in my opinion. Since I have frequent need to disable snippets when using latex, also added this in my ~/.config/ranger/rc.conf:
map bs shell vim --cmd "let g:pathogen_blacklist = [ 'ultisnips', 'vim-snipmate' ]" %f
This way, whenever I want to open a file with snippets disabled, it is easy.

Resources