VI editor "ESC" key and "Ctrl + [" do not work correctly - shell

I am a complete beginner with VI editor.
I had a chance of using the VI editor by typing git commit instead of git commit -m "my comment" when commenting my commit to my git repository.
I learned that I would go into "edit" mode by pressing i in the VI command line. And I also learned that I go back to the command mode by pressing either ESC key or Ctrl + [ key combination. However, neither keys get me out of the "edit" mode.
What happens is that pressing either keys do the same thing, something that seems like accumulating ESC key. Each time I press either key, I see something like below in the bottom line of the screen:
Pressed once:
ESC-
Pressed twice:
ESC ESC-
Pressed three times:
ESC ESC ESC
Since I know that the command to save and exit is :wq, when I press ESC one time and followed by :, the command line immediately turns into to Eval:. It seems like ESC + : is the key combination for such command.
This seems weird. What would be the problem and how could I fix it?
Thanks in advance for your help.
Edit
I found the below codes from .gitconfig file in my root directory. Is this causing the problem?
[core]
autocrlf = input
safecrlf = true
editor = emacs
SOLVED! My problem was due to the [core] editor part of the global settings of .gitconfig. Once I changed it to editor = vi I was able to exit out using VI's shortcuts. Thanks everybody for helping me out.

It is possible that there is something in your ~/.vimrc file that is causing problems. You might try moving your .vimrc file to a temporary one, then start vim basically without a configuration and see if the problem persists. If it does then put the file back and start commenting things out until you find it.
It is also possible that your terminal is passing through weird keystrokes or something.

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.

ZSH shell and CLion/PyCharm integration, movement around terminal

I'm trying to use zsh in the CLion terminal window (changed my shell to /bin/zsh)
But when I try to move around using Ctrl + Left, Ctrl + Right, Ctrl + E... I get the literal characters D, C and Ctrl + E opens the "Recent files" UI
Is there any way around this? I'd like to use the movement keystrokes as I can with bash.
Tempoz,
Check out the offical Zsh Line Editor doc and the Z-Shell Line Editor guide for tons of detail.
To start, you can see your current mappings by typing bindkey at your prompt. There are two start modes: emacs [default], and vi. If you want to use vi mode, add this to your ~/.zshrc
bindkey -v
and source it (or restart your shell, or restart your IDE). Check out how the bindkey output has changed.
If you decide you want to change or augment your key mappings, use bind in your ~/.zshrc to reassign, or add to your key map.
# Example key binding change
# bindkey key-sequence editor-command
bindkey '^Z' vi-kill-line
You will also probably want to fix your $PATH - all the JetBrains products do it incorrectly. See this answer: https://stackoverflow.com/a/51006003/1089228

How do I get out of ESC mode in zsh?

When using zsh, I sometimes accidentally press Escape out of habit, expecting it to clear the entire line as it does in Windows. Instead, it goes into a mode that I'm not sure how to get out of. The cursor goes back one character, and some keys perform some special commands, but all I really want to do is get out of this mode and be able to press Ctrl+U to clear the line.
Searching around has been tough - I get results for escaping characters.
Short answer: press a.
Medium answer: press a, then enter bindkey -e.
Long answer: Like a lot of UNIX shells, zsh has an emacs-like mode and a vi-like mode. You're in vi-like mode, and ESC takes you out of the vi-like insert mode. a puts you back into insert mode, with the cursor after the current character. (Sorry for the two different uses of "mode," but it is the accepted terminology in both cases.)
bindkey -e overrides the settings from the rc files and puts zsh into emacs mode, which only has one mode (i.e., no "ESC mode"), so this won't bother you any more. Unfortunately, it won't carry over to your next shell invocation. bindkey -v would switch from emacs mode back to vi mode.
In the absence of any other configuration, zsh defaults to emacs mode, so unless there's something in one of the rc files, the likely culprit is that the EDITOR variable is some form of vi, which causes zsh to default to vi mode. If you don't like vi mode, then you should probably hunt down what part of the system-wide or user-specific configuration is causing zsh to default to vi mode and turn it off by removing it or overriding it in one of those rc files.
If everything else fails, when you're in ESC mode, type :w then Enter to save, and then :q to exit. You can also type :wq and Enter

How can Vim stop responding?

With an intention to increment a number in a line in Vim, I pressed Ctrl+A. To my wonder Vim stopped responding! Later I realized that I had actually pressed Ctrl+S.
Can somebody please explain this behavior?
I've made some changes to the file, but not saved before vim stopped responding.
(Aside: Ctrl+X decreases the next number on the line)
You've stopped the terminal with CTRL-S. To resume, press CTRL-Q.
Btw, this is not VIM-specific, but rather terminal-specific.
To disable the Ctrl-S/ Ctrl-Q completely from your terminal, add the following line to your .bashrc :
stty -ixon

How can I invoke VIM with C-X e for long, complex, tricky commands?

I found an awesome tip here. You can "[r]apidly invoke an editor to write a long, complex, or tricky command". However, when I press the key combination above, I get Emacs open. I would like to switch it to Vim. How can I invoke Vim with C-X e?
[1. Problem SOLVED by Brian Cambell]
export EDITOR=vim
Add to your .bashrc or appropriate shell rc file
[2. Problem SOLVED thanks to Pax]
I was unable to get the tricky command back to Bash. The errors were:
> Error detected while processing BufRead Auto commands for "*":
> E117: Unknown function: JumpToLastPosition
Quotes in .vimrc solved the second problem. I am still unsure about the part in my .vimrc:
" augroup misc
" autocmd!
" autocmd BufReadPost * call JumpToLastPosition()
" autocmd FileChangedShell * call WarningMsg("File changed outside of vim")
" augroup end
[3. Problem]
What do the above part in .vimrc do?
On most Linux installs (all the ones I tested), bash recognizes both the Emacs and Vi command history keys (or you can use "set -o vi" to force it).
So, you can just use the vi-mode "<ESC>v" to to enter visual mode, this will start editing in a Vim session.
To run the command, you just save and exit from Vim ("ZZ" or ":wq"). To cancel the command, you need to delete the contents, save and exit ("1GdGZZ").
In addition to running it by exiting, you can also save it while in the editor to another location (":w /tmp/myscript").
Keep in mind that visual mode will work with the currently selected line so you don't have to start with a blank command ("<ESC>v"). You can use the normal vi-mode tools to select a line from the history first and then enter visual mode ("<ESC>kv" for last command, "<ESC>/grep<ENTER>nnv" for third-last grep command and so on).
Using this method has the advantage of not changing the "EDITOR" variable which may be used for other things (unless you want Vim for everything, which is a very sensible position to take IMNSHO).
Update:
Regarding your error, posted after the question:
JumpToLastPosition() is the function called by Vim for all files to put the cursor where it was when you last edited the file. I'm going to assume you're actually getting this error when the editing starts, not when you exit, since this is the auto function following a buffer read.
Can you start a "normal" vim session ("vim xx.txt" and then "vim xx") without this error occurring? You may find you get the same problem (and possibly only on the last one).
If you do have the same problem, you need to look at your startup files. It's possible the autocmd for BufRead is broken somehow. Have a look inside your vimrc and you filetype.vim files to see where that function is called and/or defined (I suspect it's called but not defined and that may be a mismatch between the two files or one of them has been damaged).
export EDITOR=vim
Add to your .bashrc or appropriate shell rc file
The link that you provided contains the answer:
Next time you are using your shell, try typing ctrl-x e (that is holding control key press x and then e). The shell will take what you've written on the command line thus far and paste it into the editor specified by $EDITOR.
You need to set the EDITOR environment variable (like #Brian Campbell)

Resources