Why vi doesn't show syntax highlighting when create script - linux

When I use vi to create a script, it doesn't show any syntax highlighting. But once I exit vi and use vi to open the script again, It will highlight the syntax.
What I want is to highlight the syntax for the first time when I create the file. So is there anything wrong with my configuration or it is the common situation?

When vim opens a file, it tries to guess the file type in order to enable syntax highlighting. When you open a new file called "myscript", vim has nothing to go on in order to guess the type.
When you write the file, the first file you write is #!/bin/sh (or similar), followed by the rest of the script. By this point, vim has already determined this is a plain text file, and does not syntax highlighting.
When you reopen the file, vim sees the first line of the file, and understands this is a shell script. It sets the type appropriately, and does syntax magic.
In order to solve, either name the file with an extension that suggests its type (an empty file called "myscript.sh" is, likely, a shell script), or manually set the type using :set filetype=sh
Personally, I find the first form ugly (why include the file type in the file name?), and the second one hard to remember. I just close and reopen the file :-)

vi usually guess the kind of syntax highlighting to use from the filename extension. If you're editing a file that doesn't yet have a name, you will need to tell it how to highlight:
:set filetype=html

Related

Vim syntax doesn't highlight in real time

I have enabled vim syntax on (in ~/.vimrc syntax on) and it works but only on files with a code in when I view them. When I create a new file with vim and write there some code - no syntax highlight. After saving this file and reopening with vim - syntax highlight works perfect.
I am using manjaro KDE.
When you open a new file without an extension (vim mynewfile) none of vim’s filetype detection mechanisms can recognize it (they all use either extensions or first-couple-of-lines heuristics, which don’t work here).
When you enter code and reopen the file, the line-checks for filetypes work, causing the syntax to be set correctly, causing highlights to apply.
You can always set syntax=mine (though set filetype=mine is better) to set it manually.
This problem shouldnt happen when you do vim some.c or similar, because the extension will force detection based on extension rules.
Vim must know how to highlight your syntax in order to actually highlight it. One way to do this, is for Vim to check the file name and sometimes inspect the contents of the file, and set the file type. The file type is then used to highlight the syntax.
To enable detection of the file type (and load plugin and indent files), add the following to your vimrc:
filetype on plugin indent
If Vim is unable to detect the file type, and you have not yet saved your file with a known extension, you can set the file type manually, like this:
:set filetype=html
Vim will then highlight the syntax of the file as HTML syntax.
More information is available in the help pages.

Vim doesn't highlight syntax after using "tabe" open multiple files

I just started to learn Vim, and I found it is very annoying that vim dose not highlight syntax after using tabe command to open multiple files. Any solutions for that?
Let me explain more details of this situation.
I open file A using vim
I type ":tabe file B" to open the second file.
Two files now are opened in a same vim window. However, only file A has syntax highlight, file B does not have syntax highlight. Same situation also happens in ":sp file B".
If that really happens, then you found a bug in vim! But before we jump to any conclusions, my advice is that you first try to debug your vimrc: vi.stackexchange.com/questions/2003/how-do-i-debug-my-vimrc-file
BTW, it is very annoying that every newbie vim user tries to make it behave like every other graphical editor. So another suggested read: stackoverflow.com/questions/102384/using-vims-tabs-like-buffers

Vim doesn't show a certain file with colors

Vim doesn't show a certain file with colors. However, renaming this file will avoid this problem. Besides, files of the same type don't have the same problem.
What can be a problem?
If the file extension is not recognized for syntax highlighting you can use set syntax command.
Here is an example to set the current window html syntax highlighting:
:set syntax=html
If you want to set the file extension to be recognized to a specific file extension I would follow the instructions here:
vim: persistent :set syntax for a given filetype?
Since you asked why a certain file doesn't use syntax coloring, here's the reason behind it:
The swap file!
You somehow changed the syntax value, which gets stored in the swap file. Renaming solves your problem, because Vim doesn't have the old swap file associated with it.
Yes, you fixed your problem by :set syntax=foo. But if you encounter problems, which only apply to a specific file, then it's rooted always in the swap file! There are tons of problems like this, which can be solved by deleting the swap file and therefore restoring the default expected behaviour.
The variable for the swap directory can be set like this:
:set dir=~/vimfiles/swap (my swap)
Defaults from Vims help (:h dir):
for MS-DOS and Win32: ".,c:\tmp,c:\temp"
for Unix: ".,~/tmp,/var/tmp,/tmp")

How to make vim go to last line last character and then in insert mode?

I have a few text files, each for its own purposes. (Like: download.txt, questions.txt, word-meaning.txt etc.)
questions.txt:
I put all my question, doubt approaching in my mind to this file to ask/clear when I go online (I've no access to internet everytime of the day). I delete that line from the file when I ask that question.
download.txt:
I keep names of all packages or zipballs or tarballs in this file and download when I am connected.
word-meaning.txt:
I am not a native English speaker, so whenever I see any word which's meaning I don't in my native language, I write that down in this file and use Google Translate to translate it to my native language when I am connected.
In all above cases I have to go to last line of the file everytime I have to add anything to those lists.
My Question:
Can I make vim go to line line, last character of the file and then go in in insert mode? I will alias that to something like vimll to use it with these type of files.
Similar Question:
How do I start vim in insert mode?
You can define an autocmd to go into insert mode at the end of the file whenever one of your files is loaded into a Vim window:
autocmd BufWinEnter questions.txt,download.txt,word-meaning.txt $|startinsert!
You can do this in terminal:
vim filename.ext +$ +starti!
To go to the last line, last character of the file filename.ext and then in insert mode.
You can also alias that for your convenience of use, so add the following in your .bash_aliases file:
alias vimll='vim +$ +starti!'

Prevent vim from opening binary files accidentally?

I frequently accidentally open a binary executable, i.e. "foo", when I mean to open the associated source code "foo.cpp". The root of the problem is that tab completion, i.e. :e fo<tab> typically lands on the binary instead of the source code.
Is there a way to get vim to only tab complete names of text files? Or alternatively, change the tab completion order?
Sometimes my hasty tab completion error happens outside of vim; for those cases, what is the best way to prevent vim from opening files that are not text?
Not exactly what you need, but I have something like this in my .vimrc
" ignore these files when completing names and in Ex
set wildignore=.svn,CVS,.git,*.o,*.a,*.class,*.mo,*.la,*.so,*.obj,*.swp,*.jpg,*.png,*.xpm,*.gif,*.pdf,*.bak,*.beam
" set of file name suffixes that will be given a lower priority when it comes to matching wildcards
set suffixes+=.old
For tab completion outside of vim, that will depend on your shell. Most shells have some form of autocompletion support. In particular, Zsh has the ability to autocomplete e.g. remote hosts for ssh. I'm not a wizard with these things, but it would probably be relatively simple to get your shell to drop files with certain suffixes from the autocompletion list when the command you are typing starts with "vim".
A quick google search turn up this page, which has this:
# Filename suffixes to ignore during completion (except after rm command)
zstyle ':completion:*:*:(^rm):*:*files' ignored-patterns '*?.o' '*?.c~' \
'*?.old' '*?.pro'
It should not be too difficult to modify this logic to get what you want (if you use Zsh).
Maybe you can find this useful:
set wildmenu
set wildmode=longest,list
(taken and using from How do I make vim do normal (bash like) tab completion for file names?)

Resources