45 votes
Accepted

Change an entire line in Vim

In the same way that yy copies the current line and dd deletes it, you can use cc to change it (i. e. yank it, delete it and enter insert mode). S is a synonym for cc.
user avatar
  • 1,912
37 votes
Accepted

How to delete line above/below cursor, but not current line?

:-d cuts the line above the current line. :-5d cuts the 5th line above the current line (but moves the cursor). :-5,-d cuts the 5 lines above the current line. :+,+5d cuts the 5 lines below the ...
user avatar
  • 33.1k
35 votes
Accepted

How to insert a newline without leaving normal mode

I use a mapping for that: " Quickly insert an empty new line without entering insert mode nnoremap <Leader>o o<Esc> nnoremap <Leader>O O<Esc> This way you can ...
user avatar
  • 46.5k
35 votes
Accepted

Simplest way to delete a "composite" word?

What you are calling a composite word is actually a WORD (by opposition to a word). Reading :h word and :h WORD should be helpful: *word* A word consists of a sequence of ...
user avatar
  • 46.5k
33 votes
Accepted

How to use a variable in the expression of a 'normal' command?

The execute function takes a string as argument, it expands the string and execute it a a regular ex command. Thus you can do: function! MyFunction(someArg) execute "normal! ". a:someArg. "l" ...
user avatar
  • 46.5k
32 votes
Accepted

How can I map d[count]d?

Operator-pending mode Between typing an operator (like d, c, or gU) and a motion (like w, i}, or /foo<CR>), Vim is in Operator-pending mode. You can create mappings for this using :omap and :...
user avatar
  • 7,240
30 votes

Splitting a line into two

For your specific case, when you have a space, that you also like to get rid of, I usually use rEnter
user avatar
29 votes
Accepted

What is the mnemonic for Ctrl-Y (in normal mode)?

Bill Joy and Mark Horton wrote in their original vi manual: If you want to see more of the file below where you are, you can hit ^E to expose one more line at the bottom of the screen, leaving the ...
user avatar
28 votes
Accepted

Execute normal command over range

You can do this with the normal command : :1,10normal d2w This is because the d operator doesn't accept a range, but only a motion : :h d ["x]d{motion} Delete text that {motion} moves ...
user avatar
  • 15.2k
27 votes
Accepted

Math calculations in Vim?

For this to work, you need to use the expression register. Which allows you to execute arbitrary vimscript and do something about the result of the evaluation. To use the expression register on insert ...
user avatar
  • 691
24 votes

How to insert a newline without leaving normal mode

A solution that doesn't go through insert mode, doesn't move the cursor, and allows you to use a counter to append several lines at once (3\o etc.): nnoremap <silent> <leader>o :<C-u&...
user avatar
  • 3,618
23 votes

Can I see the history of key presses in normal mode?

Following the link from @WChargin a bit further, I found this: You can start vim with the -w or -W option as follows: vim -w keys.txt my_file All the characters that you type are recorded in the ...
user avatar
  • 4,720
23 votes

How to insert a newline without leaving normal mode

If you don't want to add mappings or a package dependency, you can just yank yy an empty line and then pasted p for below cursor and P for above the cursor.
user avatar
  • 331
22 votes

How to delete content backwards until "=" at the previous line?

Not sure if I totally understand the question, but if you wanted to delete until you only have var a = {};, you could, from the cursor position, use d?1Enter d takes a motion, and you can give it a ...
user avatar
  • 5,886
20 votes
Accepted

Delete to EOL without entering insert mode

Yes. You want to use the "delete" operator instead of the "Change" operator. The delete operator is d<motion> and it deletes everything that <motion> moves over. In this case, the ...
user avatar
  • 16.8k
19 votes
Accepted

Hand Placement for Vim Navigation

First of all I will assume that you are using a QWERTY keyboard. My answer isn't based on my personal preference, I am simply reformulating a part of the amazing Practical Vim written by Drew Neil. ...
user avatar
  • 46.5k
19 votes

How I apply a command to all lines?

You are looking for the :> command and a range of 1,$ (% for short) for the whole file. :%> For more help see: :h :> :h :range
user avatar
19 votes

Delete n lines skip 1 line script

Very simple approach: Move to the first line you want to delete. Record a macro: qa3ddjq Repeat it with a high number: 1000@a Step three will repeat the macro a thousand times or until an error is ...
user avatar
  • 8,665
18 votes
Accepted

How do I shift the line indent to a specific value?

You can use the :left command: :[range]le[ft] [indent] Left-align lines in [range]. Sets the indent in the lines to [indent] (default 0). {not in Vi} Note that :left sets the indent to n ...
user avatar
18 votes
Accepted

What is the meaning of 'g'?

g is a little bit of a weird key in vim. A lot of other keys have a specific word to describe their category: h, j, k, l, }, {, w, e, etc. are all motions. They tell your cursor to move. d, c, y, etc....
user avatar
  • 16.8k
17 votes

Delete n lines skip 1 line script

The easiest solution to me would be: :%norm j3dd That is: %: for every line norm: run the following keys as if in normal mode j3dd: go down on line then delete 3 lines So from the first line, go ...
user avatar
  • 346
16 votes

What is the mnemonic for Ctrl-Y (in normal mode)?

I like to call <C-y> "Yester-line"™ like "Yesterday". Explaining it along with <C-e> rhymes, too. "Extra-line" and "Yester-line"™.
user avatar
  • 371
16 votes
Accepted

How can I delete whole word in command insert mode?

If you press Ctrl-U all characters to the left of the cursor until the beginning of the commandline will be deleted. This is also explained in the help at :h c_CTRL-U To delete a single word, you ...
user avatar
15 votes
Accepted

How do I flip characters or words like emacs' transpose?

For characters, it's fairly simple: xp to swap the letter under the cursor with the following letter, and Xp to swap the letter under the cursor with the previous letter. The x command deletes the ...
user avatar
  • 4,085
15 votes
Accepted

How does p command work in vim?

p and P always work the same way: p puts text after the cursor, P puts text before the cursor. But what you put with p and P can be "character-wise", "line-wise", or "block-wise". This means that ...
user avatar
  • 33.1k
15 votes
Accepted

How to delete entire function definition (C style)

Motion ][: next } in the first column V][d does what you want. More details can be found under :h ][ and :h section: *section* [...] The "]" and "[" ...
user avatar
  • 4,050
14 votes
Accepted

Go to last non-whitespace character of previous/next line?

g_ moves the cursor to the last non-blank character of the current line. From Vim's :help g_: g_ To the last non-blank character of the line and [count - 1] lines downward |...
user avatar
  • 5,886
14 votes

How to replace text inside () with previously yanked text?

You can use visual selection to select the text to replace, and then paste over it as follows: vi(p vi( selects Baz (because it is enclosed in parenthesis), p pastes Fnord over it. However, this ...
user avatar
  • 1,143
13 votes

How to insert a newline without leaving normal mode

I personally recommend using Tim Pope's Unimpaired plugin. It provides many mappings but the ones you will looking for are [<space> and ]<space> which create blank lines above and below ...
user avatar
13 votes
Accepted

Continue an ex mode command after "norm"?

If you have a sequence of keystrokes that you want to execute in normal mode from the command line, you can use the :normal command. However, by default the :normal command can't be followed by ...
user avatar
  • 6,466

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