Mapping 'Ctrl + O' in vim doesn't work - vim

I freshly installed Ubuntu 16.04 and then I installed vim. Soon I realized that Ctrl + O which I regularly use in vim to jump to the last place, does not work! After some search, I got the following commands:
:nnoremap <M-Left> <C-O>
and
:nnoremap <X1Mouse> <C-O>
I tried putting both these in my .vimrc but they don't give me the desired effect. I am really annoyed by this since this is one of the most used things that I use. What am I missing?
Edit: I reinstalled vim from scratch, also deleted the ~/.vimrc file. After deleting the file, I checked for /usr/share/vim/vimrc; it was there. Then I installed vim and tested for Ctrl+o; it works till I close the file. However, after I open it back, it forgets all the history.

Well, the actual answer as mentioned in the comments is your vim config files didn't have proper permissions on them. In addition to that, the key mappings you used in your question are invalid according to the docs:
:help X1Mouse
The X1 and X2 buttons refer to the extra buttons found on mice.
The 'Microsoft Explorer' mouse has these buttons available to the right thumb.
Currently X1 and X2 only work on Win32 environments.
And
:h M-Left
Alt-Left Move cursor to the beginning of the previous word
M = alt.
Put this in your vimrc. Enable the mouse first:
set mouse=a
map <LeftMouse> <c-o>
Although Vim uses inefficient gluing of movements together if you want to get to a far away arbitrary place on the screen, which the mouse solves well and much faster than Vim :)

Related

Vim backslash in command

I feel like this is a very stupid question, but I can't seem to find the answer to this anywhere. I'm relatively new to vim. After a fresh install I installed the vim-LaTeX plugin, or the LaTeX-suite for vim. It works correctly apart for the fact that there are some commands I don't understand. It gives the following instruction:
To do this, visually select a portion of the text and press \ll while in visual mode. The visually selected portion will be saved to a temporary file with the preamble from the current document prepended. Latex-Suite will then switch focus to this temporary file and compile it
But if I press '\' it does nothing and double l moves the cursor to the right. I don't understand what I'm supposed to be doing, and I feel incredibly stupid. Sorry if off-topic.
The \ is the default value for the :help <Leader> key. Plugins are encouraged to start all default mappings with <Leader>. If you've redefined it, you need to use that key instead.
Otherwise, you need to press V \ L L (without Shift) in quick succession (by default within one second).
If you want to find out whether the plugin has been successfully installed and there's actually an action behind those keys, you can use either:
:verbose vmap <Leader>ll
:verbose vmap \ll
If Vim says No mapping found, or just lists other mappings starting with some of the keys, the plugin isn't installed properly, or you're not using it right.
Press v to go into visual mode. Then you can use your arrow keys to select text. After you've selected the text you want, type \ll.
That should be it.

vim - navigating characters in command mode

Relatively new to VIM and having a great time using it.
One very minor annoyance I've been having is command mode character navigation when I want to revise a command. I'm used to using readline shortcuts on the regular (non-vim) command line but these shortcuts don't seem to work in : command mode.
For example, using ctrl + b to go back a character ends up sending me to the start of the line, or using alt + f to go forward a word ends up clearing the line and exiting command mode.
The only way I've found to navigate in command mode is to use the arrow keys, but I'm under the impression you should avoid the arrow keys in vim for max efficiency.
What is the standard way to navigate around in : command mode? Do vim users usually use the arrow keys here? Is there a different way to modify commands?
As a more concrete example, I've been using vimgrep a lot to search through files. I'll do a command like:
:vimgrep /font-family/j my-project/**/*.less | cope
Later, I'll want to use the same search but look for a different property, so I hit : then ctrl + p to access my previous vimgrep. Now here I have to use the arrow keys to navigate backwards to the search string and modify it. It would be much faster if I could use readline to navigate backwards by word then delete by word.
For small edits, Backspace and light use of the cursor keys should be fine. For anything larger, I would advise to use the command-line window:
In the command-line window the command line can be edited just like editing
text in any window.
So, there's no need to mentally switch to readline key mappings; just use the full editing power (including any custom mappings) of Vim!
You can switch via <C-F> from the command-line, or directly enter it (from normal mode) by pressing q: instead of :. All the details can be found at :help cmdline-window.
I like this question. Long time vim user, but new-ish here, so I can't vote it up. But indeed, perhaps unofficially, many vim fans feel that most of the time the goal is to not have your hands leave home row position (fingers move, hands relatively still).
I will admit, when it comes to command mode, I use the arrows. With P being on my pinky finger, I would miss-hit ctrl-P a lot, and it's faster to slide my right hand down (on my Natural keyboard) and find the up-arrow by quick feel, instantly, to do the same thing. Once I'm there, left-right arrows are also easy to find without looking or delay. Also Ctrl-arrows let you skip by word.
One of the great things about vim is the :help. I have easily spent tens of hours over the years reading through it, and it solves 95% of my problems if I have enough time and working-memory to push deep enough into it.
Here is what I found for :help readline:
READLINE readline.vim ft-readline-syntax
The readline library is primarily used by the BASH shell, which adds
quite a few commands and options to the ones already available. To
highlight these items as well you can add the following to your
|vimrc| or just type it in the command line before loading a file with
the readline syntax:
let readline_has_bash = 1
This will add highlighting for the commands that BASH (version 2.05a
and later, and part earlier) adds.
Give it a try! (I am using vim 7.4)
You can see a list of the default key binds with :help cmdline-history (scroll down a bit) and :help ex-edit-index.
You can remap this with cnoremap:
cnoremap <C-b> <Left>
cnoremap <C-a> <C-b>
" .. Probably more
Note that using cmap will probably get you into problems here since the right-hand side is the currently mapped action, whereas with cnoremap the right-hand side it will always use the native Vim action.

