Syntax highlighting in bash while typing commands - bash

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

Related

How to add new line in bashrc file in ubuntu?

I'm trying to update my bashrc file in ubuntu with some environment variables.
I can do this using below command.
echo 'export APP=/opt/tinyos-2.x/apps' >> ~/.bashrc
But I want to do it manually, meaning open the file with vim editor then add it.
The problem here is when I open the bashrc file the end line is "fi" and when I reach there and press insert and then enter to go to new line it stays at the same line and moves the fi word only or create A or C or B random characters.
May I know please some commands to handle this bashrc file so that I could add a new line and then my variables over there?
I've tried to look online but didn't find what am looking for.
Since you didn't specify if you have terminal access only or also GUI.
If you have terminal access only, any editor would do it.
Popular editors like nano or vim come installed by default in most Ubuntu releases.
To use nano, in your terminal, type
nano ~/.bashrc
Then press Ctrl + w +v to go to the end, add what you want to add, the Ctrl + o to save changes, Ctrl +x to exit.
You will need to log out and log back on, or run source ~/.profile to make your changes available to your bash environment.
Here's what you can do in vim.
Press the letter G. It will take you to the last line.
Press the letter O. It will allow you to insert text after the current lne.
Type in your content - export APP=/opt/tinyos-2.x/apps
Press the ESC key get out of editing mode.
Press the key :. It will allow you type commands.
Type wq followed by Enter. This will save the file and quit vim.
You are done.
As you are using Vim/Vi editor you need to use i/insert key to start editing, then for saving use escape & then wq to save and exit. For More detailed instructions please visit this link
Honestly, learning vim in a week (or even in a day) is tough. Took me more than a month to actually be productive on it as mh daily editor. But just to tell you. Go to the line after which you want to ass a new line using H-J-K-L or arrow keys. Then presss o. It'll spawn a new line below it and go into insert mode as well. Then write and press Esc. Enter :wq.

Execute a bash command in the text of a vim buffer

I know that I can get into bash while in vim via:
Ctrlz
or
:sh
or
:shell
etc.
Then use bash commands as normal, and get back out using fg.
What I am wondering is, can I execute a line of code from a script in vim straight to Bash, without having to exit vim, or having to copy it (via highlighting in visual mode for example) from vim, then going to a terminal and pasting it and hitting enter etc.?
Easiest way is to put the cursor on the line and type:
!!shreturn
This will replace the line with the output of the script. If you don't want that, simply follow up with u.
Arguably easiest way:
Yank the text you want to execute.
Open the cmdline (by pressing :)
type ! and then press ctrl-r and ", which will paste the content of the unnamed register to the cmdline (which will contain the text you wanted to execute)
press Enter

Bash's vim mode, not vi

I was fascinated when I discovered vi-like bash's editing mode. One can easily toggle it by issuing set -o vi command. But is there any way to toggle vim-like mode?
I mean when you are in a vi-like mode you can press v key and edit your commands in a fully functional vi editor's window but I would like to know how to force bash to start vim editor instead of vi because of the perks vim provides us (visual selection, additional commands etc)?
If you set EDITOR variable, in bash you can press Ctrl-x ctrl-e to edit your current command line in your EDITOR e.g. vim
EDIT
the ctrl-x ctrl-e is for emacs mode commandline editing, which is the default one. If you have already set it to vi mode, you could do what you have said, pressing the v. If you want to open the cmd line in vim, you have to set the EDITOR variable (in your .bashrc for example)
Personally I edit command line in emacs mode, even though vim is my main (and only) editor.
In your .bashrc, put the following line:
export VISUAL=/usr/bin/vim
If you want vim in many other contexts too, such as in git, you should also set EDITOR:
export EDITOR=/usr/bin/vim

How do I open a file in Vim from inside a Conque shell

Often I find my self navigating the filesystem from a Conque shell in Vim and want to open a specific file inside my existing MacVim session. Is this possible ? - I was hoping for something like:
shell> open some/file.txt
and then have file.txt pop up inside my existing Vim window (preferably in a new tab).
Note: I am using #wycats vim dot files (not sure this matters).
Type from ConqueShell
mvim --remote-tab-silent filename
This will open the file in a new tab in MacVim
You could also write a Bash alias to shorten the command (assuming you are using bash).
Put in your ~/.profile
alias vim='mvim --remote-tab-silent'
this would enable you to type
vim filename
from ConqueShell or bash, and have it open in a new MacVim tab, rather than terminal vim. It of course does disable your ability to run standard vim (although you could still use the vi command), so maybe you would want to name the alias differently.
Just to add, this will work only if you placed the mvim executable on your path E.G. /usr/bin/mvim. It comes with the MacVim.app
Often I find my self navigating the filesystem from a Conque shell
The beauty of running a shell from inside vim is you have all of vim and the shell at your disposal.
gf is your friend. Once you get the file you want displayed on the screen in some way, you can enter normal mode, move the cursor to the file you want to edit, then use the gf command to navigate to the file. There are many ways to use this. Any program or command that outputs file names is great for this (ll, git status, etc). You could also type the filename into the shell, just to make it visible on the screen without actually running any terminal commands (tab completion is handy here).
It is possible, you can start vim as server and then add as many files as you want, but I'm not very familiar with this, so I can't give you just a direction.

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