vim nerd commenter range command - vim

I found similar topic, but I want to do something different. When using nerd command, in normal mode You can do:
5ggv12gg,c<space>
It will toggle comment from line 5 to 12. But instead I would like to be able to write:
:5,12Ct
So I tried to write my own command for that using predefined nerd commenter command:
command! -nargs=? -range=% Ct <line1>ggv<line2>gg<Leader>c<space>
but as I assumed it gives me an error "not an editor command: 5ggv12gg,c " - probably because it expects command like :something, not normal mode command.
Is there a way to achieve something like this?

You can use :normal, as in :normal <line1>ggv<line2>gg<Leader>c<space>. See :help normal for details.

Related

How to move to end of a file upon opening it via a command in .vimrc using vim/MacVim?

I'm trying to open a file using a command I set in my .vimrc file. The relevant line in my .vimrc is similar to the following:
command Of so /Users/Dude/Working/open_file.txt
With open_file.txt containing the following:
tabnew /Users/Dude/Working/Project/config.txt
What I'd like to do when executing the 'Of' command is navigate to the end of config.txt. I've tried adding a large line number which is unlikely to exceed the number of lines in the file like so:
tabnew /Users/Dude/Working/Project/config.txt
250000
This takes me to the end of the file but doesn't seem like the right way to do it. Ideally, I'd also like to add a new line after the last line and navigate there too.
A few things:
I would suggest you use full names instead of short names. e.g. so -> source.
source is probably the wrong choice here as you can do everything with the right-hand-side of command
May want to use ! with command so you can resource your vimrc file. e.g. command! Of ...
$ represents the last line of the file. No need to choose a magic number
Create a new line can be done with :normal o or :put _
So with some tweaks we get the following command:
command! Of tabedit /Users/Dude/Working/Project/config.txt | $put_
For more help see:
:h :command
:h :put
:h :range
:h :bar
Have a look at :h :normal in your case just write :norm Go instead of your number there.
:tabnew, like most variants of :edit (and the command-line arguments when launching Vim), takes arbitrary Ex commands via the [+cmd] argument. The $ command will move to the end of the file:
tabnew +$ /Users/Dude/Working/Project/config.txt

How to execute vim internal global commands with this setup?

I have something like the below in my .vimrc on MacOSx El Capitan.
let g:plantuml_executable_script='java -jar /Users/megan/Downloads/plantuml.jar'
To be sure Java is available I typed which java on the command line and I got /usr/bin/java, so I have that added to $PATH.
Now, when I do the following in Vim by opening up a plantuml file:
:! java -jar /Users/megan/Downloads/plantuml.jar
I get a nice class diagram image generated.
What could I do in Vim so that I can type plantuml_executable_script instead and get the nice diagram?
I think you're referring to PlantUML (please let me know if I'm wrong). If it's so this webpage can get you started.
Let's say you're editing a file called sequenceDiagram.txt which has the following lines:
#startuml
Alice -> Bob: test
#enduml
and then you save it with :w command. After that you can execute:
:!java -jar /Users/megan/Documents/plantuml.jar %
to get a file called sequenceDiagram.png. Note that Vim will replace % at the end of the command line with the file name you're editing.
Now, if you want a command called plantuml_executable_script to execute :!java -jar /Users/megan/Documents/plantuml.jar %, you can create it by executing:
:command! PlantUMLExecutableScript !java -jar /Users/megan/Documents/plantuml.jar %
Please note:
An uppercaseP was used because user-defined commands must start with a capital letter.
Underscores cannot be used.
See :help 40.2 and :help user-commands for more information.
In that way you can execute :PlantUMLExecutableScript to run PlantUML. But you can go one better by creating a mapping:
:noremap <F2> :PlantUMLExecutableScript<CR>
So you can hit F2 (or whatever key you choose) to call :PlantUMLExecutableScript. See :help 05.3, :help 40.1 and :help key-mapping for more information.

VIM mapping an internal command

