I have a mapping
:cnoremap ch call ShowHistoryMatching
The problem is that the ch characters expand to the right sentence in any case they are typed, no matter if at the beginning or later in the cmap input.
The problem is when I try to search for words in vim using / or ? e.g. for
/cache - it will be expanded using the mapping above.
How can I set the mapping ch to be extended only when it occurs at the beginning of the command?
cmap's are notoriously tricky because they often execute in the wrong context. Some better alternatives:
Use a normal mapping e.g. nnoremap <leader>ch :call ShowHistoryMatching()<cr>
Create a command e.g. command Ch call ShowHistoryMatching()
Use a the cmdalias.vim plugin
Use a more clever abbreviation as described in vim change :x function to delete buffer instead of save & quit post. Similar technique to cmdailias.vim.
Personally I would just create a new command.
You can use the getcmdpos() function to determine if you're at the beginning of the line or somewhere else. This technique can replace a built-in command using an abbreviation or you can adapt it for use in a mapping, possibly with an <expr> mapping.
I often find myself doing
:s/foo/bar/g
*move to different line*
:s/bar/foo/g
on different lines. Is there an easy way to swap them around so that I can execute the second version quickly?
You could try the Abolish plugin (git homepage):
:Subvert/{foo,bar}/{bar,foo}/g
Without plugin:
:%s/foo\|bar/\=submatch(0) ==# 'foo' ? 'bar' : 'foo'/g
The quick use once only option is to do the following
:s/~/<c-r>//g<cr>
~ matches the last substitution and <c-r>/ will insert the current search string from the "/ register. Therefore flipping the substitution. A word of warning is that ~ can only be used once because after the substitution it will be changed. Also doing a search between substitutions will result in the "/ register changing.
As an alternative you could try to use the command-line window to edit the command like text in any other window.
Use q: to open the command-line window from normal mode or press ctrl-f from the command line (assuming the default setting for 'cedit').
Drew Neil has a vimcasts episode that deals with refining search patterns via the command-window which is similar.
:h /~
:h c_CTRL-R
:h quote/
:h cmdwin
:h q:
:h 'cedit'
You can use dict in vim
:%s/foo\|bar/\={'foo':'bar', 'bar':'foo'}[submatch(0)]/g
I recently started using Conqueterm in gvim in fedora, it's lot of pain to type all that, so is there any way to map it to a shorter key
i need to have this map to command mode(ESC -> :ctp) rather than in normal mode(ctp)
User defined commands must always begin with a capital letter. Lowercase
commands are only the built-in ones. Anyway, you can create a shorter version
of your command by adding something like this to your .vimrc:
:command! Ctp ConqueTerm
Or add arguments to create an even more handy command:
:command! Ctb ConqueTerm bash
Another option is to check your autocompletion settings, and type :Con and
hit the tab key. It should autocomplete to the full command. A map is also
valid:
:cnoremap Ctp ConqueTerm
Or an abbreviation:
:cabbrev Ctp ConqueTerm
As you can see there are multiple ways of doing it. Each one has its unique
aspect, though sometimes you'll not notice in simple commands like this one.
Pick up the one that works better for you.
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
I want to add a shortcut like I type :open_my_document, it become :e /var/book/document.txt.
how to?
If you want to create a custom command, use :command:
:command Open_my_document e /var/book/document.txt
Then you can use:
:Open_my_document
Normally you can just type :Ope and then hit tab and vim will complete the full command name.
Note that your command needs to start with a capital letter because only built-in vim commands can start with lower-case letters.
I think you might be looking for something like :h cabbr or :h cmap.
If you use cabbr, your text will not change once you enter your custom command. If you use cmap, it will. It's easier to just try both out to see which you'd rather use.
I don't know advantages/disadvantages of one or the other, personally, I use cmap.
Add this to your .vimrc:
map :open_my_document :e /var/book/document.txt