We’re rewarding the question askers & reputations are being recalculated! Read more.

Hot answers tagged

37

:!{cmd} sends {cmd} to the shell which executes it and shows its output on the screen. :{range}!{filter} sends the lines from the current buffer inside {range} as the input of the {filter} program and replaces them with its output. In your example, :%!jq : % is the range, which means: all the lines of the current buffer It could also be written 1,$ (from ...


35

I wrote a pretty extensive answer about this over on stack-overflow. The basic idea is that since the write command is about writing not saving, you can write the text in your buffer into an external program, such as python or bash. In your case, you would want to do something like: :w !bash or :w !sh These commands literally just write the text in your ...


25

One has to realize that the feature implemented by airline are inspired by the powerline plugin. Furthermore, I was relying on the docs too much, not realizing that there is well-written airline help document shipped with itself: :h airline Reading its documentation helps understanding the functionality of this plugin. Font As mentioned in the powerline ...


12

By sheer luck I found out that both: :%!jq . % or shorter: :%!jq . work, yet I don't understand why this updates the buffer.


12

You should add the following two lines at the end of /etc/vim/vimrc : set mouse= set ttymouse=


12

There certainly is Vim style searching in bash/readline. In fact, your command line is essentially a scaled down version of the Vim editor with a significant number of Normal mode commands available to you. More on that in a sec but first Search. The key piece of information you need is that as you stare at that blinking cursor on your command line you are ...


10

You can use autocommands to run things automatically when certain events occur. In your case, you could do this: autocmd BufWritePost *.less !less <afile> This autocommand runs every time a buffer with a name that ends in .less is written. The command that is being run is a bash command, and <afile> is the name of the file that is being ...


10

Your issue is with bash and not with vim. The problem is that when you run !<cmd> in vim or /bin/bash -c <cmd> from another shell you are launching an instance of bash in non-interactive mode. In non-interactive mode alias are not expanded and you get this error. I don't know if you'd be able to enable the shell option expand_aliases from these ...


10

You have a couple of options here: Using a plugin: vim-g gsearch vim-quicklink Or, if you prefer a lightweight solution, you can try the following: function! GoogleSearch() let searchterm = getreg("g") silent! exec "silent! !firefox \"http://google.com/search?q=" . searchterm . "\" &" endfunction vnoremap <F6> "gy<Esc>:call ...


10

That happens, because usually Vim switches to the alternate screen and works there. Upon exiting, Vim will switch back and therefore you don't see anything left from your Vim session, but the result of the command executed even before starting Vim. This feature is explained at :h xterm-screens (link) So you basically don't want that and therefore want to ...


9

Why doesn't this work? This doesn't work for exactly why you say, vim is replacing the % with the path. So, your non working line :! mv % ${%/.txt/.asc} is expanding to :! mv myfile.txt ${myfile.txt/.txt/.asc} which bash will call a bad substitution; You are trying to expand a variable called myfile.txt, and bash doesn't support variable names with . ...


8

Instead of adding -i to shellcmdflags and reading your entire .bashrc just for loading aliases, keep your aliases in a separate file, like ~/.bash_aliases for Ubuntu, and add to your vimrc: let $BASH_ENV = "~/.bash_aliases" From the bash manual, Invoked non-interactively When Bash is started non-interactively, to run a shell script, for example, ...


7

As documented in :help 'smartindent' When typing '#' as the first character in a new line, the indent for that line is removed, the '#' is put in the first column. The indent is restored for the next line. If you don't want this, use this mapping: ":inoremap # X^H#", where ^H is entered with CTRL-V CTRL-H. That is, 'smartindent' causes this behavior ...


7

You can use :execute {expr1} to execute a string from evaluation of {expr1} as an Ex command. :exec "!tmux set-buffer" getreg("0") will first combine the string !tmux set-buffer with the results of getreg("0"), adding a space in between, and then execute the resulting command. Presumably you want this only in normal mode, so it is advisable to use nnoremap ...


7

I think the problem comes from the second bang in your command which has a special meaning for vim. It is automatically replaced with the previous external command. Any '!' in {cmd} is replaced with the previous external command (see also 'cpoptions'). But not when there is a backslash before the '!', then that ...


7

&path has nothing to do with $PATH. Also $PATH is set and used. See :echo $PATH. You should also be able to change its value with :let $PATH = $PATH.':some/path' -- there are a few write only things, $PATH is not one of them IIRC


6

You can use: :set t_Co=0 This will tell Vim that you're not using a colour terminal. The difference with using :syntax off is that this will still enable some syntax highlighting features with bold, underlined, and "reverse video".


6

Your test is flawed. If you run !!bash, you're calling just the bash command explicitly. Why would you expect bash to be interactive in that case? !! is shorthand for :.!, so you're running :.!bash, which would become something like: bash -i -c 'bash'. So the first bash might have extglob set, but the second won't. Run :!echo $- for a better test: himBHc ...


5

You can use: vim +'normal! 2GI;' +'x' path/to/your/file The + parameter allows to execute a command after opening the buffer. The first command normal! 2GI; goes to line 2 and add a ; at the beginning of the line The second command saves and exit. Bonus point: To uncomment the same line: vim +'normal! 2G^x' +'x' path/to/your/file


5

You can set or modify an environment variable in Vim like so: :let $MY_ENV = 'coconuts!' Child processes inherit the parent process's environment, so this is available when you start a new shell process with :sh (which is a child of the vim process: :sh $ echo $MY_ENV coconuts! It also works with NeoVim's :terminal: :terminal $ echo $MY_ENV coconuts! ...


5

Just discovered this after viewing /etc/vim/vimrc for the first time. Uncommenting the line "set background=dark" in vimrc solves my readability problem also. It changes the syntax colors so that they are distinguishable against my black background! This is actually better than what I had hoped for in the original question! (Presumably :set background=dark ...


5

The built-in strftime() does exactly what you want: <C-r>=strftime('%y-%m-%d %H:%M:%S') To turn that into a mapping: inoremap <expr> <key> strftime('%y-%m-%d %H:%M:%S') You can of course use whatever external command you need but it is important to keep in mind that the output of Unix commands usually ends with a newline character that ...


5

As @chicks pointed out, you can place this script in your PATH: #!/usr/bin/env bash if [[ "$#" -ne 0 ]] || [[ -t 0 ]]; then exec vim "$@" else exec vim - fi I am using a variant of this script in ~/bin/e to open the current directory (vim .) when no argument is provided. I guess you could also use a shell function: v() { if [[ "$#" -ne 0 ]] || [[...


4

No it isn't possible. Vim will be the child of your bash process. A child can't change the current directory of its parent (except by doing tricky and very discouraged things: https://stackoverflow.com/questions/2375003/how-do-i-set-the-working-directory-of-the-parent-process). You may also want to read: https://unix.stackexchange.com/questions/141313/...


4

Quoting from the help (:h E34) Any '!' in {cmd} is replaced with the previous external command And further in the help (:h :bar) These commands see the '|' as their argument, and can therefore not be followed by another Vim command: [various other] :[range]! [...] To be able to use another command anyway, use the ":execute" ...


4

Scenario: you are working in bash shell. First, make sure the local edit mode is vi: $ set -o vi Now, when you find yourself working on a long command like the following, hit Esc+v. $ fjhwfq this is my long command This will load your command into a temporary vi session. Now you can edit it as you wish, and even add another command... fjhwfq some long ...


4

External commands are not executed in an interactive shell so your .bash_profile is not read. To make your custom command available you must turn it into an actual shell script, put it in your $PATH, and make it executable: #!/usr/bin/env bash echo "Hello world!" But there's a much better way to "compile something": :help :make.


4

To avoid screen flashes when editing stream non-interactively, you need to start Vim in Ex mode by adding -e (Ex mode) or -E (improved Ex mode) into your command-line arguments. Here is the simple example editing text from the standard output and printing the result into standard output: $ echo "foo bar" | vim -E +%s/foo.// +%p -cq! /dev/stdin bar To ...


4

I often just prepend the command with #. After exiting vim it's then in the bash history.


4

An awkward solution Copy content into a register from an editing window and paste in to the :terminal window with <CTRL-W> " {reg} to run it. Example with one editing buffer and one :terminal buffer running bash: text content in editing window echo hello cmd to copy the content to the 0 register :vipy move to the terminal window <CTRL-W&...


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