How to execute vim internal global commands with this setup? - vim

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.

Related

Use a user defined vim command inside another user defined vim command

I am trying to have to custom commands in vim as follows:
The first command just runs a bash script. this works fine on its own.
:command Build :!./build-linux.sh
Now I want to use this command in another such as this:
:command BuildAndDebug :Build | :call vimspector#Launch()
This command is supposed to run the bash script and then launch a debugger.
When I try to do this, I get this error back:
E488: Trailing characters: :Build | :call vimspector#Launch()
I can't seem to find the trailing characters here. I suspect the :Build is the cause of this because it does not know where the command ends. However I can't seem to get it to work at all.
You need to add the -bar attribute to your command definition if you want to be able to "chain" other commands after it with |:
:command -bar Build :!./build-linux.sh
This is described in chapter 40 of the user manual: :help usr_40, and documented under :help :command-bar.

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

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.

vim nerd commenter range command

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.

Vim - run ant command with current file as an argument

In my IDE (webstorm) I have configured an external tool to run ant like so:
ant myTarget -DfileNoExt="$FileNameWithoutExtension$"
the $FileNameWithoutExtension$ is expanded by the IDE when the command is run.
I am transitioning to using Vim. I am a Vim n00b.
How do I do the same with Vim (MacVim specifically)?
Vim will expand % as the current file. You can use modifiers on it (see :help filename-modifiers). You can tell vim to use ant as your make program:
compiler ant
setlocal makeprg=ant\ myTarget\ -DfileNoExt=\"%:t:r\"
Now you can use :make to build your current file. You should get build errors in your quickfix (view it with :copen).
You probably want to put the above script into a file ~/.vim/after/ftplugin/java.vim. That will load it for every java file you open.
Note that if you want to use different targets, :make will pass all arguments to ant. So :make otherTarget will execute ant myTarget -DfileNoExt="file" otherTarget.
you can map either Hotkey or Your own key-sequence in vim.rc(not sure in mac, should have similar resource file)
add this line to your vim resource file:
imap <F5> <ESC><ESC>:!ant mytarget -Dfilenoext=\"%<\"
which means when you editing the source code, press F5 will run your ant tool
for more, you can read http://vim.wikia.com/wiki/Compile_Java_with_Sun_JDK_javac and http://www.vim.org/scripts/script.php?script_id=155

Resources