Hot answers tagged

43

:!{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 ...


41

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 ...


26

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 ...


19

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


16

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


15

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 ...


11

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 ...


11

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

! has to be escaped which can be done with a backslash \!. 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 ...


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 ...


6

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! ...


6

You could try it like this: vim -c "lefta vsplit file2.txt|split file1.txt" file3.txt With the option -c you can specify commands that should be executed after the first file was loaded. So here: The file file3.txt is loaded. The command lefta vsplit file2.txt|split file1.txt is executed. This are in fact two commandsseparated by | lefta vsplit ...


6

Actually, you don’t need saved sessions for this; vim -S will happily accept any file containing vimscript. So, write some commands for your custom layout (stolen from the other answer) edit file3 leftabove vsplit file2 split file1 And then do vim -S layout.vim. This also helps avoid cluttering the command and makes it easier to edit the layout commands.


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

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

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 ...


5

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


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 ]] || [[...


5

An awkward solution Copy content into a register from an editing window and paste into 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> W ...


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

From :h -u: -u {vimrc} The file {vimrc} is read for initializations. Most other initializations are skipped; see |initialization|. This can be used to start Vim in a special mode, with special mappings and settings. A shell alias can be used to make this easy to use. For example: alias vimc vim -u ~/....


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