John O'M.
  • Member for 6 years, 11 months
  • Last seen more than a month ago
How can I clear word highlighting in the current document (e.g. such as after searching for a word)?
Accepted answer
72 votes

As an alternative to :noh, I like to do :let @/="" mapped to a keyboard shortcut. The difference is that :noh leaves the search term in the search register, so n and N in normal mode resume the ...

View answer
Re-indenting badly indented code
Accepted answer
54 votes

I do this one of two ways. Indent adjusted paste First, if the code in the buffer is formatted, but at a different level of indentation, I use ]p instead of p, which pastes the code as is, but with ...

View answer
How do I change the current split's width and height?
51 votes

This is one of the few reasons I like to use vim's mouse mode. If you use the GUI version, or your terminal supports sending drag events (such as xterm or rxvt-unicode) you can click on the split ...

View answer
How to add indentation guides/lines
Accepted answer
51 votes

Unified answer: If you are using tabs I'd try using listchars: :set listchars=tab:\|\ :set list note: there is a space after the last \ above. This method is good because it uses Vim's built-in ...

View answer
How to reverse the order of lines?
35 votes

The power of global will work here: :g/^/exe "normal ddggP" Or, more simply (thanks @tommcdo) :g/^/move 0 The first will match every line and for each line, delete it and paste it to the top of ...

View answer
How do I jump to the location of my last edit?
28 votes

To add to dnetserr's answer and Peter Rincker's comment, Vim maintains a list of changes, and has some commands associated with this. :changes will list the changes, showing you where they were and ...

View answer
Does Ex mode have any practical use?
27 votes

I rarely use ex-mode, but when I need it I'm grateful for its existance. I sometimes access systems via ssh over VPN, and these connections can sometimes get slow. Making the problem worse, I ...

View answer
How do I change the default size of plugin window (NerdTree, Taglist, etc)?
Accepted answer
26 votes

The examples below set the width to 60 columns. Adjust to your preferences. NERDTree NERDTree uses a variable for just this: :let g:NERDTreeWinSize=60 This will set the width to 60. You can put ...

View answer
How can I redefine plugin key mappings?
23 votes

As mentioned in other answers, the plugins are sourced after the vimrc is done. If you want to keep your overrides in your vimrc instead of doing an after plugin, you can use this "trick" anywhere in ...

View answer
Insert an incrementing number to each line in either a selection or a match
Accepted answer
20 votes

Similar to the answer at https://vi.stackexchange.com/a/818/227, you can use the global command. With it you can instruct vim to search for lines matching a pattern, and then perform commands on it. ...

View answer
How do I open a file from another git branch?
18 votes

This is a little more broad than what OP asked, but for people not wanting to use plugins, and possibly other revision control systems, this little snippet tends to work fairly well: :new :r! git ...

View answer
How can I convert words to camelCase in a macro?
Accepted answer
17 votes

A One Liner I found a simple regular expression replacement (assuming 'magic' is set): :s/ \([a-zA-Z]\)/\u\1/g This match any single letter after a space, saving the letter. Then, it replaces it ...

View answer
What is <Leader>?
15 votes

To quote :help <Leader>: To define a mapping which uses the "mapleader" variable, the special string "<Leader>" can be used. It is replaced with the string value of "mapleader". If ...

View answer
What's the difference between let and set?
14 votes

:set is used for internal options and handles the types of the option. It allows things like inversion of boolean options (e.g. :set invpaste) adding and removing settings from a list (e.g. :set ...

View answer
Using marks with dead keys
Accepted answer
13 votes

Try making it a mapping: " Mapping dead keys in normal mode nmap á 'a That way, when Vim gets the accented a, it interprets it as `+a Using nmap allows this to happen in normal mode only; in insert ...

View answer
How can I run an autocmd when starting Vim with no file, a non-existing file, or a blank file (but not when starting with an existing file)?
Accepted answer
12 votes

This works for all 3 of your cases for me: function InsertIfEmpty() if @% == "" " No filename for current buffer startinsert elseif filereadable(@%) == 0 " File doesn'...

View answer
How do I move to the last character in a sentence?
Accepted answer
11 votes

From my research into this, you need to define at least two custom motions. The first will be for just moving the cursor, and the second will be for use in operators. For a full functioning setup, it ...

View answer
Why does the cursor move to the start of the line after <Esc>?
Accepted answer
11 votes

Looking at the documentation for autoindent has an answer as to why and how to work around it. :help 'autoindent': Copy indent from current line when starting a new line (typing <CR> ...

View answer
How can I print from vim using the native KDE print dialogue?
Accepted answer
10 votes

The loss of kprinter in the move to kde4 was one of the reasons I stuck with kde3 for as long as I could. After some searching, I found this "3rd party" replacement: http://kde-apps.org/content/show....

View answer
How is command history resolved between multiple instances of Vim?
Accepted answer
10 votes

The answer to your title question is what you observed. From the vim user manual :help 21.3 When you run Vim multiple times, the last one exiting will store its information. This may cause ...

View answer
How do I make Vim use a default filename when not given any filenames?
Accepted answer
10 votes

Try putting this at the end of your .vimrc (modify as appropriate) if @% == "" && getcwd() == "/tmp" :silent edit test.sh endif This checks if the current filename is empty (@% ...

View answer
Use shell variable substitution with current filepath
Accepted answer
9 votes

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

View answer
`echon` for `echomsg`
Accepted answer
9 votes

This is not currently possible in Vim. Internally, :echomsg is implemented as :execute, except that when invoked as :echomsg the result of execution is displayed with the attribute of the last :...

View answer
Vimwiki: can't fold list items
9 votes

Folding lists in VimWiki According to the documentation in vimwiki version 2.1 (the latest I found), there are a few fold methods, and of them only one works for folding list items. The setting is ...

View answer
Showing command history with showcmd
Accepted answer
9 votes

I couldn't find any way to do this in a regular Vim. However, if you are willing to patch Vim, the following may work for you. The very simple patch (applied against Vim-7.4.052) disables the actual ...

View answer
Using set listchars, what are 'extends' and 'precedes'?
Accepted answer
8 votes

Extends and Precedes are used when you don't have line wrapping on, to show that there is more text to the left or right of the line. In a wide window, I might have |This is a long line of text.$ ...

View answer
Reopen the most recently closed buffer
Accepted answer
8 votes

I played around with this a bit and the following seems to work for the cases I tested, including closing a window split and a tab with one window using :q. augroup bufclosetrack au! autocmd ...

View answer
Open file and scroll to bottom command line param?
Accepted answer
8 votes

The following works for me: vim log +$ The + bit tells vim to run a command after startup. The $ command moves to the last line in the file. Depending on your shell, you might need \ before the $ ...

View answer
How to add permanent line numbers to a file?
8 votes

I like to use the vim global command to accomplish tasks like this. This applies to adding iteration to the beginning of a line or modify a symbol in the text. It looks more complicated than the other ...

View answer
Jumping to a file in vimscript
Accepted answer
7 votes

If you don't care about the column, and as an example you have the linenumber and filename in variables, you could do this: execute "edit +" . linenumber . " " . filename Or, as from your example ...

View answer