I want to change the shortcut for entering command-line mode in vim from : to ;.
I can do:
:map ; :
and that works, but now I want to remap : to other command. If I do that ; is changed too and I cannot enter command line mode any more.
I suppose I should use something like:
:map : <command-line-mode>
but I don't know the correct command to use.
:noremap ; :
:noremap : <something else>
Using map only will map the key recursively, meaning if you map ; to :, then : to someting else, now both ; and : will do whatever you mapped : to do. Because ; is mapped to do whatever : does. to prevent this recursive behavior, use non-recursive maps noremap, which will map each key independently of what the mapped-to key does.
Related
Is there any way to achieve that remapping? I looked a bit into
map
and
inoremap
but they appear to affect within a given mode, not how to enter a given mode.
You could use nmap, e.g.,
:nmap ; :
to map semicolon to colon. The 'n' in nmap indicates normal mode.
If you wanted to use, say, the <F2> function key to enter command-line mode from insert mode, you can do:
:imap <F2> <Esc>:
There's an exhaustive 3-part tutorial about key mappings in vim here.
I am trying to create a mapping the allows me to execute my current node.js file when I press comma + n. I am trying to use the following:
:map <cn> :!node .
When I type this in it simply jumps my cursor to a random line in my current file. How do I create this mapping?
You've specified the mapping keys in the wrong format, see :help key-notation. To trigger the mapping via , followed by N, use this:
:nnoremap ,n :!node %<CR>
Additional notes:
You should use :noremap; it makes the mapping immune to remapping and recursion.
Likewise, you should be as specific in the modes, so :nmap instead of :map for normal mode only.
A mapping works as typed. As you invoke an Ex command from normal mode, you need to conclude command-line mode via <CR>, just as you would press Enter when typing this interactively.
You probably want to pass the current file to node; that's done by the special % identifier, not by .. See :help cmdline-special
noremap :hsp :botright new
noremap :vsp :botright vnew
"Not an editor command: hsp"
I'm probably googling the wrong thing, but I can't find many results on aliasing vim commands. I can find tons of info about mapping keys to commands like my one for tabs:
noremap <C-t> :tabnew<CR>
But can't find commands mapped to other commands.
What you're doing is simulating a command with a mapping. You're saying that when you press the 4 keys :hsv in normal mode, it should type out the keys :botright new (which would need a <CR> to run, as others have said), but it's not actually making the command hsv. You could make an actual command with a user command (:h user-commands). These must start with a capital letter, though.
:command Hsp botright new
:command Vsp botright vnew
Now you can type :Hsp and hit enter to run the command botright new.
Did you try command abbreviation?
ca hsp botright new
ca vsp botright vnew
You will have to initialize the expansion of the abbreviation by hitting the space key afterwards. Depending on the global vim configuration, expansion also happens automatically just when enter is pressed.
with your same mapping, I cannot get the Not an editor command: hsp error message with my vim (v7.4).
Your mapping works fine, but you don't have <cr> at the end, so when you press :hsp in normal mode, your mapping will switch to commandline mode, and put the mapped command there, without executing it. You have to manually press Enter.
#XZS's answer works, but keep in mind that it is an abbreviation(ab), not a mapping. ab is not command aliases, it is not exactly same as mapping. For example, you have to press another key (like space) after hsp to trigger the ab. also, you cannot ab some special keys, this would be another limitation of ab.
There is c(nore)map for command mapping.
e.g. you could have:
cnoremap hsp botright new
with above line, as same as your original one, you have to manually press Enter, if you want it to be executed, you need add <CR> at the end of the line.
I think if I do this, I would create mapping.
Creating command alias can be tricky:
Using a simple cabbrev and/or cmap will cause expansions and mappings to fire in unexpected places like during searches with / and in the middle of filenames.
cmap's will have a visible delay in outputting to the screen which is why cabbrev is often used.
However there are a few ways to create a proper alias:
Create a command via :command.
e.g. command W w
command's first letter must be a uppercase letter
must supply -nargs, -bar, -complete, and -range options according to the needs of your alias
Expression :cabbrev to guard the abbreviation from expanding in in proper places.
expression mapping use the <expr> option
verify getcmdtype() is equal to :
verify the abbreviation is at the beginning of command line via getcmdline() or getcmdpos()
e.g. cnoreabbrev <expr> W getcmdtype() == ':' && getcmdline() ==# 'W' ? 'w' : 'W'
Use :Alias via the cmdalias.vim plugin by Hari Krishna Dara
e.g. Alias W w
uses an expression cabbrev under the covers similar to the technique above
Is there a way to unfold code when going to a line number? For instance I type :35 where line 35 is folded, then I have to unfold that section manually to actually get to that line. I would like to type :35and have that code then unfolded automatically and my cursor put on line 35 without any further key presses.
If you use the 35G command instead of :35, you can achieve this with the following mapping:
"[count]G Also open fold under cursor when supplying [count] (i.e.
" jumping to a particular line, not the end of the
" buffer). Use [count]|gg| if you don't want this.
nnoremap <expr> G (v:count ? 'Gzv' : 'G')
For :35 itself, this would be hard to achieve. You would have to intercept the <CR> via a :cmap <expr>, check the typed command via getcmdtype() and getcmdline(), and, if it's a number, manipulate the command, i.e. append normal! zv to it; like this:
cmap <expr> <CR> getcmdtype() == ':' && getcmdline() =~ '^\d\+$' ? 'normal! zv<CR>' : '<CR>'
zv. From :help zv:
View cursor line: Open just enough folds to make the line in
which the cursor is located not folded.
While this command could probably be triggered automatically in some way, I have not come across it yet. Using the command as-is has served me well, though.
Define a new command mapping. In this example, I chose \gz:
:nmap \gz gg<Bar>zO
In an attempt to make myself use the shorter keystroke to get a colon, I have the following mappings defined in my .vimrc file:
noremap ; :
noremap : ;
However, this breakes some of my other mappings, since now it interprets a colon as a semicolon. For example, the mapping
map ,c :cd ~/code<CR>
becomes
map ,c ;cd ~/code<CR>
How can I fix this?
The commands of the :map family interpret the characters in the
mapping definition as if they were typed by the user, so that any of
the currently defined mappings (including the one being defined) are
triggered as usual. It is the reason why it becomes possible to define
recursive or nested mappings when necessary. And this is also why the
colon mapping gets applied to other mappings defined via :map, like
the one in your example:
:map ,c :cd ~/code<cr>
To avoid this behavior, use the :noremap family of commands, which
do not interpret any mappings in the right-hand side of a mapping
definition (see :help :nore):
:noremap ,c :cd ~/code<cr>
In most cases, such interference with other mappings is an undesirable
side effect. As a rule of thumb, I would recommend one to go by the
following convention when defining a new mapping:
Always use the :noremap family of commands,
unless there is a clear reason not to.