Hot answers tagged

32

You have to use - in place of the file name on the command line: echo This is example. | vim - The above command will open an unnamed buffer filled with the text read from the standard input.


26

This happens when vim is invoked and it's connected to the previous pipeline's output, instead of the terminal and it's receiving different unexpected input (like NULs). The same happens when you run: vim < /dev/null, so reset command in this case helps. This is explained well by grawity at superuser. If you're using find to pass file names to edit, you ...


19

The problem is how Vim writes files. By default, it makes a copy of the file and overwrite the original one. You can observe this with: # Show the file's inode $ ls -i a 3156153 a # Open file in Vim, :wq # inode changed! $ ls -i a 3155322 a crontab gets confused by this (see :help crontab) You need to use :set backupcopy to yes to make Vim overwrite ...


12

Workaround suggestion: use a buffer as a filesystem navigator Use the vim - command to read a list of paths from stdin. Vim's :help -- explains this:1 Start editing a new buffer, which is filled with text that is read from stdin. The commands that would normally be read from stdin will now be read from stderr. Example: find . -name "*.c" -print | ...


10

While I mostly agree with @romainl comment (markdown was made to be explicit enough not to need a preview) you can do this in different ways: [OSX / Unix] The instant-markdown plugin is a solution. You need to have node.js installed and to use the following command: [sudo] npm -g install instant-markdown-d You also need to have the packages xdg-utils, ...


9

To print buffer to shell standard output, vim needs to start in Ex mode, otherwise it'll open "normal" way with its own window and clear any output buffers on quit. Here is the simplest working example: $ echo foo | vim -e '+%print' '+q!' /dev/stdin foo or even shorter: $ echo foo | ex +%p -cq! /dev/stdin $ echo foo | ex +"%p|q!" /dev/stdin Note: The ...


8

Besides reset, you can try: stty sane which should also make your terminal usable again. See here for explanations. And somehow this can be considered a vim misbehavior, at least Neovim doesn't have this issue at the moment.


7

This is just mostly a fun note about bash and readline, but if you set your EDITOR environment variable to vim, hitting Ctrl+x+e will open up the current line in vim. Further, if you type Meta(Alt/Opt/Esc)+Ctrl+e, bash will perform an in place expansion on the current command line, i.e.: $ echo $EDITOR # Assuming EDITOR was set with Meta(Alt/Opt/Esc)+Ctrl+...


6

When you run vim using su or sudo, I believe it's running the root user's .vimrc (if there is one) instead of yours. I think the most "correct" way to resolve this is to use sudoedit rather than running vim directly as the super user. This answer should get you going with that: https://unix.stackexchange.com/questions/46699/start-vim-as-my-user-with-root-...


5

As <C-z> will suspend Vim itself, not just the launched shell command, that won't work. What I would do is abort the long-running command with <C-c>, then relaunch the same command in the background via :!! & (The :!! command comes handy for recalling the previous external command here; you could also use the Ex command history via <Up&...


5

Move the special filename - before -e: cat test.md | vim -e -c'g/foo/p' -cq -s - ^ ✘ cat test.md | vim - -e -c'g/foo/p' -cq -s ^ ✔ Before -e, - is read as literal text. After -e, - is read as an Ex command. To suppress the Vim: Reading from ...


5

I understand that you are set up with the features necessary for copy and pasting with the system clipboard, but I want to be a tad redundant as those registers can be a tad challenging to set up. It's all about the features that are enabled with your install of Vim. In OS X and Linux, the clipboard feature needs to be enabled, and Linux usually also ...


3

vipe is a convenient tool for editing pipelines, part of the moreutils package. It is a wrapper for editors (including the vi which gives it its name). It uses the EDITOR environment variable to set the editor, so, like with other tools that make use of EDITOR (such as crontab, visudo, etc.), you can script actions using that variable. For example, you can ...


3

I tried to use backtick expansion, but that does not seem to be working on Windows. So I think the easiest solution is something like this: :exe ":e" $ABC."foobar.txt"


3

tpope/vim-eunuch Like you've discussed, $SUDO_COMMAND can be used to find the original filename(s). Together with a nifty usage of fnameescape and doautocmd it is possible to deduce the correct filetype. This is what tpope/vim-eunuch does: https://github.com/tpope/vim-eunuch/commit/4087a5463ff1d9e779dace6fddbdad10e072c1ea If filetype detection is all ...


3

In bash I've found it useful to use process substitution using the <(command) syntax, in example: vim <(echo This is example.) vim <(cat /etc/hosts) See also: How to edit files non-interactively (e.g. in pipeline)? at Vi SE How to pipe the result of a grep search into a new vi file at unix SE


3

The reason is that xargs sets stdin to /dev/null, whereas vim needs stdin to be /dev/tty. BSD xargs (e.g. Mac) solution: echo -e 'file1\nfile2' | xargs -o vim -o sets the stdin of xarg's child process (vim in this case) to dev/tty. GNU xargs (e.g. Linux) solution: GNU xargs does not have the -o option. Instead you will have to use a more complicated ...


2

@Martin's self-answer is correct. A variation though, for people like myself who try to limit populating their vimrc file with occasional exceptions -- here editing a crontab, is to use modelines. This allows to place the exceptions directly in the target file. Below is my crontab modeline: # vim: nu et tw=130 ts=8 sts=4 sw=4 ff=unix fo-=l fo+=tcroq2 bkc=...


2

As any child process inherits the environment from its parent, you can temporarily redefine Vim's own environment before executing your tool: let [temp, $ENV_VAR] = [$ENV_VAR, ''] %!cmd let $ENV_VAR = temp


2

I'm not sure about csh specifically, but for sh, ksh, and Bash, set -o vi, and for Zsh I think bindkey -v. You can add that to your ~/.profile or whatever. You can also add this to your ~/inputrc: set editing-mode vi set keymap vi-command for all programs that use Readline (including Bash).


1

timtowtdi As pointed out in the comments to the other answer you could also solve this with editing root's ~/.vimrc file. There are other options though. symlink If your personal ~/.vimrc doesn't have any plugin weirdness or hard-coded directories you could just symlink root's ~/.vimrc to your personal one. That way any other settings or future changes ...


1

<esc>:q! To close the file without saving. <esc>:wq To close the file with saving. Those are the first two commands that I teach people who need to use VIM in a pinch. They are useful, and provide a base for understanding the use of the <esc> key and : commands. As Christian mentioned in his comment, VimTutor is probably the best way to ...


1

You can put the command in a shell script that backgrounds itself. The contents, for example, might be: #!/bin/sh sleep 100 &


1

Note: This answer only seems to work with the tcsh and fish shells. I also tried bash, dash, mksh, and zsh, and it doesn't work there; I'm not sure why, because if I do the same actions from these shells without Vim, it does work as expected... (:! commands are executed though the shell). I happen to use tcsh, so it works for me... You can use :set shell=/...


1

Normally when a process is started, you don't have much control on it other than terminating it (Ctrl+c) or suspending it (Ctrl+z, but including the parent process). However depending on the process type and operating system, here are few tricks that you can try: Sending SIGTSTP or SIGSTOP will stop the process (the same signal sent by Ctrl+z), and SIGCONT ...


1

Not the same error but also happening quite often: Error detected while processing /root/.vimrc: line 2: E319: Sorry, the command is not available in this version: syntax enable line 7: ... This error will show up, if you use vi instead of vim for crontab. You need to set the editor of crontab by adding the following code in your ~/.bashrc file: ...


Only top voted, non community-wiki answers of a minimum length are eligible