Create vim function or map for gggqG - vim

I have this nmap defined:
nmap <leader>fr :gggqG<CR>
And when I execute it I get an "E492: Not an editor command gggqG" error. However, when I manually execute gggqG it does what it should (basically reformat the current document based on the new tw setting I've setup). I'm starting to want this command more and more and so am looking for a way to simplify the execution of it (I know just typing gggqG is not hard). Does anyone know how I can I define this thing (which I guess is not an editor command) as function or map? Many thanks!

gggqG is series of normal commands not command-line command.
nmap <leader>fr gggqG
You should also probably prevent recursive mappings and use nnoremap.
nnoremap <leader>fr gggqG

Related

Vim find in files shortcut from command

Hi I use vimgrep to find text in the project.
However its a little slow because i have to write this each time:
vimgrep /text/ **/*.* | cwindow
Is there any way to map it in for example C-shift-F and show the text box.
After that when I hit enter it will execute the vimgrep commands.
That will save much time
Thank you
Vim is highly customizable (which is one big reason for its popularity), so there are several possibilities to provide a shortcut:
incomplete mapping
A mapping does not necessarily contain a complete command. You can just prepare the command-line and leave the cursor in a certain spot, so that you can fill out the rest and trigger the (Ex) command with Enter. For example:
:nnoremap <Leader>f :vimgrep // **/*.* <Bar> cwindow<Home><Right><Right><Right><Right><Right><Right><Right><Right><Right>
The trailing keys position the cursor in between the //, so you can fill in text, and then execute the command.
Note: Your suggested <C-S-f> mapping would be the same as <C-f> (an unfortunate limitation in the current implementation), and therefore override a useful built-in command. I've instead used <Leader>f (where the default for that key is \; cp. :help <Leader>).
custom command
This is already nice, but assuming we'll never have a need to edit the other parts of the command, we can shorten this to a custom command, e.g. :FindText:
command! -nargs=1 FindText vimgrep /<args>/ **/*.* | cwindow
With this, you can search via :FindText text. If you still prefer a mapping, this would become
:nnoremap <Leader>f :FindText<Space>
Other benefits of custom commands are that these are easier to recall from the command-line history (as they are different from other :vimgrep commands you might use), and you're building a library of higher-level editing commands, which over time can be reused to build even more higher-level commands, which makes your editing more efficient.

Vim simple mapping

I started using Vim recently, just installed NERDTree (a plugin to navigate files).
The command to access that plugin is :NERDTree so I though it's a good idea to start learning mappings by assigning one to that command.
So I added to my .vimrc file the following line: map :nt :NERDTree - but when I type :nt in a vim file (even after restarting) I receive the following error message: not an editor command: nt
I also tried to add the mapping directly while editing a file by typing :map :nt :NERDTree but it returned the same error when I tried to use the command.
I checked that answer:What is the difference between the remap, noremap, nnoremap and vnoremap mapping commands in vim?, so it seems to me that :map (opposed to noremap etc.) is the good command for that.
The plugin works fine when typing the original command.
What am I doing wrong? (sorry for the noob question)
:NERDTree is a command, not a mapping, so there's no reason for creating a recursive mapping, here.
:map is too overreaching. You should use :<mode>map (for recursive mappings) or :<mode>noremap (for nn-recursive mappings).
You are missing a <CR> at the end of your mapping to tell Vim to actually execute the :NERDTree command.
In this specific case, the right mapping would be:
nnoremap :tn :NERDTree<CR>
But mapping something to :<anything> is not a good idea because it will introduce a timeout whenever you try to execute an Ex command. This means that you need to find another combo. Why not <Space>n?
nnoremap <Space>n :NERDTree<CR>
With the mapping that you have, it will be require multiple keystroke. Will it be okay for you to use a single key like F2?
nnoremap <F2> :NERDTreeToggle<CR>
This will toggle open/close NERDTree upon pressing F2 and save you some key stroke.
Here
you can figure out, how vim's mapping work and look like ;). Don't forget to source your new .vimrc before using.

Vim with VOoM and LaTeX-Suite plugins result in odd key mapping behavior

I use latex-suite to write .tex files. To easily switch between default compiler (pdflatex) and xelatex I have the following script in my _vimrc file.
function SetXeLaTeX()
let g:Tex_CompileRule_pdf = 'xelatex --interaction=nonstopmode -synctex=1 -src-specials $*'
endfunction
map <Leader>lx :<C-U>call SetXeLaTeX()<CR>
So in Vim, I can normally use \lx to call SetLaTeX() function. And actually I have similar scripts for other key-bindings such as \lp for pdflatex and \la for arara.
Everything works just fine until Voom outline command is called.
After runnig the command :Voom latex which generates Voom outline file .tex_VOOM1 alongside the .tex file, all these key mappings (\lx, \lp and \la) start behaving oddly:
They no longer calls my custom functions, instead they all trigger character-wise Visual mode.
It seems somehow Voom remapped all my commands. And I cannot over-ride it by running
:map <Leader>lx :<C-U>call SetXeLaTeX()<CR>
command in Vim's current session.
Can anybody tell me what is exactly wrong? How to fix this oddity?
Update
Strangely enough, I discovered that if I remove the <C-U> key in custom functions, no misbehavior is encountered. After this tweak, the \lx command will work as expected.
You should use :noremap; it makes the mapping immune to remapping and recursion.
Apparently, Voom defines a <C-u> mapping, and your :map commands use that, and it's wreaking havoc on them.

Combine two vim commands into one

I'm having trouble combining two vim commands, specifically <C-w>s and <leader>x into <leader>r (i.e. split window and open spec counterpart of current file). Any help?
Thanks!
It would help if you'd post what exactly you've tried that didn't work. Generally, doing what you describe should be simple. It should be enough to put this in your .vimrc file:
nmap <leader>r <c-w>s<leader>x
This maps <leader>r to expand to the key sequence <c-w>s<leader>x. Note that these are not "commands", as you call them in your question, they're "mappings". A "command" is something completely different in vim, you can read up on that with :help user-commands.
One thing to be careful of is using nmap instead of nnoremap. The command nmap maps the sequence on the left to the sequence on the right while re-using mappings that have already been defined. On the other hand, nnoremap creates a mapping with the original meanings of the keys, so in your case won't work (since <leader>x is defined by some plugin). This is one possible reason you may have failed while trying to do it, but I can't tell from your question.

Vim - how to store and execute commonly used commands?

If I wanted to process a batch of text files with the same set of commands for example:
:set tw=50
gggqG
Can I save the above and run it with a shortcut command?
If you want to use it only once, use a macro as specified in some of the other answers. If you want to do it more often, you can include the following line in your .vimrc file:
:map \r :set tw=50<CR>gggqG
This will map \r to cause your two lines to be executed whenever you press \r. Of course you can also choose a different shortcut, like <C-R> (Ctrl+R) or <F12> or something.
The following in .vimrc will define a new command Wrap that does what you want.
command! Wrap :set tw=50 | :normal gggqG
Call it with :Wrap
As a very quick start, put this in your .vimrc:
" define the function
" '!' means override function if already defined
" always use uppercase names for your functions
function! DoSomething()
:set tw=50
gggqG
endfunction
" map a keystroke (e.g. F12) in normal mode to call your function
nmap <F12> :call DoSomething()<CR>
note: the formatted code above looks rather horrible, but lines starting with " are comments.
Other than macros, you can use argdo. This command will perform the same operation on all open files. Here is how you format all open files using :argdo and :normal:
shell> vim *.txt
:argdo exe "normal gggqG"|up
Before you go for writing a thousands-lines .vimrc (which is a good thing, but you can postpone it for a while), I think you might want to look at the plain recording, in particular you may consider using the qx (where x is any key) for recording, q to finish recording and #x to execute recorded macro.
Yes, the important word is macro
But it seems like a 'command' such as :set tw=50 would be better included in the .vimrc file so vim uses it every time you start it up.

Resources