VIM: Know which command executed when a key pressed - vim

How do I know which command will be executed when I press a key, for example <Leader>c?

To see mappings use:
:verbose map <leader>c
Replace map by the corresponding imap, cmap, etc., as needed.
For Vim's built-in commands you'll need to use the help:
:help gq
See :help context for pointers.

Sometimes if map <keys> is not enough you may use one of the following:
:debug normal <keys><CR>: for normal, visual, select, operator-pending and insert/replace/virtual replace modes but not for ex/command-line mode. You will have to precede <keys> with something that enters target mode.
:set verbosefile=/tmp/verbose.log verbose=15<CR><keys>:set verbose=0<CR>: for all modes it will procude a log of all commands executed in file /tmp/verbose.log. You will see errors if there is a recursive structure somewhere.
Start vim with vim -s <(echo '<keys>') -D. It will enter debug mode immediately after vim starts, but you will have to skip all initializations manually.
These are all advanced debugging features and they are very time-consuming, but they may help where something more simple cannot.

Related

jumping with Ctrl-] in vim does not work

I am working with vim on debian.
When I press ctrl-] vim beeps, inserts a new line and goes to "visual block". It does not jump to tag. The same happens with :ctrl-]. Even in insert-mode with ctrl-] vim exits from insert-mode, insert new line and goes to 'visual block'.
But when I use such lines in .vimrc
nnoremap <F3> <c-]>
vnoremap <F3> <c-]>
jumping with F3 works.
Command :verbose map c-] returns No mapping found
update:
As we have discovered in insert mode we can see how vim is interpreting <c-]>. In insert mode after <c-V><c-]> the correct output should be ^] . But my output is
t
^C
Screenshots: after <c-v> I get
And after <c-v><c-]> .
So vim isn't receiving the keystrokes properly. How to check what changes input?
try running vim -u NONE to check if that's happening without any configuration, that way you can be sure it's not a mapping or misconfiguration.
if that solves the issue, check your vim configuration!
Also try doing it in gvim and gvim -u NONE to narrow down the issue to what it's very likely to be. If that works it's a shell or terminal misconfiguration.
if it still fails in the windowed GUI of vim, that means this is X that is wrong, try using xev to see what your key outputs, and xmodmap to printout the values assigned to all your keys on your keyboard layout.
Try using another terminal (like urxvt, gnome-terminal, xterm or the raw linux console), to determine whether it's a terminal emulator misconfiguration of your keys or if it's your shell.
if it's your terminal emulator, then get through the configuration or reset its configuration, there's something setup that shouldn't have been.
Try changing shell to see if that improves (by I doubt it will)… And add the following configuration files in your home directory:
.inputrc
Tab: complete
set meta-flag on
set input-meta on
set output-meta on
set convert-meta off
If that's not enough, you might want to check as well you stty settings for your current shell as well.
If none of that helps, then… take a big hammer and hit very hard your keyboard and your computer, that won't solve your situation, but you'll feel better!
HTH

Error in my Vim mapping

I would like to map Control + F3 with map <c-f3> :w <CR> :make <CR><CR><CR>. When I leave out the Control this works fine. With control from normal command mode insert mode is entered and 1;5R is inserted into the buffer.
When I use a simpler command e.g. map <c-f3> dd this works as expected.
With Shift instead of control I get a similar behavior (1;2R inserted into buffer). What can I do get my mapping right?
Edit: I want the command to be executed from normal command mode. However, vim enters insert mode, which is not desired.
The combination of function keys and modifiers is problematic in terminal Vim; whether this works depends on the terminal.
Check your $TERM value; a wrong one can cause these problems. Also, your termcap database must be correct and complete.
Try out another terminal (gnome-terminal vs. konsole vs. xterm); it may work there.
Alternatively, use GVIM; it doesn't have these problems (but still can't map some other combinations).
If you use varying terminals, it's best to avoid these key combinations, and just use plain <F1..12>, and <Leader>... for the rest.
map defines keystrokes for normal, visual and operation modes. For insert mode you should use imap.
Something like this I believe: imap <c-f3> <esc>:w :make <cr><cr><cr>. <ESC> puts the Vim to normal mode.
:he :key-mapping for explaining.

vim goto file in new tab-page as the default behavior

Background
Vim 7.3 all platforms.
Vim allows the user to open a file "hyperlink" by pressing gf.
Problem
Goal
Find a solution that makes gf work with tab-page as the default behavior.
Details
The current default behavior opens the new file in the same viewport, but sometimes a user might want to open the new file in a different tab using the Vim tab-page feature.
Alternatively, a user may wish to always have gf use tab-page and never open the linked file in the same viewport.
Vim allows the gf command to open in a new tab, but it requires the user to type control-W <C-W> before invoking gf.
The existing approach is not optimal, because the keystrokes are cumbersome and introduce the risk of repetitive strain injury for users who make heavy use of the gf command.
See also
Vim help:
:help gf
:help tab-page-intro
:help tab-page
:help tab-page-commands | /_gf
:help CTRL-W_gf
Desired solution
It would be desirable to do away with the need to type <C-W>gf all the time, in order to save keystrokes and reduce the risk of repetitive strain injury.
Failed attempt
The following attempt did not work (use a normal command):
:normal <C-W>gf
Questions
why does not :normal <C-W>gf work as expected?
what alternative approaches will work to make Vim exhibit the desired behavior and reach the goal?
The answer to your first question:
What comes after :normal is supposed to be raw key presses, as you'd type them in normal mode. What you used means nothing so it does nothing (you are lucky, it could mean something and fuck up your entire buffer!).
It would work if you inserted the raw representation of <C-w>:
:norm <C-v><C-w>gf
which should look like:
:norm ^wgf
The answer to your second question:
nnoremap gf <C-w>gf

