Vim delete word at cursor

I’m now switching to VIM from TextMate. I found ^+W in INSERT mode very useful. However, I’d like to delete not only the word before cursor, but the word after or around cursor as well.

I did some googling, but the only thing I can find was ^+W to delete word BEFORE cursor.

Miraj50's user avatar

Miraj50

4,1871 gold badge21 silver badges33 bronze badges

asked May 7, 2009 at 10:14

Mantas's user avatar

0

Normal mode:

daw : delete the word under the cursor    
caw : delete the word under the cursor and put you in insert mode 

answered May 7, 2009 at 14:34

Amjith's user avatar

AmjithAmjith

22.3k14 gold badges43 silver badges38 bronze badges

5

I think it’s just daw

daw - delete a word

Kevin's user avatar

Kevin

6,4585 gold badges44 silver badges53 bronze badges

answered Sep 13, 2010 at 18:57

steve's user avatar

stevesteve

1,1711 gold badge7 silver badges2 bronze badges

4

What you should do is create an imap of a certain key to a series of commands, in this case the commands will drop you into normal mode, delete the current word and then put you back in insert:

:imap <C-d> <C-[>diwi

Pavlo's user avatar

Pavlo

42.5k14 gold badges76 silver badges113 bronze badges

answered May 7, 2009 at 15:27

Whaledawg's user avatar

WhaledawgWhaledawg

4,1844 gold badges26 silver badges21 bronze badges

2

The below works for Normal mode: I agree with Dan Olson’s answer that you should probably be in normal mode for most deletions. More details below.

If the cursor is inside the word:
diw to delete in the word (doesn’t include spaces)
daw to delete around the word (includes spaces before the next word).

If the cursor is at the start of the word, just press dw.

This can be multiplied by adding the usual numbers for movement, e.g. 2w to move forward 2 words, so d2w deletes two words.

Insert Mode ^w
The idea of using hjkl for movement in Vim is that it’s more productive to keep your hands on the home row. At the end of a word ^w works great to quickly delete the word. If you’ve gone into insert mode, entered some text and used the arrow keys to end up in the middle of the word you’ve gone against the home-row philosophy.
If you’re in normal mode and want to change the word you can simply use the c (change) rather than d (delete) if you’d like to completely change the word, and re-enter insert mode without having to press i to get back to typing.

answered May 7, 2009 at 10:18

Andy's user avatar

AndyAndy

3,75424 silver badges28 bronze badges

0

answered May 7, 2009 at 10:17

Nadia Alramli's user avatar

Nadia AlramliNadia Alramli

111k36 gold badges172 silver badges152 bronze badges

3

To delete all characters between two whitespaces, in normal mode:

daW

To delete just one word:

daw 

answered Dec 7, 2016 at 19:42

krmld's user avatar

krmldkrmld

1,20814 silver badges9 bronze badges

It doesn’t look like there’s any built-in way to do it in insert mode, which was the question. Some of the other answers are correct for normal mode, as well as pointing out that a custom mapping could be created to add the functionality in insert mode.

Honestly, you should probably do most of your deleting in normal mode. ^W is neat to know about but I’m not sure I can think of a situation where I’d rather do it than esc to go into normal mode and have the more powerful deletion commands at my disposal.

Vim is very different from a number of other editors (including TextMate) in this way. If you’re using it productively, you’ll probably find that you don’t spend very much time in insert mode.

answered May 7, 2009 at 10:34

Dan Olson's user avatar

Dan OlsonDan Olson

22.6k4 gold badges41 silver badges56 bronze badges

3

To delete the entire word your cursor is on use diw
To delete the entire word your cursor is on and to put you in insert mode use ciw
if you dont want to delete the entire word but delete from where you are the dw/cw

answered Apr 21, 2017 at 19:57

Naseeruddin V N's user avatar

Since there are so many ways to delete a word, let’s illustrate them.

Assuming you edit:

foo-bar quux

and invoke a command while the cursor is on the ‘a’ in ‘bar’:

foo-bquux  # dw:  letters then spaces right of cursor
foo-quux   # daw: letters on both sides of cursor then spaces on the right 
foo- quux  # diw: letters on both sides of cursor
foo-bquux  # dW:  non-whitespace then spaces right of cursor
quux       # daW: non-whitespace on both sides of cursor then spaces on the right
 quux      # diW: non-whitespace on both sides of cursor

answered Mar 25, 2019 at 12:05

0xF's user avatar

0xF0xF

3,0551 gold badge23 silver badges29 bronze badges

0

Insert mode has no such command, unfortunately. In VIM, to delete the whole word under the cursor you may type viwd in NORMAL mode. Which means «Visual-block Inner Word Delete». Use an upper case W to include punctuation.

answered Nov 10, 2017 at 16:44

mycelo's user avatar

mycelomycelo

4095 silver badges4 bronze badges

The accepted answer fails when trying to repeat deleting words, try this solution instead:

" delete current word, insert and normal modes
inoremap <C-BS> <C-O>b<C-O>dw
noremap <C-BS> bdw

It maps CTRL-BackSpace, also working in normal mode.

answered May 31, 2009 at 6:23

0

For deleting a certain amount of characters before the cursor, you can use X. For deleting a certain number of words after the cursor, you can use dw (multiple words can be deleted using 3dw for 3 words for example). For deleting around the cursor in general, you can use daw.

answered May 7, 2009 at 10:18

John T's user avatar

John TJohn T

23.6k11 gold badges56 silver badges82 bronze badges

In old vi, b moves the cursor to the beginning of the word before cursor, w moves the cursor to the beginning of the word after cursor, e moves cursor at the end of the word after cursor and dw deletes from the cursor to the end of the word.

If you type wbdw, you delete the word around cursor, even if the cursor is at the beginning or at the end of the word. Note that whitespaces after a word are considerer to be part of the word to be deleted.

answered May 7, 2009 at 10:34

mouviciel's user avatar

mouvicielmouviciel

66.5k13 gold badges110 silver badges140 bronze badges

Not exactly sure about your usage of «before» and «after», but have you tried

dw

?

Edit: I guess you are looking for something to use in insert mode. Strangely enough, there is nothing to be found in the docs, ctrl-w seems to be all there is.

answered May 7, 2009 at 10:18

innaM's user avatar

innaMinnaM

47.4k4 gold badges67 silver badges87 bronze badges

I made two mappings so I could get consistency in insert and out of insert mode.

:map <'Key of your choice'> caw

:imap <'Key of your choice'> <'Esc'> caw

Sagar Jain's user avatar

Sagar Jain

7,30512 gold badges46 silver badges83 bronze badges

answered Jan 19, 2014 at 17:16

userFog's user avatar

userFoguserFog

10.4k1 gold badge14 silver badges7 bronze badges

I’d like to delete not only the word before cursor, but the word after or around cursor as well.

In that case the solution proposed by OP (i.e. using <c-w>) can be combined with the accepted answer to give:

inoremap <c-d> <c-o>daw<c-w>

alternatively (shorter solution):

inoremap <c-d> <c-o>vawobd

Sample input:

word1 word2 word3 word4
               ^------------- Cursor location

Output:

word1 word4

answered Apr 3, 2018 at 2:36

builder-7000's user avatar

builder-7000builder-7000

7,0313 gold badges18 silver badges43 bronze badges

  • First install Vim.

  • To install the packages, first install ‘vundle’ using following command. Use ‘git-shell’ to run this command in Windows.

    git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
    
    (in Windows : use 'git-shell' to run below command)
    git clone https://github.com/VundleVim/Vundle.vim.git C:/Users/<Username>/.vim/bundle/Vundle.vim
    
  • Copy and paste the .vimrc file in the home directory.

  • In Windows, paste in the .vimrc file at C:/Users/<Username>/; and change the line “set rtp+= …” in .vimrc with correct location of “Vundle.vim” i.e. “set rtp+=C:/Users/<Username>/.vim/bundle/Vundle.vim”.

  • Use double quote to comment/uncomment the packages in .vimrc file e.g. ” Plugin ‘mattn/emmet-vim’ will not install the package “emmet-vim”. Or add some more packages in .vimrc file as required.

  • After selecting the packages, open vim and run the command -> :PluginInstall and it will install the all the plugins.

  • Use :help <plugin name> to see the list of operations.

1.1. Starting Vim¶

Commands Descriptions
:w save file
:w <filename> save file as <filename> and keep the current file open
:sav <filename> save file as <filename> and open <filename>
:q quit (if already saved)
:q! quit without saving
:e <filename> open new/existing <filename> in new buffer
:wq save and quit
:bn go to next buffer i.e. file
:b <filename> go to buffer with <filename>
:bd close current file without exiting vim
:bd! close current file without exiting vim and ‘no modification’
:vim * open all files in the directory (all in same buffer)
:vim file1 file2 file3 open file1, file2 and file3 in vim
:n go to next file
:n <filename> go to file name
:prev go to previous file
ctrl-Z suspend vim
fg bring forground vim

1.2. Undo/Redo¶

Commands Descriptions
u undo
crtl-r redo

1.3. Insert¶

Commands Descriptions
i insert mode at cursor
I insert mode at beginning of the line (i.e. first character of line)
s delete character under the cursor and enter into insert mode
S delete the line and go to insert mode from the beginning of same line
a insert mode after the cursor
A insert mode at the end of line
o insert mode on below line
O insert mode at bottom line
C delete from cursor to end of line and go to insert mode
r replace current character
R replace characters until Esc is pressed (i.e. same as insert button in keyboard)

1.4. Copy/Paste/Delete¶

Commands Descriptions
y yank (copy)
yiw copy the word
yw copy the word after the cursor
yy or Y copy the line
y$ copy till end of the line from current location of cursor
“+y copy to clipboard e.g. “+yiw will copy the word in clipboard
<F3>y same as above (use <F3> for clipboard, remapped in .vimrc)
<F3>p paste from clipboard (see above line as well)
p paste after cursor (remapped as ]p in .vimrc )
]p paste with indentation
P paste before cursor
ctrl-P paste from clipboard (remapped in .vimrc)
shift insert paste from clipboard (remapped in .vimrc)
d<command> delete<command>
diw delete word (and stay at normal mode)
ciw delete word (and go to insert mode)
dw or cw delete word after the cursor
dd or cc delete line
D or C delete till end of line
x delete character (delete)
X delete character (backspace)
. repeat previous operation

