How to interactively set syntax highlighting in VI editor - syntax-highlighting

Using vi, I want to display files in distinguished colors in unix. If I save a file as .sql or .java it will highlight the keywords. I want to make changes to which words are highlighted, and in what color.
How do I control syntax highlighting and color schemes with various vi file extensions?

Type following command after a colon:
:set ft=sql.

I just did it by putting :syntax on. I suppose you want to save the settings so it can be highlighted each time you open one of those files, in which case the answer by Eric it's the correct one.

For file specific settings, there is also the power of modelines.
You can put them in the first or last five (unless you change modelines variable) lines to control variable settings.
eg:
# vim: syn=sh
or even:
/* vim: tw=60 ts=2: */
see vim's help: :h modeline

Are you sure one of the user don't have an alias to vim or something like that? CF: http://masstransmit.com/garage_blog/alias-vi-vim/

Related

Customize zsh prompt

How can I remove the [ that is placed before my commands after they are entered (e.g. the top two lines in the picture). Looking for something to put in my .zshrc file.
As per chepner, they are part of the terminal not zsh.
The way to remove them is:
Choose Edit > Marks > Automatically Mark Prompt Lines. The tick lets you know whether the option is enabled.

Syntax highlighting in bash while typing commands

Is there a way to highlight my input into the bash console while typing code?
For example, if I use bash keywords, those are highlighted in a specific color?
edit:
I'm particularly interested in solutions which make use of what bash already has to offer, like tweaking .bash_profile or similar.
You can use the syntax highlighting feature of vim/vi.
Open a file, for example open existing file called file.sh, enter
vi file.sh
Now press ESC key, type “: syntax on” i.e. type as follows
:syntax on
To turn it back off, press ESC key, type : syntax off
:syntax off
To make it permanent you can create a new .vimrc file in your home directory. Open the terminal and enter:
vim ~/.vimrc
There you can enter your various configurations. When done, you need to save the file and restart vim.
To be sure which vimrc is being used, you can ask inside of vim by typing:
:echo $MYVIMRC

Set up vim automatic coloring for FORTRAN

I cannot set the automatic coloring on vim for FORTRAN. I looked at the other thread on stackoverflow, and both of these lines in my .vimrc do not work:
syntax on
au BufNewFile,BufRead *.f08 set filetype=fortran
The "syntax on" does actually works, but just underlines some part of the code. I want to have the conventional FORTRAN colors.
Actually you were quite close to the solution. Adding the following lines to .vimrc works on my mac.
:syntax enable
:au BufNewFile,BufRead *.f08 set filetype=fortran
PS.: I'm using the Terminal.app. If you use another Termial, the problem might be caused due to the value of TERM. Typing
echo $TERM
should e.g. give xterm-color. Alternatively it might be VT100, which doesn't support colors. But you can easily change this by setting:
TERM=xterm-color
in your Terminal. This you can of course also add to your .bash_profile or .bashrc or whatever file which is read on start-up.

How can I make bash tab completion behave like vim tab completion and cycle through matching matches?

I've been meaning to find a solution for this for YEARS.
I am sooo much more productive in vim when manipulating files than bash for this reason.
If I have
file_12390983421
file_12391983421
file_12340983421
file_12390986421
In bash and type file_1->tab , it obviously lists:
file_12390983421 file_12391983421 file_12340983421 file_12390986421
And this is horrible and painful to work with.
The same sequence in vim will loop through the files one at a time.
Please someone tell me how to do this in bash, or if there is another shell that can do this, I'll switch tomorrow.
By default TAB is bound to the complete readline command. Your desired behavior would be menu-complete instead. You can change your readlines settings by editing ~/.inputrc. To rebind TAB, add this line:
TAB: menu-complete
For more details see the READLINE section in man bash.
For bash >= 4 you might like these settings. You can try them directly on the command-line, and put them in your ~/.bash_profile if you like them.
# If there are multiple matches for completion, Tab should cycle through them
bind 'TAB:menu-complete'
# And Shift-Tab should cycle backwards
bind '"\e[Z": menu-complete-backward'
# Display a list of the matching files
bind "set show-all-if-ambiguous on"
# Perform partial (common) completion on the first Tab press, only start
# cycling full results on the second Tab press (from bash version 5)
bind "set menu-complete-display-prefix on"
This setup is similar to Vim's set wildmode=longest:full:list,full
I pulled these settings from this question on the Unix & Linux site.
By the way, since you are here, here are some other great bindings:
# Cycle through history based on characters already typed on the line
bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'
# Keep Ctrl-Left and Ctrl-Right working when the above are used
bind '"\e[1;5C":forward-word'
bind '"\e[1;5D":backward-word'
This means if you type ssh<Up> it will cycle through previous lines where you ran ssh
If you don't like what you got, you can clear the line with Ctrl-K Ctrl-U
I pulled these settings from this question on AskUbuntu.
On top of
# cycle forward
Control-k: menu-complete
# cycle backward
Control-j: menu-complete-backward
you may also consider adding
# display one column with matches
set completion-display-width 1
This way you would preserve the current Tab functionality and make bash display the possibilities in one column. So instead of
file_12340983421 file_12390983421 file_12390986421 file_12391983421
you would get
file_12340983421
file_12390983421
file_12390986421
file_12391983421
P.S. You can get up to date readline library from this The GNU Readline Library website.
Thanks to #sth I found what works best for me:
To keep normal bash tab completion, and then use ctl-f to cycle through when needed using menu-complete
put this in your .inputrc file:
"\C-f": menu-complete
In my experience, the solution provided in sth's answer has never completely worked for me. TL;DR: Add set -o vi to your ~/.bashrc.
When using menu-complete in conjunction with vi keybindings, I have to make sure that my ~/.bashrc has:
set -o vi
It's never been enough for my ~/.inputrc just to have:
TAB: menu-complete
set editing-mode vi
set keymap vi
My guess is that somehow set editing-mode and set keymap are clobbering the TAB: ... setting, but I haven't looked into the documentation thoroughly to figure out why this is the case.

How can I activate Vim color schemes in OS X's Terminal?

I'm working with the Vim 7.2 that comes with Mac OS 10.6.1 (Leopard), using the Mac's "Terminal" app. I'd like to use a fancy color scheme. I did this...
:syntax on
Then this...
:colorscheme slate
:colorscheme elflord
:colorscheme desert
etc...
Syntax highlighting is working, but I'm finding that regardless of the scheme I choose, the only colors displayed are the basic Red, Blue, Cyan, Gray, etc.
Is there a way to get the Terminal app to display a larger collection of colors to allow some more subtle schemes?
Create a .vimrc file on your home ~/ folder and then edit it with vim ~/.vimrc. You can try adding syntax on inside ~/.vimrc file. The following command does that:
echo "syntax on" >> ~/.vimrc
It will highlight your code syntax on vim
You need to create file ~/.vimrc and add syntax on in that file
vi ~/.vimrc
syntax on
save the file
and run your vim
Add "syntax on" to the file /usr/share/vim/vimrc and you'll get highlighting in your files every time you edit one.
# vi /usr/share/vim/vimrc
Add this line at the end of the file:
syntax on
Now you'll get highlighting when you edit whatever's file.
The Terminal.app supports AFAIK only 16 colors; iTerm supports more colors or you use mvim (as suggested by Daniel).
You might want to consider using a version of Vim that is a native Mac app (that runs in a window).
MacVim has great color schemes and you can still launch it from Terminal like so:
$ mvim file.txt
That will open your file in a new Vim window.
#ashcatch - Can't leave a comment, but wanted to add that iTerm has other advantages over Terminal.app such as sensible copy and paste (configurable 'word' regex for easy double click selection of paths/urls, middle click paste) and terminal mouse support (:se mouse=a in vi to get mouse text selection, moving of window borders etc.)
I'd be lost without it.

Resources