Download and install the latest MySQL version from mysql.com. Choose the x86_64 version for Intel (unless your Intel Mac is the original Macbook Pro or Macbook, which are not 64 bit chips. In those cases, use the 32 bit x86 version).
I used to have a co-worker who was really good at UNIX.
He showed me how to use Vi key bindings to edit my shell commands.
He placed the command in a file that ran every time I logged in.
Since then, I've moved to a different project.
Unfortunately I don't remember how to set this up.
Is there anyone here who knows how to use Vi key bindings to edit commands in the terminal?
How can I make that setting permanent?
kurtmmigrated from stackoverflow.comJul 13 '12 at 19:55
This question came from our site for professional and enthusiast programmers.
6 Answers
You're talking about the greatest feature ever!
You can use vi commands to edit shell commands (and command history) by adding this to your .bashrc
file:
set -o vi
You can also run that command from the command line to affect only your current session.
If you don't use bash, substitue the appropriate rc file for your shell.
This allows you to use vi commands to edit any command...
You can also use j and k to move through your history (after pressing ESC).
You can also use / (after hitting ESC) to search for old commands.
In other words, to find that super-long cp
command you did ten minutes ago:
ESC/cpENTER
Then you can cycle through all the matching commands in your history with n and N.
All this makes me 10 trillion times more productive at the command line!
If you're using bash, as jahroy and evil otto have already answered, you can use
to cause bash to use vi-style editing commands.
Once you've done this, you can type Escv to launch the vi editor with a temporary file containing a copy of the current command line. You can edit the command, even replacing it with multiple lines; when you save the file (:wq
), the shell will execute the edited commands.
If you prefer
you can use Ctl-XCtl-E to do the same thing. It will use your preferred editor $EDITOR
, not necessarily emacs.
(Personally, I use vi (actually vim) for most of my editing, but I prefer set -o emacs
in the shell; switching in and out of insert mode is great for editing files, but awkward for interactive commands. YMMV.)
You can use set -o vi
to change your line editing commands as @jahroy posted, but you may be thinking of the fc
command (available in bash and I think ksh, but probably not tcsh), which will put the previous command into an editor (FCEDIT or EDITOR, which you probably have set to vi) and then executes the command when you exit the editor. See the manpage or help fc
for details, or just try it out.
To edit most recently used command in vim and invoke an edited version after editor saves and exits, use fc
shell built-in. Without any arguments it will do the following:
- Creates a temporary file in
/tmp
and populates it with the most recently typed command - After editor exits normally (with exit code 0) it executes command found in that temporary file and removes this file. Edited version is saved in history as a usual command (at least in zsh). It executes nothing if editor exits with code other then zero (in vim this can be achieved by either killing it or using
:cquit
).
Command works both in bash and zsh.
Indeed, this is in fact the greatest feature ever. There's more though:
Create a file named
containing the line
Editline, is a BSD licensed version library that provides readline-like services. That .editrc will set your keybindings to vi mode in MySQL's command line shell, or any application that does not use readline.
I have grown somewhat forgetful with the passing of years, so my .zshrc sources some wrappers for vi mode, to show me what mode I am in on the command line. It's nice.
I know in zsh you can type
and get the list of keybindings. I'm sure bash must have something similar. Not all of the vi keys are bound by default. Bind them how you see fit and you are off to the races.
in your terminal , type export EDITOR=vi
.
To save this setting . put this line in the file ~/.profile
Not the answer you're looking for? Browse other questions tagged shellvimterminalvi or ask your own question.
In this tutorial, you will learn-
What is the VI editor?
The VI editor is the most popular and classic text editor in the Linux family. Below, are some reasons which make it a widely used editor –
- It is available in almost all Linux Distributions
- It works the same across different platforms and Distributions
- It is user-friendly. Hence, millions of Linux users love it and use it for their editing needs
Nowadays, there are advanced versions of the vi editor available, and the most popular one is VIM which is Vi Improved. Some of the other ones are Elvis, Nvi, Nano, and Vile. It is wise to learn vi because it is feature-rich and offers endless possibilities to edit a file.
To work on VI editor, you need to understand its operation modes. They can be divided into two main parts.
Command mode:
- The vi editor opens in this mode, and it only understands commands
- In this mode, you can, move the cursor and cut, copy, paste the text
- This mode also saves the changes you have made to the file
- Commands are case sensitive. You should use the right letter case.
Pdf Editor On Mac
Insert mode:
This mode is for inserting text in the file.
You can switch to the Insert mode from the command mode by pressing 'i' on the keyboard
Once you are in Insert mode, any key would be taken as an input for the file on which you are currently working.
To return to the command mode and save the changes you have made you need to press the Esc key
Starting the vi editor
To launch the VI Editor -Open the Terminal (CLI) and type
&If you specify an existing file, then the editor would open it for you to edit. Else, you can create a new file.
vi Editing commands
Note: You should be in the 'command mode' to execute these commands. VI editor is case-sensitive so make sure you type the commands in the right letter-case.
Keystrokes | Action |
---|---|
i | Insert at cursor (goes into insert mode) |
a | Write after cursor (goes into insert mode) |
A | Write at the end of line (goes into insert mode) |
ESC | Terminate insert mode |
u | Undo last change |
U | Undo all changes to the entire line |
o | Open a new line (goes into insert mode) |
dd 3dd | Delete line Delete 3 lines. |
D | Delete contents of line after the cursor |
C | Delete contents of a line after the cursor and insert new text. Press ESC key to end insertion. |
dw 4dw | Delete word Delete 4 words |
cw | Change word |
x | Delete character at the cursor |
r | Replace character |
R | Overwrite characters from cursor onward |
s | Substitute one character under cursor continue to insert |
S | Substitute entire line and begin to insert at the beginning of the line |
~ | Change case of individual character |
Make sure you press the right command otherwise you will end up making undesirable changes to the file. You can also enter the insert mode by pressing a, A, o, as required.
Moving within a file
You need to be in the command mode to move within a file. The default keys for navigation are mentioned below else; You can also use the arrow keys on the keyboard.Keystroke | Use |
---|---|
k | Move cursor up |
j | Move cursor down |
h | Move cursor left |
l | Move cursor right |
Saving and Closing the file
You should be in the command mode to exit the editor and save changesHtml Editor On Mac
to the file.Keystroke | Use |
---|---|
Shift+zz | Save the file and quit |
:w | Save the file but keep it open |
:q | Quit without saving |
:wq | Save the file and quit |
Summary:
Setup Mysql On Mac
- The vi editor is the most popular and commonly used Linux text editor
- It is usually available in all Linux Distributions.
- It works in two modes, Command and Insert
- Command mode takes the user commands, and the Insert mode is for editing text
- You should know the commands to work on your file easily
- Learning to use this editor can benefit you in creating scripts and editing files.