1.5. Paste in search or colon commands¶

  • Following commands can be used at command mode during search or color commands e.g. :w ctrl r ctrl w etc.
Commands Descriptions
ctrl r “ paste the last copied data
ctrl r ctrl w paste the word under cursor
ctrl r % print the naem of current file
shift insert paste the data from clipboard

1.6. Search¶

Commands Descriptions
/ forward then use n/N for next/previous match
? backward
* (asterisk) word under cursor forward (exact match)
g* word under the cursor (partial match)
# word under cursor backward (exact match)
g# word under cursor backward (partial match)
/<word1> search for exact match for “word1”
:set ignorecase use this option for avoiding case-matches
:set noignorecase use this option for case-matches

1.7. Replace¶

  • Use ‘c’, ‘g’, ‘gc’ and other combination to perform the desired replacement.
Commands Descriptions
:s /word1/word2 substitute word1 with word2 in current line (only first occurrence)
:s /word1/word2/c substitute word1 with word2 in current line after confirmation (only first occurrence)
:s /word1/word2/g substitute word1 with word2 in current line (all occurrence)
:s /word1/word2/gc substitute word1 with word2 in current line ater confirmation (all occurrence)
:1,4 s /word1/word2 substitute word1 with word2 in lines 1 to 4 (only first occurrence in each line)
:%s /word1/word2/g replace all occurrences
:%s /word1/word2/gc replace all occurrence after confirmation
:%s /ctrl-r ctrl-w/word2/gc replace all occurance of the word under cursor after confirmation (exact match)
:s /<word1>/word2 substitute exactly matched “word1” with word2 in current line

