Is it possible to improve this vim mapping that repeats a command on the command line? - vim

So here is my mapping:
nnoremap <cr> :nnoremap <lt>cr> :w!<lt>cr>:!tmux send-keys -t :1.1 "py.test --cov=." C-m <lt>cr><lt>cr><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left>
It's pretty awesome. What it does is when I first start my vim session (or after reloading a vimrc), and hit enter, I can immediately type the name of the tmux session that I'm in and hit enter again. Assuming that I'm editing my files in the zeroth pane of window 1 and I have a tmux split (horizontal, in my typical case). Subsequent times hitting enter in normal mode will save my active file and launch my py.test tests in another window. That means that I can technically continue coding before my tests have finished passing. I forget who I first got the idea from, but h/t that guy.
Anyhow, you've no doubt noticed that there are a lot of <left>'s in that mapping, because I'd like to start the command out being able to set the session name. But there also might be cases where I'm editing my code in a different window or something, or I need to change my pytest command or something, so I still want the ability to make those modifications.
Is there a way that I can improve this mapping? Maybe by approaching it in a different way altogether?

Your initial mapping builds another mapping command, and uses <Left> to insert the cursor on the right position to complete and then execute it.
An alternative to that would be defining a custom command (e.g. :TestInSession) that takes the variable parts as argument(s). Then, your initial mapping could just build the other mapping, leaving the cursor at the end of :TestInSession, and you would have less clutter in the command-line, and easier editing at the end.
If you need the ability to re-configure your other mapping, you could define an alternative initial mapping that isn't overwritten, e.g.:
:nnoremap <Leader><cr> :nnoremap <lt>cr> ...

Related

Sometimes Vim starts ignoring mappings on Ctrl-keys

Lately, when using Vim in tmux over ssh, sometimes something happens where Vim starts ignoring all my mappings that start with Ctrl: for instance <C-P>, which I have bound to the CtrlP plugin. Instead, Vim runs the builtin action (in this case, moving lines upward in the file). I don't doubt that the correct keypress is reaching Vim, as it uses the correct builtin action associated with <C-P> (using :send-keys to send ctrl-p from tmux also causes the builtin <C-P> in vim to run, instead of the mapping).
All my mappings not involving the control key still work. For instance, I can do this:
nnoremap p :echo "test"<cr>, and pressing p echoes test
But then immediately after,
nnoremap <c-p> :echo "test"<cr>, and pressing CTRL-P doesn't echo test, it moves the cursor up one line.
Restarting Vim always fixes the issue, but at some point, something I do causes the problem to surface again. I've been working mostly in tmux through ssh lately, so I'm not 100% sure if either one of those are the problem (although I think I recall this happening once in Vim in tmux not over ssh), but as mentioned above, I believe using send-keys directly from tmux as a test is ensuring that vim is getting the actual ctrl-p keycode. As well, <C-V><C-P> in insert mode does actually insert ^P.
Note that although I've used ctrl-p here as an example, since it's a key I actually use a lot in practice, this applies to any control key mapping.
Is there some kind of Vim state I don't know about that's causing this to happen? Is this likely a terminal problem? What are my next steps?

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>

Traversing directories with vim file name completion in insert mode (Ctrl-X Ctrl-F)

I’m trying to use vim’s compl-filename feature (Ctrl-XCtrl-F) to complete paths in INSERT mode, but I can’t work out how to traverse into directories without (temporarily) ending the completion mode:
Let’s say I want to complete the path /etc/sysconfig/network-scripts/ifup.
I would like to be able to do something like:
/eCtrl-XCtrl-F
/etc/
/etc/sysCtrl-F
/etc/sysconfig/
/etc/sysconfig/netCtrl-F
/etc/sysconfig/netconsoleCtrl-N
/etc/sysconfig/networkCtrl-N
/etc/sysconfig/network-scripts/
/etc/sysconfig/network-scripts/ifupCtrl-Y
/etc/sysconfig/network-scripts/ifup
The issue is, as soon as I start typing* after a path match (like /etc/), it ends file name completion. I would like it to stay in file name completion, so that I can still use Ctrl-F, Ctrl-N, etc. Since it ends completion, I have to type Ctrl-XCtrl-F again to restart it, and the helpful completion popup menu disappears in the meantime.
Is there an option I can set to change this?
* By ‘typing’ here, I am referring to characters in 'isfname' -- of course, typing other characters (like space or punctuation) should not continue file name completion.
I'm not sure exactly what you're saying, but you can just press Ctrl-XCtrl-F again on a directory while you're in the completion menu to expand it. You don't have to close out of the menu first. I just keep Ctrl held down and tap xf to traverse a directory, n and p to move up and down and w to go back up.
If you don't use :h i_CTRL-F then you could remap it. For example,
inoremap <C-f> <C-x><C-f>
Simple remap would be
inoremap / /<C-x><C-f>
So when you type slash(/) in insert mode you will get that auto completion popup :)
Place it in your .vimrc file (for vim) or in init.vim (for neovim)
Vim doesn't do auto-completion.
For that, you'll need a dedicated plugin like AutoComplPop or NeoComplCache
Please use insert "i" first before using cntr+x+f. I was in similar situation. :)

Vim "show my last command" command?

Is there a command which shows what was the last command in normal mode?
Suppose I accidently hit random key and got some unexpected result.
Sure I can undo it, but could I reveal what key was pressed and how it was interpreted?
Hit the colon (:) and then use the up arrow to start going back through previous commands. You can use the up/down arrows too to move around the list.
q: will show you command history in Vim.
q/ will show you history of searches.
And must importantly, :q will quit the mode.
The text from the last command is stored in the . register. You can see all registers by :display. Unfortunately it doesn't say what the started the normal command.
To see commands from : (command mode) you can use :hist or q: which is limited to the last 20 (by default).
Another ability is to save the undo buffer :wundo undo.bin -- but the undo buffer is binary.
But none of these actually answer your question. I'm curious if it can be done.
Entering colon : then ctrl+p shows your previous command, i.e., moving backward through your vim command history. ctrl+n moves forward.
This is very convenient if you're used to using the command line and prefer not to change your keyboard hand positioning to use arrow keys.
It is difficult to know it. You can play with the variables:
v:operator
v:count (and v:prevcount)
v:register
But you cannot fully get the last normal mode command issued.
However if you want to systematically record everything you type while in Vim, you can launch vim -W ~/.vim-last-scriptout (a Windows version: vim -W "%HOMEPATH%\Vim\.last-scriptout) You can alias it in your shell on a UNIX machine. Every single key, or control-key, will be recorded into that file. Note that if you happen to use gvim or vim -g (the GUI) you might encounter this bug.
If you want to replay this file you can use :source! (with the exclamation mark) or the -s option from the command line.
On Windows I have set gvimportable.exe -W gvim_directory\last_scriptout as my default editor in my Commander program (FreeCommander). This way I can always remember what I have typed to do something and repeat a sequence of commands on another file. Of course I have another shortcut for opening Vim and playing the scriptout.
Note that the file might be written only when Vim exits, so you have to lose your session to know what you've done.

Resources