VIM keybinding to jump back to initial position after indenting the whole file

I have created a keybinding that should indent a whole file.
My first solution looked like this:
map <F4> gg=G
The problem is that after pressing F4, the cursor jumped to the first line of the file. So I have tried to improve my solution with the feature of markers, look like this:
map <F4> mzgg=G'z<CR>
I expected this would have resolved my problem, but the command do the same as the first. When I try to jump to the z marker manually vim told me "marker not set".
After changing the keybinding, I have or course restarted vim! I am using the GVIM 7.3 on a WIN 7 machine.
Thank you in advance for your Help!
Edit:
After trying to get my keybinding working by tipping it directly to vim commandline. I find out that the keybinding was working quite nice. I think problem is that I create a session some times ago (with mksession) and if you load a session I think vim ignores the vimrc-file. Is this assumption right?
Solution:
In these thread I find a soultion to make mksession save less options.
Another lightweight approach: set the ` mark, format the buffer and jump back to the mark afterwards.
:nnoremap <key> m`gg=G``
I would recommend the use of CTRLo and CTRLi which allow to go respectively backward and forward in the jump list. See :help jumps.
The following mapping
map <F4> gg=G2<C-o>
works. (It jumps back two times in the jump list)
But generally, the jump list is a great way to navigate in a file, this is likely the shortcuts that use the most in my daily use. It is also works if you jump to a tag to go back to your original location.
You might also want to use nnoremap rather than map, this way it will only work in normal mode, and you could potentially reuse F4 in combination in another key binding without having recursive mappings.
so
nnoremap <F4> gg=G2<C-o>

How to go back (ctrl+z) in vi/vim

In normal text editors [with all due respect to Vim] there is a shortcut Ctrl+Z when you have done something nasty and want to return to the previous version of the text. Like BACK button in Word. I wonder how can you achieve this behaviour in Vim.
You can use the u button to undo the last modification. (And Ctrl+R to redo it).
Read more about it at: http://vim.wikia.com/wiki/Undo_and_Redo
The answer, u, (and many others) is in $ vimtutor.
Just in normal mode press:
u - undo,
Ctrl + r - redo changes which were undone (undo the undos).
Undo and Redo
Here is a trick though. You can map the Ctrl+Z keys.
This can be achieved by editing the .vimrc file. Add the following lines in the '.vimrc` file.
nnoremap <c-z> :u<CR> " Avoid using this**
inoremap <c-z> <c-o>:u<CR>
This may not the a preferred way, but can be used.
** Ctrl+Z is used in Linux to suspend the ongoing program/process.
On a mac you can also use command Z and that will go undo. I'm not sure why, but sometimes it stops, and if your like me and vimtutor is on the bottom of that long list of things you need to learn, than u can just close the window and reopen it and should work fine.
I had the same problem right now and i solved it. You must not need it anymore so I write for others:
if you use gvim on windows, you just add this in your _vimrc:
$VIMRUNTIME/mswin.vim behave mswin
else just use imap...

Ctrl + S is not working as a horizontal split in VIM when using CommandT

I've been tweaking my ~/.vim quite a lot lately, and it seems that I broke something.
I'm using the CommandT plugin, which when the search is open allows for the following:
enter - open the file in current buffer
Ctrl + S - open the file in horizontal split window
Ctrl + V - open the file in vertical split window
The problem is, that Ctrl + S suddenly stopped working in terminal VIM, only Ctrl + V is working. At first I thought I broke something in my general config, but then I tried it in MacVim, and Ctrl + S works just fine.
I'm not aware of having set anything specific only to the terminal VIM.
Is there any way that I can tell, if a key is being rebound to something else and where is that happening?
btw, my ~/.vim is a fork of Janus with most stuff removed and changed, I'm keeping it mostly for the Rakefile for installing plugins. here's the repo
I've found solution for this problem here: https://apple.stackexchange.com/a/34503
Just put in your .bashrc / .zshrc this line of code:
stty -ixon -ixoff
You can list all current mappings by typing :map inside the command window.
You could also clear all maps using :mapclear and then map <C-S> again.
It could be that you vimrc contains alternate maps depending on the environment. Look for things like has("gui_running").
Finally when running MacVim gvimrc is sourced vs. vimrc when running vim in the terminal.
Depending on the differences in both of those, mappings and other features will change.
I noticed, that you are talking about Cmd key in the title, but about Ctrl in your description. The Cmd key does not work in terminal vim.
If you are talking about the Cmd key, consult my answer to a similar question.

Resources