1.8. Indentation¶

Commands Descriptions
>> Right indent the current line
5>> Right indent 5 lines
<< De-indent line
5== Re-indent 5 lines
>% Increase indent of a braced or bracketed block (place cursor on brace first)
=% Reindent a braced or bracketed block (cursor on brace)
<% Decrease indent of a braced or bracketed block (cursor on brace)
]p Paste text, aligning indentation with surroundings
=i{ Re-indent the ‘inner block’, i.e. the contents of the block
=a{ Re-indent ‘a block’, i.e. block and containing braces
>i{ Increase inner block indent
<i{ Decrease inner block indent
Commands Descriptions
:retab convert existing tabs to spaces

1.9. Mouse settings¶

Commands Descriptions
:behave mswin mouse functionality will work as windows
:behave xterm mouse functionality will word as x-windows

1.10. Command/Visual Mode¶

Commands Descriptions
Esc or crtl-[ command mode
v visual mode
crtl-v visual block mode

1.11. Cursor movement¶

Note

To run the ‘ctrl-]’ and ‘ctrl-T’ command, we need to create the tags first. In the below command, the ‘tags’ will be created for all the python files in the directory, which will be stored in a file ‘tags’,

(run the following command in shell)
ctags -R *.py
Commands Descriptions
h left
j down
k up
l down
w forward to the beginning of next word
3w forward 3 words
W forward to the beginning of next word (only spaces are the end of word)
b backward to the beginning of next word
B backward to the beginning of next word (only spaces are the end of word)
e forward to end of next word
E forward to end of next word (only spaces are the end of the word)
gg go to first line of page
G go to end line of page
10G go to 10th line
10gg go to 10th line
0 go to first character of line
$ go to end character of line
^ go to first non-black character of line
M go to middle of the screen
fa go to next a in current line
Fa go to previous a in current line
ta go to end of next a in current line
Ta to to end of previous a in current line
ctrl-o go to previous location e.g. we went to line 10 from line 18, ctrl-o will go to line 18 again
ctrl-i or Tab go to next location i.e. go to line 10 again from line 18.
gd go to the local declaration of the word under cursor
gD go to the global declaration of the word under cursor
g* search for the word under the cursor
g# same as g* but in backward direction.
gf go to the filename under the cursor, use ctrl-o to go back
ctrl-] go to tag definition (a tag can be a function or variable name etc.); use ctrl-o to go back
ctrl-T go back to previous loation from where ctrl-] was exectued

1.12. Screen movements¶

Commands Descriptions
ctrl-d move half screen down
ctrl-f page down
ctrl-e move one line down
ctrl-u move half screen up
ctrl-b page up
ctrl-y move one line up
z<Enter> move current line to the top of screen (with cursor at the beginning)
zt move current line to the top with without changing the location of cursor
move current line to the center of screen (with cursor at the beginning)
zz move current line to the center of screen (without moving cursor)
z- move current line to the center of screen (with cursor at the beginning)
zb move current line to the center of screen (without moving cursor)

1.13. Unix Shell¶

Commands Descriptions
:shell go to unix shell
exit type exit in unix shell to come back in Vim

1.14. Registers¶

Commands Descriptions
“ayy copy line in register ‘a’
“ap paste content of register ‘a’
Capital letters append the new value to previously stored values
“Ayy copy line and append to previous value in register “a”
Then use “a to paste the value in register ‘a’
“=3*2<Enter>p paste the result i.e. 6 at the current cursor location
:registers display the values in all the registers
:registers abc display the values of registers a, b and c

1.15. Multiple Files¶

Commands Descriptions
:arg grep -l ‘import’ *.py open all files in current folder which contains word ‘import’

1.16. Mark¶

Commands Descriptions
ma mark the line with name ‘a’ (use a-z or 0-9)
  Next go to some other line and excute following command
d’a delete till line which is marked as ‘a’
:marks show the list of marks
‘a go to mark a

1.17. Sorting¶

Commands Descriptions
!10G (press enter) sort (press enter) it will sort first ten lines according to name
!G (press enter) sort (press enter) it will sort all the lines according to names
!!ls go to terminal, run ls command and print the output on the file (i.e. print list of file in current directory)
!!dates go to terminal, run date command and print the output on the file

1.18. Printing the code¶

Commands Descriptions
:hardcopy <filename.pdf> open the printing-instructions-window
:TOhtml and then save the file Save in html format, use :colorscheme default for white background

1.19. Mapping¶

  • Please read the comments in .vimrc file for more commands and details
  • comment/uncomment the command using double quote as per requirement.
Commands Descriptions
:map display the key-mappings,

1.19.1. Copy/paste from clip board¶

Use <F3> and then normal copy/paste command from clipboard.

Copy/paste to clip board  
<F3> is remapped nnoremap <F3> “+
<ctrl-P> is remapped nnoremap <C-P> “+]p (paste with indentation)
<F3>yiw or <F3>yy etc. copy the word or line etc. in clipboard
<F3>p paste the data from clipboard

1.19.2. Disable arraow keys¶

Below code can be used in .vimrc file

“Key mappings : disable arrow keys
no <left> <Nop>
no <down> <Nop>
no <up> <Nop>
no <right> <Nop>
ino <down> <Nop>
ino <left> <Nop>
ino <right> <Nop>
ino <up> <Nop>

1.19.3. Code execution¶

“python commands
” Execute : F9 (Below code is used in .vimrc file)
:autocmd FileType python :nmap <F9> :! clear <CR> :! python % <Enter>
“C/C++ commands
“Compile : F9 (Below code is used in .vimrc file)
:autocmd FileType c,cpp :nmap <F9> :! rm -r out <CR> :! clear <CR> :! g++ % -o out <Enter>
“Run : Ctrl+F9 (Below code is used in .vimrc file)
:autocmd FileType c,cpp :nmap <C-F9> :! clear <CR> :! ./out <CR>

1.20. Buffer¶

Commands Descriptions
:bn go to next buffer i.e. file
:b <filename> go to buffer with <filename>
:bd close current file without exiting vim
:bd! close current file without exiting vim and ‘no modification’

1.21. Split windows¶

Commands Descriptions
:split split window in two part and display current file in both window
:split <filename> open <filename> in split window
:5 split <filename> open <filename> in new split window with width of 5 line
:new split window in two part with second window as blank
crtl-w j go to below split window
crtl-w k go to above split window
crtl-ww, crtl-w w go to next split window
crtl-w + increase the width of split window by one line
5 crtl-w — decrease the width of split window by 5 line
crtl-w = make all split window of equal size
crtl-w _ maximize the current split window

1.22. Auto completion¶

Commands Descriptions
ctrl-p,ctrl-n auto complete by looking previous/next words (use ctrl-p or ctrl-n to change the words from list)

1.23. Text files¶

Commands Descriptions
:set textwidth=50 change the line after 50 character
:1,5 center 50 textwidth = 50 and center the lines 1 to 5
:1,5 right 50 textwidth = 50 and right justify the text on lines 1 to 5
:1,5 left 4 left margin = 4 for lines 1 to 5
Use $ for end of the line as shown below,  
:1,$ center 50 textwidth=50 and cneter all the line
or use % sign for the file (results is same as above,  
:% center 50  
:set wrap turn the wrap words on
:set nowrap turn off the wrap words

1.24. Macros¶

Commands Descriptions
qa start recording and store in reg ‘a’. Then perform certain operations. press ‘q’ again to stop recording.
@a execute macro
3@a repeat macro 3 times

1.25. More commands¶

Commands Descriptions
ctrl + g name of current file
ctrl + u move half screen up
ctrl + d move half screen down
J join line below with current line
3J join below two line with this line (not 3)
z= spell suggestion
~ change case of letter
:digraphs to see the list of symbols e.g. copyright etc.

1.26. Block Visual Mode¶

Add same items in the beginning/end/middle of all the lines

Commands Descriptions
crtl-v select the block with cursor movement
press
I (insert before)
or A (insert after)
or c (replace)
type the text -> press Esc -> block will be replaces by text

1.27. Zoom Screen¶

Commands Descriptions
Zoom in crtl-+
Zoom out ctrl–

1.28. Save session¶

Commands Descriptions
:mksession name.vim save session
:mksession! name.vim override session
:source name.vim load session

1.29. Folding¶

Use space (remapped in .vimrc) at the line below the function definition for folding/unfolding the code.

1.30. Plugins¶

First install ‘vundle’ using following command. Use ‘git-shell’ to run this command in Windows.

  • git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
  • After that copy and paste the .vimrc file in the home directory (in Windows paste in the director C:/Users/<Username>/)
  • Use double quote to comment/uncomment” the packages in .vimrc file e.g. ” Plugin ‘mattn/emmet-vim’ will not install the package “emmet-vim”. Or add some more packages in .vimrc file as required.
  • After selecting the packages, open vim and run the command -> :PluginInstall and it will install the all the plugins.
  • Use :help <plugin name> to see the list of operations.

1.30.1. NERDTree¶

Commands Descriptions
NERDTree turn on the directory structure
NERDTree! disable the directory structure
m then use various operations for delete, and create files.
e.g  
m-a meher create a file with name meher
m-a meher/ create a folder with name meher

1.30.2. Surround¶

Commands Descriptions Results
ysiw” add “” to the word Meher -> “Meher”
ds” delete surround quotes “Meher” -> Meher
cs[( or cs]) change surround [] by () [2+3]/2 -> (2+3)/2
cst<h1> change <p> tag to <h1> <p>Meher Krishna</p> -> <h1>Meher Krishna</h1>

1.30.4. vim-table-mode¶

Follow the below steps to create the ‘rst-table’ in Vim,

  • Enable/Disable table mode using ‘:TableModeEnable/TableModeDisable’ or using ‘:TableModeToggle’,
  • Type column-names e.g. |Col1 | Col2| and press enter
  • Then press | twice to create the table. Do the same to add more lines in the table.
  • Also, go above the |Col1 | Col2| and press | twice (required for correct rst-table-format)

1.30.5. vim-extline¶

This is used to underline the text, which is required for making Headings in .rst document.

  • Type the symbols (in insert mode) e.g. = and then press ctrl-l ctrl-l to complete it.
  • Or press ctrl-l and then press the symbol,
  • Or press ctrl-l and then press a number e.g. 1, 2 etc. This is autocomplete the underline base on heading levels.
  • Some more commands are listed below,
Commands Descriptions
ctrl-l ctrl-l Auto-line update
ctrl-l ctrl-h Horizontal line update
ctrl-l ctrl-u Change to underlined title
ctrl-l ctrl-o Change to overlined title
ctrl-l ctrl-i Change to underlined and overlined title
ctrl-l = or = ctrl-l Force Section heading (with = as underline)
ctrl-l 2 Force Section heading (level 2)

1.30.6. ConqureShell¶

Use :ConqueTermSplit to start the terminal in the Vim.

1.30.7. Airline¶

Airline theme plugin for better view e.g. display name of file, line number, column number etc.

I know I’ve probably looked over this a million times in all the vi documents I’ve read, but I can’t seem to find the delete from cursor to end of line command.

0xSheepdog's user avatar

0xSheepdog

2,7321 gold badge19 silver badges31 bronze badges

asked Nov 29, 2010 at 20:58

Falmarri's user avatar

1

D (uppercase letter D)

The command dw will delete from the current cursor position to the beginning of the next word character. The command d$ (note, that’s a dollar sign, not an ‘S’) will delete from the current cursor position to the end of the current line. D (uppercase D) is a synonym for d$ (lowercase D + dollar sign).

ilkkachu's user avatar

ilkkachu

129k15 gold badges231 silver badges386 bronze badges

answered Nov 29, 2010 at 21:00

Tok's user avatar

TokTok

10.3k2 gold badges25 silver badges12 bronze badges

0

As others have mentioned: you can use d$ or D (shiftd) to delete from the cursor position until the end of the line.

What I typically find more useful is c$ or C (shiftc) because it will delete from the cursor position until the end of the line and put you in [INSERT] mode.

Pierre.Vriens's user avatar

answered Dec 8, 2015 at 20:57

Brad Johnson's user avatar

Brad JohnsonBrad Johnson

1,2911 gold badge8 silver badges4 bronze badges

5

One of the nice things about vi is its logical command structure. d followed by a motion command deletes to the target of that motion. $ moves to the end of the line (mnemonic: like in regexps). So d$ deletes to the end of the line. Similarly, e moves to the end of the current word, and w moves to the beginning of the next word; so de deletes the end of the current word, and dw additionally deletes the following whitespace.

answered Nov 29, 2010 at 21:33

Gilles 'SO- stop being evil''s user avatar

3

You probably want to use D. Move the cursor to the first character you want to delete, then hit shift-D. Everything gone. Actually, it’s in the default cut buffer, so you can P or p paste it back in.

I use Dp (delete to end of line, then put it back), move to the end of some other line, then p again to paste the same text in at the end of this other line. Works wonders in config files, where you need to put some complicated URL in two or more places.

answered Nov 30, 2010 at 4:52

1

I think an insert mode shortcut could come in handy.

In insert mode maybe would be better starting changing until the end of line (put this on your ~/.vimrc):

inoremap <C-l> <C-o>C

So you have, as have been said, D in normal mode and Ctrl+l in insert mode. As you can see you have also C that starts changing till the end of the line.

<C-o> ......... insert normal keystroke in insert mode

I have chosen Ctrll because l is under your fingers. The Ctrlk is already used to insert digraphs.

I have been searching through :h i_Ctrl for some free keybindings, and it is actually the bigger problem when it comes to creating new shortcuts to perform actions in vim.

answered Jan 19, 2018 at 10:19

SergioAraujo's user avatar

To delete a range of lines from after the cursor position, 3D will delete from the cursor until the end of the line, as well as the next two lines completely (i.e., deletes 3 lines after the cursor position).

e.g. for the following text (cursor represented as |),

If there's a cursor |in the line
here
we
go

Using the command 3D will output:

If there's a cursor
go

answered Jul 5, 2018 at 13:51

Nick Bull's user avatar

Nick BullNick Bull

5433 silver badges13 bronze badges

0

My cursor is on the final character of a word in Vim. Without moving the cursor, is there any succinct way to delete the current word and the word before it?

As far as I can tell, the only way to delete the current word is to use diw. Using db leaves the character under the cursor (which happens to be the last character).

asked Sep 12, 2011 at 16:26

wanttobegoodatvim's user avatar

I would do either 2dbx or vbbd. Or v5bd if there were 5 words to delete.

I prefer vbbd as I like to have some visual clue of what I’m going to do and it feels a bit cleaner.

answered Sep 12, 2011 at 17:51

romainl's user avatar

romainlromainl

22.1k2 gold badges48 silver badges58 bronze badges

2

Backward moving actions, (like db) take effect from the boundary in front of the cursor.

Probably your easiest way to do this is something like xd2b — that takes care of the character under the cursor first.

Generally I prefer to use Bdw (or wdB) when deleting whole words, as this doesn’t leave you with a double space where the word was. Obviously this depends on the context of your actions, and what else you’re trying to achieve.

answered Sep 12, 2011 at 16:39

paulw1128's user avatar

2

d2vb is one option. The added v modifies the motion to also delete the character under the cursor.

Details:

Vim makes a distinction between inclusive and exclusive motion. v
toggles the «inclusiveness» or «exclusiveness» of a motion. For an
example of toggling the opposite direction (inclusive => exclusive),
try it with e:

dve

See :help inclusive for an explication. Until now, you thought it
was just esoteric nonsense! Didn’t you? Didn’t you?! (At least, my
eyes glazed over whenever I used to read that section in the help…
:)

Source.

answered Nov 1, 2016 at 5:00

Braham Snyder's user avatar

Administrative Commands

Runtime Commands

Path to runtime executable

:echo $VIMRUNTIME

Example Output

/usr/local/share/vim/vim74

Show path order

:set runtimepath?

Use :set rtp as a shorter alternative to same command.

Example Output

~/.vim, /usr/local/share/vim/vimfiles, /usr/local/share/vim/vim74, /usr/local/share/vim/vimfiles/after, ~/.vim/after

Version

Show advanced version info

:version

Settings

Check if compatible with vi mode is on or off

:set compatible?

Plugins

Check if plugins are set to load

:set loadplugins?

List all sourced plugin scripts (pathogen plugin mgr should appear before other plugins)

:scriptnames

Troubleshooting Tips

  • Backup up your .vimrc and create a new one with minimal config.
    • If testing pathogen plugin just put execute pathogen#infect(), syntax on, filetype plugin indent on in .vimrc

Buffers and Windows

Buffers

Open an empty buffer in the current window.

:new

Buffers will become hidden when they are abandoned.

:set hidden

List open buffers.

:ls
:buffers

Next buffer, Previous buffer

:bn, :bp

Go to third buffer, for example

:b3
:buf 3

Windows

Open a file in current window

:e filename.txt

Open a file, horizontal split

:split filename.txt
:sp filename.txt

Open a file, vertical split

:vsplit filename.txt
:vsp filename.txt

Move cursor to previous window.

C-w-w

Close this window.

C-w-c

Make this the only window.

C-w-o

Move cursor to hjkl arrow window.

C-w-(h,j,k,l)

Combining commands

Operation + Motion

Operations:

  • d (delete)
  • y (yank)
  • c (change)

Motions:

  • w (until the start of the next word, EXCLUDING its first character)
  • e (to the end of the current word, INCLUDING the last character)
  • $ (to the end of the line, INCLUDING the last character)
  • 0 (from cursor to bol)

Delete the word vans from trucks vans cars

dw while on the v

=> trucks_(c)ars

note a space is left intact and the cursor, () ends up on the c.

Delete the word vans from trucks vans cars

de while on v

=> trucks_(_)cars

note two spaces are left this time and the cursor ends up one space left.

() = cursor, _ = space

Some possible operation+motion combinations:

dw, de, d$, d0
yw, ye, y$, y0
cw, ce, c$, c0

Quantity + Motion

motions: w, e, $, 0

quantity: 1 to n

0 (move to the start of the line)

2w (move cursor two words forward)

4b (move cursor two words back)

Move cursor to the end of the third word forward.
If at start of word, this word is included.
If at end, it will move three whole words.

3e

Quantity + Operation)

operations: dd, d

quantity: 1 to n

5dd (delete the next 5 lines)

d2w (delete two words)

Command Mode — Copy/Paste/Select

Text must be selected first for most (if not all) of these commands. V selects a line, v selects a character etc.

Cut(yank), Paste(put)

Yank to buffer current line:

yy or Y

Yank from cursor to bol, yank from cursor to eol:

y0, y$

Put (paste) after position or after line, put before position or before line:

p , P

Yanking characters or words. Enter visual mode, select text, yank then put:

v then hjkl then y then p

Yanking lines. Enter visual mode, select text, yank then put:

V then hjkl then y then p

Changing/Replacing/Joining

Changing characters or words. Enter visual mode, select text, yank then put:

c then hjkl then y then change text.

At the end of this line, join the beginning of the next line:

J

Change word. change command is a delete command that remains in insert mode:

cw

Change to end of line:

C

Change line:

cc

With cursor on f of function selects whole function block:

v$%

Inserting

Insert text from external file foo.txt:

:r foo.txt

Indenting

Indent one or more lines:

> + h,j,k,l

Un-indent one or more lines:

< + h,j,k,l

Indent n following lines, confirm with enter:

2> <enter>

Unindent n following lines, confirm with enter:

2< <enter>

Comments

Comment a block of code:

  • C-v one space to left of block
  • Use movement keys to select a vertical row
  • I
  • Enter //
  • Esc
  • Wait
  • Done

Uncomment a block of code:

  • C-v on first forward slash (comment)
  • Use movement keys go down and right (select all the //)
  • x to delete
  • Esc
  • Done

Command Mode — Deleting

Delete character to right, left.

dx , dX

Delete word at cursor.

dw

Delete the preceding word.

db

Delete from cursor to eol.

D, d$

Delete entire line.

dd , :d

Delete this number # of lines from cursor. e.g. 4dd would delete the next 4 lines:

#dd

Delete to this number # this character x, e.g. d2f. delete to 2nd .

d#fx

Delete character.

x

Delete this number of characters. e.g. 10x deletes 10 characters.

#x

Undo last action, undo changes on current line.

u , U

Redo. i.e. undo the undo’s.

C-r

Repeat last command.

.

Delete everything on line to the left of the cursor.

d0

Delete everything on line to the right of the cursor.

d$

Deletes the character under the cursor, clears current line. Both enter
insert mode after.

s, S

Text deleted with d, c or x is also copied.

Executing files and using the command line from within vim

Launch an external command.

:! [command]

Print working directory.

:pwd

List buffers.
:ls

Short Directory listing.

:! ls

Long Directory listing.

:! ls -l

Long alphabetical listing.

:! ls -la

Delete File.

:! rm newfile.txt

Exit vim Shell:

esc

Connect to mysql on Mac OS 10.

:cd /Applications/MAMP/Library

! bin/mysql --host=127.0.0.1 --port=8889 -u[user] -p[password]
! bin/mysql --host=127.0.0.1 --port=8889 -uroot -p12345

Overview: Select Text to write (output) to a file.

  1. V (enters visual mode)
  2. h,j,k,l (select text)
  3. :
  4. Vim adds ‘<,’> to command, :'<,'>
  5. w (to write file)
  6. (name of file)

v + h,j,k,l + : (+'<,'>) + w + <filename>

:!'<,'>w filename.txt

Read (input) file contents into current file:

:r filename.txt (file contents are placed below cursor line)

Read shell command output into file:

:r! echo $PATH

Read shell long listing output into file:

:r! ls -l

Read network configuration into file:

:r! ifconfig

Command Mode — Movement

h,j,k,l (left,down,up,right)

- , + (previous line, next line)

w , W (next word, next blank delimited word)

b , B (beginning of word, beginning of blank delimited word)

e , E (end of word, end of blank delimited word)

( , ) (sentence back, sentence forward)

{ , } (paragraph back, paragraph forward)

0 , $ (beginning of line, end of line)

gg (beginning of file)

G (end of file)

nG , :n (line n)

H , M , L (top, middle, bottom of screen)

C-f (move forwards one screen or page, in a file)

C-b (move backwards one screen or page, in a file)

C-d (scroll down in the file)

C-o (last cursor position)

C-i (next cursor position)

spacebar (advance the cursor one position)

f+x (search forward for a character, where x is the character)

f+x then ; (same as above but to find the next instance on the line use repeated ;)

F+x (search backwards for a character, where x is the character)

t+x (search forward for a character, where x is the character, but stop before
the character)

T+x (same above but backwards)

* (with cursor on a word, move to next word)

# (with cursor on a word, move to previous word)

% (on {}, (), [] will find next matching one)

Ranges

Ranges may precede most ‘colon’ commands and cause them to be executed on a
line or lines. For example, :3,7d would delete lines 3 — 7.

Ranges are commonly combined with the :s command to perform a replacement on
several lines, as with :.,$s/pattern/string/g to make a replacement from the
current line to the end of the file.

:n,m (lines n-m)

:. (current line)

:$ (last line)

:'c (marker c)

:% (all lines)

:g/pattern/ (all matching patterns in file)

:w <file> (write file, current file if no name given)

:w >> <file> (append file, current file if no name given)

:r <file> (read file after line)

:r !program (read program output)

:n, :p (next file, previous file)

:e (edit new file)

:.!program (replace line with program output)

~/.vimrc (location of global config file)

Command Mode — Searching

/"string" (search forward, for a word in the file, from the start)

/"string"/+n (search forward, for a word and include next n lines.
e.g. /loopers/+10)

?"string" (search for a word in the file, from the end, i.e backwards. e.g.
?cars searches for cars)

n , N (searches for next instance of the word, previous instance)

C-o (go back to last position)

C-i (go forward)

Search is interpreted as a regex, so a*b means zero or more a's followed by a b, ^abc means abc at the beginning of a line, [0-9] looks for the next digit, etc.

Vim Modes — Switching

Insert before cursor, before line.

i , I

Append after cursor, after line.

a , A

Open new line after the current line, before current line.

o , O

Replace one character, replace one word.

r , R
re (turns hen into han, h[e]n => han)

Visual mode (characters), visual mode (lines)

v , V

Change.

c

Change word.

cw

Change word from position until end of word.

ce

Exit insert mode.

esc

Help related commands for vim

Open help system

:h, :help

Close help system

:q

Jump from one window to another

C-w C-w

Place cursor on tag and jump to a subject

C-]

Jump Back

C-o

Looking for normal mode help: (no prepend)

shell

:help w
:help user-manual
:help c_ C-d
:help insert-index

Looking for visual mode help: (prepend with v_)

:help v_u

Looking for insert mode help: (prepend with i_)

:help i_<Esc>

Looking for command line help: (prepend with :)

:help :quit

Looking for command line editing help: (prepend with c_)

:help c_<Del>

Looking for vim command argument help: (prepend with -)

:help -r

Looking for option help: (enclose in »)

:help 'textwidth'

Searching for help:

:help word, then C-d to see matching entries for word

:help grep

Like this post? Please share to your friends:
  • View comment in word
  • View chart in excel
  • View buttons in excel
  • View button in excel
  • View all the comments in word