I'm having an issue with vim while I'm trying to remotely edit a sqlrpgle file in AS/400. I'm using the next command to open the file in vim:
:e ftp://myusername#mydomain/mylibrary/mymember.myfile
The file opens up but is filled with # characters instead of source code. Am I calling it the wrong way?
I tried writing the command this way but it failed:
:e ftp://myusername#mydomain/mylibrary/mymember.myfile.sqlrpgle
The "#" characters (0x40) are EBCDIC spaces.
Enable ASCII transfer mode with the following netrw option:
:let g:netrw_ftpmode="ascii"
Edit files using the following format:
:e ftp://user#host/library/file.member
I am trying to customize vim highlighting by placing additional instructions into local config $project/.lvimrc, which is managed by the https://github.com/embear/vim-localvimrc plugin.
Unfortunately, it seems that commands like
syntax match Operator "\<MYOP\>"
located in .lvimrc are ignored silently by vim. Typing the command in the command line works as expected. Other commands from .lvimrc also work. So what may stop vim from interpreting local highlighting correctly?
That was because https://github.com/embear/vim-localvimrc plugin launches local files in a sandbox by default. Syntax commands are not allowed in a sandbox (at least in my setup), so the exception was raised. For some reason, Vim handles such exceptions silently.
In my case, the following modifications formed a solution:
Disable sandboxes for localvimrc by adding let g:localvimrc_sandbox = 0 to master .vimrc file
Add set conceallevel=2 to the localvimrc
It could be a problem with the loading order, i.e., your .lvimrc is loaded, then the filetype syntax is loaded and overwrites the .lvimrc syntax commands. You could check that by including echom statements on both files.
Also notice that the local vimrc is not the standard way of customizing syntax highlight. From Vim FAQ 24.11:
You should not modify the syntax files supplied with Vim to add your
extensions. When you install the next version of Vim, you will lose your
changes. Instead you should create a file under the ~/.vim/after/syntax
directory with the same name as the original syntax file and add your
additions to this file.
For more information, read
|mysyntaxfile-add|
|'runtimepath'|
I tried mapping the key to :SyntasticCheck of Syntastic vim in .vimrc
nmap <leader>sc :SyntasticCheck<CR>
but it doesn't work. Also, when I type this command :SyntasticCheck into vim command it doesn't work, but when I save file with :w it works. It checks the syntax & raise errors in bottom window.
Any Ideas?
Syntastic runs the checker on the current file, not the contents of the buffer. If you have a file without syntax errors, add a syntax error, but don't save the file, :SyntasticCheck will not mark the error because you haven't saved the file. Your mapping is probably working fine, but nothing is getting marked by Syntastic because you haven't saved the file.
I've run into an odd problem in Vim. I would like to drag and drop a file from my desktop or file manager into Vim and edit it. Gvim handles this behavior correctly.
When I attempt to do the same thing in console Vim, the path to the file name is inserted instead. For example, if I drag and drop the file /home/myuser/foo.matic, it will apply the text string '/home/myuser/foo.matic' to the current buffer.
If I type :edit, then drag and drop the file name, Vim treats '/home/myuser/foo.matic' as a new directory.
I believe the problem here is the quotes before and after the file path. These appear to be inserted by both gnome-terminal and terminator. Is there a way to strip these quotes from the file name when dragging and dropping? Alternatively, is there a way for Vim to ignore the quotes?
You can’t make vim own :e command to do what you need, but you can define your own one. Most straightforward solution - make shell parse what was intended to be parsed by the shell - is listed below:
command -nargs=? -bang -bar E :execute "e<bang> ".fnameescape(system("echo -n ".<q-args>))
. This command accepts only :e[!] {file} variant, no +cmd and ++opts are allowed.
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.