how to write a shortcut map in vim? - vim

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

Related

How to efficiently edit strings after dot in vim

In vim, I have to change someText.someTextAfterDot string to someText.somethingDifferent. The cursor is at the t in After. What should be the most efficient way to do this?
If I use 'cw' command then that removes the complete someText.someTextAfterDot including someText which I don't think is efficient because now I have to type the someText again.
Normally, I use 'F.ce' (find the last . and edit till end of current word) command and then type somethingDifferent. This seems like a lot of work as in other editors (like WebStorm or Sublime) a simple double click on someTextAfterDot can do the trick.
Is there any other more efficient way to do this in vim?
First of all cw should only go forward so only remove terDot. If cw is doing anything more then I assume you have some bad mappings.
The "inner" word text object, iw, is what you need. e.g. ciw. However if you have added . to 'iskeyword' then word's will include . and the behavior of ciw will be incorrect.
To find out where 'iskeyword' is last set use the following command:
:verbose set iskeyword?
For more help see:
:h 'iskeyword'
:h iw
:h w
:h cw
:h :verbose
My intuition would be to type bdeisomethingDifferent. I dunno that it's a great improvement, but it at least doesn't make you type the . again the way F.ce does.
I normally use change inner word for that. You simply have to type
ciwsomethingDifferent
Check the documentation for text object selection too.

Vim auto-completion on substitutions and searches in the command line

Is it possible to use Ctrl+n style auto-completion when doing searches and substitution commands in vim?
What I mean is: say I have a variable named myNumber in my current file. If I enter insert mode and type myNu and then hit Ctrl+n, it will autocomplete the word to myNumber (assuming there are no other words that start with those letters).
Is there any way to get this same functionality on the command line? Is there a setting or plugin that will allow me to type /myNu and then hit a key to complete my text search to /myNumber ? Or, more realistically: let me type :s/myN and complete that to :s/myNumber so I can more quickly type out substitution commands?
While in command mode, press Ctrl+f — you will enter command line window. There you can edit your command like in vim. Auto-completion should work too.
Looks like the plugin http://www.vim.org/scripts/script.php?script_id=2222 does more or less exactly what I wanted. Upvoting Kent though, because that's really nifty.
This plugin also enables auto-completion in command line.
https://github.com/vim-scripts/sherlock.vim

How do I change the way vim autocompletes commands?

I use the :split in vim all the time, I however neveer uses :spelldump or :spell* in general.
Is there any way to make :split be the first commmand to appear when autocompleting on :sp ?
Like I said in the comment you don't need to autocomplete for this particular use case, :sp will do the trick. If you enter part of a builtin command that is ambiguous, Vim prefers one command over the other:
:s could be :substitute, :split, :spelldump, etc., but for Vim it is :s[ubstitute]
:sp could be :split, :spelldump, :sprevious, etc., but for Vim it is :sp[lit]
In the help this is indicated with square brackets around the optional part of the command just as is shown above.
To answer the general question: I don't think you can change the way Vim autocompletes commands. You can define your own shorter command (which must be uppercase), e.g. :command! S split. Or you could define a mapping, e.g. :nnoremap \s :split<CR>. Finally, you could use a builtin normal mode command, which for this particular use case is simply CtrlW followed by S.

how to add vim keymap

While programming I am regulary using the following two lines:
sprintf(buff,"%s", __func__);
putrsUART(buff);
Is it possible to set any keyboard shortcut to insert these two lines?
E.g. when I type \sp in command mode, these functions get added at the cursor position in my file. Is this possible? And if so, how do I map my keys?
Thanks in Advance.
You can use abbreviations, which are designed for this.
:abbr spb sprintf(buff,"%s", __func__);
:abbr uart putrsUART(buff);
Use :help abbr for the gory details. Note that you need to type another character after the abbreviated form for vim to recognize them. This comes naturally for these as you will type ENTER as the next character. It is also possible to enter more than one line with abbreviations. Simply use <CR> where you want a new line.
Here's an easy mapping for normal mode that lets you hit \sp (unless you've remapped leader, in which case use that instead of \) in order to insert the sprintf statement.
map <Leader>sp isprintf(buff,"%s", __func__);<Esc>
That being said I think abbreviations are the way to go here
As already mentioned, abbreviations (which I would limit to insert mode (:iabbr), because you probably won't need them in the command-line) are best for simple expansions; you can also define them only for certain filetypes only (via :iabbr <buffer> ...).
Your __func__ looks like a template parameter that you need to adapt each time. You cannot do this via abbreviations, but there are various plugins (many inspired from functionality in the TextMate editor) that offer template insertion with parameter expansion and several advanced features. Check out one of snipMate, xptemplate, or UltiSnips.
Try snip-Mate for inserting regularly used codesnippets. http://www.vim.org/scripts/script.php?script_id=2540
Wrong answer, Sorry:
Try this in your vimrc:
map <c-w> :sprintf(buff,"%s",func)<cr>
This means mapping to Ctrl-W.

VIM: how to add search/replace command to vimrc and map to a shortcut

I have two search/replace commands that I find myself running in vim fairly often to clean up html code so I can copy/paste it online. The commands are:
:%s!<!\<!g
:%s!>!\>!g
I wanted a way I could map both of these commands to be run together ... I did some searching for how to use the :map commands in vimrc, however, I can't see how to combine the two lines into a single command that is run with a single keystroke (or a single sequence of strokes).
Thanks!
You can put the commands on a single line separated with a bar.
:%s!<!\<!g|%s!>!\>!g
But you'll have to escape it in the map command
:map <F3> :%s!<!\<!g\|:%s!>!\>!g<CR>
:TOhtml
will create a new buffer containing your previous buffer HTML-ized, including entity escaping (and syntax highlighting, if you had that enabled). See :h TOhtml for more information.
If you are using this search/replace pattern to HTML encode entities, you might want to check out the unimpaired plugin. Amongst other things, this provides shortcuts for encoding and decoding XML, URL and C strings.
:map <F3> :%s!<!\<!<cr>:%s!>!\>!<cr>
of course can be replaced with whatever key you wish
I've not tried it, but can you not put them on the same line separated by "<CR>"?

Resources