1

I'm really new to vim and I was searching for moving my single word or a line to move 'up' 'down' 'left' 'right' in insert mode I was able to achieve the up a line or block in visual mode and down a line or block in visual mode but now, I want the left and right motion like the vim plugin matze/vim-move but I don't want to use any plugins and instead use builtin function (If any that do the same) for moving up a line or down. I'm using

inoremap <silent> <A-J> <ESC>:m .+1<CR>==i
inoremap <silent> <A-K> <ESC>:m .-2<CR>==i

If there is anyway to do the same for left or right....??

2

1 Answer 1

3

You can use the following, somewhat hacky, maps to do this (inspired by the unimpaired plugin):

" move a small word left
imap ]w <ESC>mx$ox<ESC>kJ`xdawhelphmx$"_daw`xa
" move a big WORD left
imap ]W <ESC>mx$ox<ESC>kJ`xdaWElphmx$"_daw`xa
" move a small word right
imap [w <ESC>mx$ox<ESC>kJ`xdawbPhmx$"_daw`xa
" move a big WORD right
imap [W <ESC>mx$ox<ESC>kJ`xdaWBPhmx$"_daw`xa

In the above, 'big WORD' means (:help WORD):

WORD consists of a sequence of non-blank characters, separated with white space.

Advice

Since you say you're very new to vim (welcome!), I'll point out that normal mode is the place to do most 'editing' tasks like swapping words, lines and the like, while insert mode is predominantly for inserting text (hence the built in editing shortcuts are more limited). Often, newcomers feel more comfortable with insert mode since it has a more familiar 'feel' to editing with other editors. So my general advice is to embrace normal mode as the normal mode to be in while editing.

7
  • 5
    Wholeheartedly agree with the "advice" here. Embrace The Vim Way ™ or you'll never get as much out of Vim as it has to offer. :) Have you gone through vimtutor?
    – B Layer
    Jan 4, 2022 at 10:14
  • 1
    Excellent Advice!
    – filbranden
    Jan 4, 2022 at 18:44
  • I completely agree to the fact that normal mode is where the editing part is but it is fun to not do the manual labor of going to normal mode "edit" then back to insert mode, I have lots of normal mode mappings but at times I just want to edit some small bits on the fly while initial scribbles Jan 6, 2022 at 17:41
  • @JayChotaliya Yep - I'm not criticising your approach per se - insert mode maps aren't anti-vim of course :)
    – mattb
    Jan 6, 2022 at 17:49
  • @mattb Is there any way I can edit the github.com/matze/vim-move is plugin to do something like what I want but in insert mode or even in normal mode all I want to do is auto select the word by using visual selection and move that particular left or right if its possible it would be of great help Jan 6, 2022 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.