How do I start vim in insert mode?

Whenever I enter vim, there are 99% chance that I will go in insert mode and edit the file. Can I make vim always start in insert mode?
You can start vim like this:
vim -c 'startinsert' FILENAME
If you want, you can edit the .bashrc file (if you are using bash) and add this line:
alias vim="vim -c 'startinsert'"
You can use vim +star, which is even shorter. NB: star is short for :help :start.
If you want this behavior by default, the best option is to add the line
autocmd BufRead,BufNewFile * start
into your ~/.vimrc. Also take a look at :h 'insertmode', which outlines a special option made for this kind of functionality. However, it can make it difficult to get out of insert mode which is crucial for growing in your vim ninja skills.
You can, and it's very simple.
The :startinsert command enters insert mode. (It is the exact command-line-mode counterpart to typing i in normal-mode.) Just drop it into your vimrc so it runs at startup. Unlike some of the other suggestions, this doesn't interfere with dropping back to normal-mode by ESC as normal.
Additionally, there's something called "Easy mode", started from vim -y or evim. It's a more radical departure than just starting in insert mode: it has some key bindings matching other editors', and normal-mode commands are done by hitting Ctrl+O instead of Esc. As a consequence of that, being in insert mode is the rule rather than the exception.

What's the use of the exclamation mark ('!') in Vim's command line after certain commands like ":w!"?

Basically I would like to know the difference between: :w and :w! or :wq and :wq!
The ! qualifier tells Vim to force the operation. For example, if the file was read-only you would use :w! to write it anyway. If the file was modified and you wanted to quit without saving, you would use :q!. :wq! just means force write and quit in one command.
In your examples the exclamation point means to force the action (e.g. :w! will overwrite an existing file and :q! will quit without saving).
That said, there are many other uses depending on the command, e.g.:
:set <option>! toggles a boolean option, e.g. :set number!
! followed by some shell command executes that command directly from the
editor, e.g. :! ls /etc
:w !cmd pipes the contents of the current buffer to the command cmd, e.g. :w !sudo tee % (a.k.a. the write with sudo trick).
Besides the situations where the exclamation point forces things, like writes, it will turn a command into a toggle command. So if I do:
:set cursorline
the line my cursor is on will be highlighted. I can turn it off with:
:set nocursorline
Or I could do:
:set cursorline!
That command flips between the two settings, off and on.
I turn cursor line highlighting off and on frequently, and the toggle command lets me do it with a simple function key mapping. Without the toggle, I would need either two mappings: one to turn it on, and a second to turn it off. Or I would have to write a function to determine whether the cursorline setting was on or off, and then turn on the opposite setting.
This works with, as far as I know, all command line settings that have on and off settings, like hlsearch, paste, cursorcolumn, number, insearch, etc.
Note also that the exclamation point will toggle the no version of the command. For example, you could also toggle the cursor line setting with:
:set nocursorline!
It really depends on the command considered. Regarding the ones you have enumerated, it forces the command as others have already answered you.
However, there are other commands like :global, :map, :make, :silent, ..., where the bang (!) has other effects. Read their documentation:
:help help
(and we can give the bang any meaning we want in the commands we define)
example
meaning
:wq! :q!
do it anyway!
:autocmd! {group} {event} {pat} cmd
override specific autocommands (:help autocmd-remove*)
:function!
override existing
:com[mand][!] [{attr}...] {cmd} {repl}
override existing
:set number!
(override 0 with 1, or 1 → 0) toggle an option
:!ls
shell command
:com[mand][!] [{attr}...] {cmd} {repl}
Define a user command. The name of the command is
{cmd} and its replacement text is {repl}. The
command's attributes (see below) are {attr}. If the
command already exists, an error is reported, unless a
! is specified, in which case the command is
Command attributes
User-defined commands are treated by Vim just like any other Ex commands. They
can have arguments, or have a range specified. Arguments are subject to
completion as filenames, buffers, etc. Exactly how this works depends upon the
command's attributes, which are specified when the command is defined.
There are a number of attributes, split into four categories: argument
handling, completion behavior, range handling, and special cases. The
attributes are described below, by category.
......
Special cases
:command-bang :command-bar
:command-register :command-buffer
:command-keepscript
There are some special cases as well:
-bang The command can take a ! modifier (like :q or :w)
-bar The command can be followed by a "|" and another command.
A "|" inside the command argument is not allowed then.
Also checks for a " to start a comment.
shell
example
meaning
#! /bin/sh
shabang
!!
last history
!2
second last history
ptipython
example
meaning
:!ls
shell command

Resources