How can I run a normal command after scripts load? - vim

I'm trying to start vim from command line and have it jump to a certain place in the file and run <c-c>g to trigger goto_definition in python-mode.
So far running vim filename "+call cursor(x, y)" does at least get me to the position I want, but now how do I run <c-c>g afterwards?
If I try vim % "+call cursor(x, y)" -c "normal! <c-c>g" I get a "not an editor command" error

Try
vim FILENAME -c "call cursor(x, y)" -c "call pymode#rope#goto_definition()"
<c-c>g is actually bind to function pymode#rope#goto_definition().
You can look up in :map to see the mapping.
Refer to here if you're interested in why not an editor command error occured.

Related

Running arbitrary vim commands from bash command line to script vim

I want to script vim to edit files from the command line. For example I want to do something along the lines of:
vim -<SOME_OPTION> 'Iworld<Esc>bIhello <Esc>:wq helloworld.txt<CR>'
or:
echo 'Iworld<Esc>bIhello <Esc>:wq helloworld.txt<CR>' | vim
and have it save the file helloworld.txt with a body of hello world
Is this possible? I've tried a few different approaches but none seem to do it. I realize I can do things like vim +PluginInstall to run Ex commands from the command line, but I'd love to be able to string together arbitrary motions
This can be achieved with the + flag and the :normal command:
$ vim +"norm Iworld" +"norm Ihello " +"wq helloworld.txt"
I think what you are looking for is vim's -w/W and -s {scriptin} option. Well in your case you should make a scriptfile, and with -s file to let vim execute all your "key presses"
I think vimgolf has used these options too.

Execute external command in the same screen

When executing :silent !command (followed by :redraw!) the screen goes to the terminal, then it come back to vim.
How can I make it execute in the same screen, without this "blink"?
If I understand you correctly you want to just execute some command, but don't want to see the output. If so, then just use the function system
:call system("g++ myfile.cpp -o output")
or whatever command you want to do. However, I would recommend just putting the output in a seperate window, you can use the QuickRun Plugin and the just run:
:QuickRun bash -src "g++ myfile.cpp -o output"
and the result will be in a separate window.

Why does setreg not work in Vim's custom command as expected?

This question is actually related to a previous question of mine.
I have the following custom command in _vimrc:
command! Test %s/some/\=setreg('C', submatch(0), 'l')/g
I run this custom command Test on the following text:
something
After running the command the text becomes:
=setreg('C', submatch(0), 'l')thing
But if I run the same command %s/some/\=setreg('C', submatch(0), 'l')/g from the command line of Vim, I get:
0thing
When running the custom command Test the register C is not changed. But it is changed when I run the above command from command line which is as expected.
I don't know exactly what's bugging you, as with the last time, both of your commands work fine for me.
Usually this is related to some non-default setting, often the 'cpoptions'. Try with :set cpo&, or after vim -N -u NONE. Also, check whether the backslash is in the command output of :command Test.
Finally (should you not find the root cause), you can work around it by using an intermediate function:
function! Test()
%s/some/\=setreg('C', submatch(0), 'l')/g
endfunction
command! Test call Test()

text editor mode for calling bash command

suppose I want to enter a multiline command via bash I know that I can append \ in the end of the line to enter a new line
however is it possible to enter a legitimate "text editor mode" where you don't even have to enter \ and simply press enter would suffice
eg..you type in the command into the command line then before entering the parameters you press some magic button which allows you to enter a vi like mode then you enter stuff into the "vi mode" then you exit and then the text you entered in the "vi mode" turns into the parameters of the command then you press enter then the command executes
is it possible to do that in bash command line? if so, how do I do it?
See man bash:
edit-and-execute-command (C-xC-e)
Invoke an editor on the current command line, and execute the
result as shell commands. Bash attempts to invoke $VISUAL,
$EDITOR, and emacs as the editor, in that order.
Per default bash is configured for emacs mode, hence the emacs like C-xC-e command.
If you really like vi you can also set your bash into vi mode: set -o vi. This allows you to do normal line editing the vi way without invoking an explicit editor.
Bash can emulate vim mode (though not very well) with:
set -o vi
You can edit the previous command in vi or your default editor by using the fc command. This pops open an editor window and when you exit it executes the edited command. That mode could bed used repeatedly to edit a complex command.

Calling command from `-c` command line argument doesn't work in Vim

I'm using the Fugitive plugin.
It has a command(?) Git! which executes a command and opens the result in a new buffer.
Example:
:Git! diff --cached
I have a function which calls this, and does some other things after that.
And I have this command declaration:
command! Hello execute ":Git! diff"
If I run :Hello from within vim, it works as it should. But when I run vim -c Hello, it throws this error:
Not an editor command :Git! diff
How can I do this?
(PS: How can I make this error message to stay until I press ? It appears for about a second and disappears.)
The reason is that Fugitive only defines its commands for buffers whose files are under Git version control. Precisely, the code in plugin/fugitive.vim only sets up autocmds that detect files under Git control, and only then defines buffer-local commands.
So at least you need to pass a Git-controlled file to your Vim invocation. If that still doesn't work, try explicitly triggering the detection via
:doautocmd User Fugitive

Resources