I can't figure out how to map an internal command in vim.
I want to map to the command :Indent, the action g=GG (indenting the whole document)
I did this :
:command Indent execute "g=GG"
And it doesn't seem to work.
I successfully mapped
:command Java execute ":!javac *.java; echo ' **** done **** ' "
but how do i make it compile only the file that i am working on.
gg=G is a normal mode command. You need to use :normal, here:
:command! Indent normal! gg=G
But… :Indent<CR> is much longer than gg=G so I'm not sure that's a good idea.
I don't understand the need either. Still, if you want a command, at least let's have it support ranges:
:command! -range=% -nargs=0 Indent <line1>,<line2>normal! ==

Run command from within vi / vim

As part of learning Haskell, for fun I'm attempting to use Raspberry PI. Having encountered a myriad of issues installing ghci on the PI I've resolved to using just ghc.
So to create, compile & run a new Haskell file :
vi first.hs
i
main = putStrLn "First"
Esc
:w
:q
ghc -o first first.hs
./first
Output is : "First"
I would like to automate the commands :
Esc
:w
:q
ghc -o first first.hs
./first
Can these be added as new command from within vi / vim, something like :
:mycustomcommands
And run from within the vi / vim editor ?
Maybe you could try adding something like this to your vimrc:
function! ExecuteHS()
w
!ghc -o first %
!./first
endfunction
And to use this function you just have to call it like that :call ExecuteHS(). Vim will be put on background during the execution of your file and will then come back on foreground at the end of the execution.
As a bonus you can add the folowing line to your vimrc
nnoremap <key> :call ExecuteHS()<CR>
Replacing <key> with your prefered key combination <Leader>e for example. This way you'll simply have to hit ,e (if you didn't changed your leader key) in normal mode to call the function.
That's probably not the cleanest way to do it but it should work for what you want.
Absolutely in vim, though not necessarily in other vi flavors. See this tutorial on defining custom commands. Put the custom command in your vimrc and it will always be available as :Customcmd or whatever you call it. For one-button access, you can use :remap to assign a hotkey to your custom command or the sequence of built-in commands you want to run. This is a tutorial on keymappings that will give you more information.
I second #statox's referral to https://vi.stackexchange.com :)
I use vim-haskell, which includes a couple nice things. In particular, it includes a file for setting up cabal-install as the compiler, which is a very nice way of working. Dump this in ~/.vim/compiler/cabal-build.vim:
CompilerSet makeprg=cabal\ build
CompilerSet errorformat=
\%W%f:%l:%c:\ Warning:%m,
\%W%f:%l:%c:\ Warning:,
\%E%f:%l:%c:%m,
\%E%f:%l:%c:,
\%C\ \ %#%m,
\%-G%.%#,
\%-G%.%#
And this in ~/.vim/ftplugin/haskell.vim:
compiler cabal-build
(The argument to compiler should match the name of the file you put in ~/.vim/compiler.) Then you can run :make in vim and it will save any changed buffers (assuming autowrite is set) and build your project. When there are errors, it will populate the quick-fix list, which lets you jump to the specific file and line numbers of each error or warning with a key. Read more about this feature with :help quickfix. Once everything is working, you can :!cabal run to run it.

Re-execute command by :history option in VIM

How can you execute a command again listed in the
:history
option in vim. There are numbers shown. Is the only way to copy the command by hand, then re-enter it? Or is there something like in shell script.
:history is only there for you to look at it.
To re-execute a previous command, you have two options:
Use <Up> and <Down> at the command prompt:
:m10
(do stuff)
:<Up>
Use the "command-line window":
You can call it with q: and navigate with search and use the beautiful normal mode commands we all love.
Position the cursor on a line and hit <CR> to re-execute the command.
Edit a command and hit <CR> to execute the new command.
You can quit the command-line window with :q.
See :help cmdline-window.
I use q: shortcut in normal mode to open interactive command history window. You just move to the right command and execute it by pressing Enter. You can find more information and other ways of accessing history here.
What I like to do is type the first few characters in the command and press <UP>. For example if you want to repeat an edit command of the file file.txt you could:
:e fil<UP><ENTER>
If the first <UP> does not give you the command you want, keep pressing<UP> until you find the command you were looking for.
If Vim is compiled with +eval you can use histget( {history} [, {index}])
:exe histget('c', 15)
That isn't exactly convenient to type, so you can also create a user-defined command:
:com -nargs=1 HI exe histget('c', <args>)
Thereafter you can use HI {index} to execute the history entry:
:HI 15

Resources