EvergreenTree
  • Member for 6 years, 11 months
  • Last seen more than a month ago
List known filetypes
Accepted answer
94 votes

Simple Solution Type :setfiletype (with a space afterwards), then press Ctrl-d. See :help cmdline-completion for more on autocompletion in vim's command line. Complicated Solution This solution ...

View answer
How to make a vimscript function with optional arguments?
Accepted answer
49 votes

Yes, you can take optional arguments in a function. It is not as convenient as python's way of doing it, but this is how you do it: function FooBar(...) " This is like *args in python echom a:0 "...

View answer
How to indent as spaces instead of tab?
Accepted answer
48 votes

You are looking for the expandtab option. When this option is set, spaces are always used. You can put set expandtab in your vimrc to always have this option set when vim starts. If you want to ...

View answer
Can I delete an entire line, except the line break?
Accepted answer
43 votes

You can use 0D this will go to the first character on the line and delete until the end of the line. Note that you can use ^D if you want to leave any preceding whitespace alone. Also, these ...

View answer
Suppress output from a vim autocomand
Accepted answer
29 votes

Silent should go before !./make.sh, not before au. Putting silent before au just means it will register the autocommand silently. It should look like this instead: au BufWritePost * silent! !./make....

View answer
How do I apply a set of keystrokes to every occurrence of a word?
Accepted answer
21 votes

You can use the :global and :normal commands for this. The :global command has the following syntax: :global/{pattern}/{command} This will run "command" on every occurrence of "pattern". You can ...

View answer
How to deal with string containing spaces in Vim script?
Accepted answer
16 votes

The function you are looking for is fnameescape(). It will escape all special characters in a given string containing a path to a file to match the standard of the current operating system. For more ...

View answer
How do I restore a group of tabs?
15 votes

In vim, there is a feature which allows you to save your current session to a vimscript file. This can be done using the :mksession command. Here is a synopsis of the command given in the vim ...

View answer
Is there a command to enter Visual Block mode?
Accepted answer
14 votes

There is no built in command to start visual block mode in vim, but you can define one yourself: command! Vb normal! <C-v> Here is a breakdown of how it works: command! Vb - This creates a ...

View answer
Browse oldfiles list length
Accepted answer
13 votes

:browse oldfiles and :oldfiles read from the viminfo file, so changing the maximum number of previously edited files to be remembered in the 'viminfo' option should work. You can do this by changing ...

View answer
Run bash command on writing a file with a particular extension?
Accepted answer
12 votes

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

View answer
Substitute with pure vimscript (without `:s`)
11 votes

The reason it is warning you about unintended side effects is because :substitute does move the cursor and overwrite the previous search (if used outside of a function). However, this does not mean ...

View answer
Can :Explore be started from the terminal?
11 votes

The "Explore Mode" you are talking about is netrw, a vim plugin which is provided by default. It should open in that "mode" if you just try to open a directory with vim, eg. vim foo/bar/baz/

View answer
Is there a text object for the entire buffer?
10 votes

There is no text object for the whole file by default, but it is possible to create them using omap. In this case, it would look something like this: onoremap f :<c-u>normal! mzggVG<cr>`...

View answer
Make Command-line mode type <Esc> and Ctrl commands
10 votes

This is because special keys like <Esc> are not translated in normal commands, and are treated like you pressed <Esc>. To remedy this, you can use an exec command. Like so: :exec "norm cw\"...

View answer
Can I repeat the last UI command?
9 votes

There is no way of doing this by default in vim because vim does not keep track of the previously executed wincmd. However, it is possible to do this through some clever mappings: function! s:Wincmd(...

View answer
How can I disable all mouse functions in MacVim?
Accepted answer
9 votes

To disable all mouse functions, you can simply put this in your vimrc file: set mouse= For more, see :help 'mouse'.

View answer
How to wait for user input in the middle of a mapping?
Accepted answer
9 votes

You can use a mapping with the <expr> flag to achieve this. Mappings with the <expr> flag will evaluate the right hand side of the mapping as an expression and then apply the result as ...

View answer
Calling function to insert text with inoremap
Accepted answer
9 votes

There is a better way of using functions in mappings, and that is by using the <expr> flag. For example, say you wanted to insert the current line number at the cursor position by typing ln in ...

View answer
Read file from vimscript variable
Accepted answer
9 votes

This is because vim does not evaluate variable names or functions with most commands. In your case, vim thinks you literally want to read the contents of a file called g:filePath into the buffer. ...

View answer
How to substitute text across line breaks?
Accepted answer
9 votes

You can use \n to do substitutions across multiple lines. There are also special characters which start with \_ that are the same as their normal counterparts, except it adds newline to their ...

View answer
What is the difference between set and :set (commands with or without a leading colon)?
8 votes

There is no difference. If vim sees a : before a command in your vimrc (or any vimscript file) it will ignore it. Also, as mentioned above, this isn't specific to the set command. Usually the : ...

View answer
Vimscript file rewrites itself and complains of a mismatched endfor
Accepted answer
8 votes

There is a much better way of accomplishing this than brute forcing it. You can use these simple expr mappings instead: nnoremap <expr> <c-i> "i" . nr2char(getchar()) . "<Esc>" ...

View answer
Substitute pattern between braces
Accepted answer
8 votes

First Solution This requires some advanced cmdline-range magic to work. Here is what I have come up with: :?{?,/}/s=bar=baz=g Now, to break it down: ?{? - Searches backwards for the first line ...

View answer
How to insert a file path with tab- or autocompletion relative to the current file's dir?
7 votes

You can use vim's file name completion feature for this. To use it, press CTRL-xCTRL-f in insert mode. For more on this, see :help compl-filename. Do note that it isn't recursive, so you must ...

View answer
Delete text after match in Ex?
Accepted answer
7 votes

There is a better way of accomplishing the task you showed in ex mode, which is by using the :delete command and a basic cmdline-range. To delete all lines between and including the beginning of the ...

View answer
When is it recommended to use the scope s: and the argument abort to define a function?
Accepted answer
6 votes

The s: simply means you are not polluting the global namespace. Usually you would do this if it is a function that doesn't need to be called by the user. As for the abort flag, here is what the docs ...

View answer
vimscript conditional based on grep on current buffer
Accepted answer
6 votes

The function you are looking for is search(). This function will search starting at the cursor position, and when a match is found its line number will be returned. If no match is found, 0 is ...

View answer
Use vi/vim from shell to filter / format text
6 votes

You can launch vim using two flags to accomplish this. The two flags are -e and -s. When vim is launched with the -e flag, vim automatically enters ex mode, which is just vim's command line and ...

View answer
normal mode delete with search pattern - not deleting whole match
Accepted answer
5 votes

Add \zs to the end of the search pattern. This will make it so that everything before \zs will be excluded from the actual match, but it is required to be there. The counterpart of \zs is \ze, which ...

View answer