Short and simple example
How can I use shell like command substitution in the vim command-line mode?
For example, if I want to edit a particular path whose path is available as output of another command, say, which foo, I could do something like this on the shell.
vim "$(which foo)"
How can I do this with the :e command when I am already within Vim?
I am looking for something that is the equivalent of :e $(which foo.txt).
Real use case
The above is just a short example so that this question makes sense for everyone. Here is the real reason why I am looking for this answer.
In the shell, I can execute gdb -p $(pidof bar) to attach to a process bar and debug it. With Vim, I am using ConqueGdb which accepts the same arguments as gdb, so I would want to do something like :ConqueGdb -p $(pidof bar) from within Vim.
:e `which foo`
:help backtick-expansion
Related
By reading this question I learned that you can give -c to vim such that it will call a command. The question is then answered by calling vim -c startinsert. Good, but what if I want to give other commands? I only know the shortcuts like i for :startinsert.
For example I'd like to write vim -c gotoend but it doesn't exists.
After some reading, all : commands, which are ex commands are written here and in man vim, but where do I find a lis or method to know what can be done?
The commands for options -c are called ex-command named after ex(tendended line editor). The list of commands are in vimdoc and vimhelp.
The command to jump to the end of file is just :$ so you can do this in command line:
vim -c $ <file>
Please note you probably better escape or quote $ as it's a shell metacharacter:
vim -c \$ <file>
or
vim -c '$' <file>
:help :normal. Also note that instead of -c command you can use +command.
With that, I'd typically say vim +"norm G" foo.txt to start at the bottom. You could even alias it: alias vimbottom='vim +"norm G"'; then you can use vimbottom foo.txt to start at the bottom, if you prefer.
where do I find a lis or method to know what can be done
There is no better resource for Vim than its :help. And I don't know any place that has all the ex-commands in one place, since it would be a huge list. (EDIT: :help :index actually has that. Thanks, Christian Brabandt!)
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.
Vim newbie here (has worked with zsh for a few months now). I think I install too many dotfiles or have a wrong configuration because whenever I attempt an external command (even thing such as :!rm TEST, Vim exits with a message zsh: suspended (tty output) vim .. How do I fix this?
The dotfiles: https://github.com/daryllxd/dotfiles. (I got them from someone else).
Try to comment this out:
set shellcmdflag=-ci
If this doesn't work for you look at the following.
From the manual:
Commands are first read from /etc/zshenv; this cannot be overridden.
[...]
Commands are then read from $ZDOTDIR/.zshenv. If the shell is a
login shell, commands are read from /etc/zprofile and then
$ZDOTDIR/.zprofile. Then, if the shell is interactive,
commands are read from /etc/zshrc and then $ZDOTDIR/.zshrc. Finally,
if the shell is a login shell, /etc/zlogin and $ZDOTDIR/.zlogin are
read.
From what I understand,
set shell=zsh\ -i
should work.
if u want back to the vim by suspended,u could use fg,just like this:
fg %vim\ [u viming file]
this title maybe help u:http://ytliu.info/blog/2013/09/28/ttyde-na-xie-shi-er/
In vim, type :sh will switch to shell, and exit can exit the shell and get back to vim. Is there any command to check if it's in vim shell mode? So that I won't accidentally vim to open the same file again. I want to avoid below scenario:
vim myfile > :sh > exit > vim myfile // get warning of another vim instance is editing the same file
These are the normal scenario:
vim myfile > :sh > exit // keep editing
vim myfile > :wq > vim myfile // keep editing
In addition to #a3nm answer, what you can do is
use pstree -h: it will output process tree with current branch highligted and all you need to check is whether there is vim in the highlight.
Another possibility is ps t: it will show all processes using the current terminal and should show vim in a list when you are inside :sh. ps -oargs t may be more useful in case you want to know arguments you ran vim with.
These methods are more reliable because VIMRUNTIME, VIM and MYVIMRC environment variables may be overrided by you in order to do some customizations (BTW, they are defined by vim for use in vimscripts, not by :sh). They also work for other processes that allow you to run a subshell, but do not define any environment variables.
I would also suggest to consider using <C-z> in normal mode or :suspend/:stop in Ex because these use shell vim was launched from instead of creating new. This behavior gives you access to history of commands you typed before running vim and also makes you able to write more complex and time-consuming shell configuration without needing to wait every time.
In case you use <C-z> both methods still work, but first method won’t highlight vim because it will be at the same level (has the same parent) as pstree itself, likely just below or above pstree in graph. This also enables third method: jobs shell builtin.
In order to restore from <C-z> you should use fg (a single % in zsh and bash also works) which is less to type then exit (but more then <C-d>).
The :sh command in vim seems to define the VIMRUNTIME, VIM and MYVIMRC environment variables, so you can just check if they are defined. To do so, you can run echo $VIM, for instance, which should return an empty line in a normal shell and something like /usr/share/vim in a shell run from vim.
I want to run command like this:
vim -c "%g/blablabla/norm /str<ESC>cwSTR" file
How I write escape character in the command?
As you type the command, use control-v then escape to enter the escape.
However, I have to question whether vim is the right tool for this job. Normally, you would be better off with something like sed. That said, I'm not quite clear what the vim command is up to, so maybe you do need it.
This is what I would do:
vim -s -c ':exec "normal %g/blablabla/norm /str\<Esc>cwSTR"' file
Notice that I am using :exec to expand the "\" to the real . The advantage? it is more script friendly. However, I am not sure what this command is doing, are you using some of your custom maps here?