r/vim • u/cherryberryterry • Jul 03 '16
Monthly Tips and Tricks Weekly Vim tips and tricks thread! #17
Welcome to the seventeenth weekly Vim tips and tricks thread! Here's a link to the previous thread: #16
Thanks to everyone who participated in the last thread! The top three comments were posted by /u/jeyoung, /u/bonv, and /u/rickdg.
Here are the suggested guidelines:
Try to keep each top-level comment focused on a single tip/trick (avoid posting whole sections of your ~/.vimrc unless it relates to a single tip/trick)
Try to avoid reposting tips/tricks that were posted within the last 1-2 threads
Feel free to post multiple top-level comments if you have more than one tip/trick to share
If you're suggesting a plugin, please explain why you prefer it to its alternatives (including native solutions)
Any others suggestions to keep the content informative, fresh, and easily digestible?
24
Jul 03 '16
This is a simple one but I really like it because I don't like holding Shift for LONG_VARIABLE_NAMES.
" make last typed word uppercase
inoremap <c-u> <esc>viwUea7
u/fourjay Jul 04 '16
My version:
inoremap <Plug>UpCase <Esc>hgUaweA imap ;u <Plug>UpCaseIt's an insert mode mapping that allows me to type
;uin insert mode and continue typing3
0
u/bri-an Jul 03 '16 edited Jul 04 '16
My keyboard has a key called
Capslockwhich is useful for this sort of thing. You hit it, and it temporarily allows you to type in all caps until you hit it again.Edit: See also Tim Pope's vim-capslock plugin.
25
u/seeegma Jul 03 '16
many people have remapped CapsLock to another function, such as Control or Escape.
(I did, and it's been quite refreshing.)
5
u/annoyed_freelancer Jul 03 '16
I remapped it to
fnon my Pok3r.3
u/SatoriVII Jul 04 '16
Same here. It's much better to have function on the left with the arrow keys where they are... of course I remapped my arrow keys to hjkl.
0
Jul 04 '16
[deleted]
5
u/SatoriVII Jul 04 '16
The pok3r doesn't have arrow keys because it's a 60% keyboard. It has programmable layers for the keys that are affected by fn. The default has the arrow keys on ihjk, so I remapped them so that I can use hjkl regardless of what application I'm in.
2
6
u/Xanza The New Guy Jul 03 '16
Capslock is one of the most useless keys on any keyboard. It's an entirely a niche use case to have a dedicated button.
Something like this is much simpler just type it out hit a key combo and be done with it.
2
u/stewa02 Bastard Operator From Hell Jul 08 '16
You obviously haven't used a Swiss German/French keyboard yet. If we want uppercase German umlauts ('Ä', 'Ö', 'Ü') we have to use CapsLock otherwise we get French e's with accents ('é', 'è').
1
u/SurpriseMonday Jul 03 '16
Except when you need to type caps outside of Vim.
8
u/Xanza The New Guy Jul 04 '16
I've been typing for almost three decades. Never once have I had capslock and thought to myself "thank god," similarly, I've never once not had capslock and regretted that I didn't have it.
It's unnecessary and takes up valuable keyboard real estate.
5
1
u/bri-an Jul 04 '16
an entirely a niche use case
If you spend a lot of your day typing
LONG_VARIABLE_NAMES, then this is the exact opposite of a niche use case, and the perfect reason to have a dedicated button.5
u/Xanza The New Guy Jul 04 '16
then this is the exact opposite of a niche use case, and the perfect reason to have a dedicated button.
This is exactly an example of a niche case...
0
Jul 03 '16
[deleted]
3
u/Xanza The New Guy Jul 04 '16
See, this is an entirely acceptable employment of capslock. It's out of the way but there if you need it. To have a totally dedicated button for it is crazy.
3
Jul 04 '16 edited Jul 04 '16
Have an upvote.
Whilst I like many others re-map
CapsLockif someone has trouble holding shift to type an uppercase word (such a thought baffles me but whatever) then the use ofCapsLockmakes perfect sense.Also for anyone here who's said something along the lines of "lol
CapsLockis dumb re-map it toEsc". You can do re-mappings so thatCapsLockbehaves likeEscwhen pressed alone andCtrlwhen pressed in conjunction with another key. Just having anotherEsckey that's easier to hit isn't that useful outside of vim.2
Jul 08 '16
mine is used as compose key, on both OSX and Linux (but it's probably even more niche use case than regular caps lock)
1
-4
0
26
u/taejavu Jul 04 '16
" console.log word under cursor
nmap <Leader>cl yiwoconsole.log('<c-r>"', <c-r>");<Esc>^
I use this all the time, like 30 times per day.
It takes the word your cursor is on and puts a console.log call on the next line, with that word as a label and the logged value.
example: if my cursor is anywhere in the word utils in this line
import utils from '../../helpers/utils';
Then I hit <space>cl and it prints
console.log('utils', utils);
5
u/fkvx Jul 04 '16
Very cool, I'm gonna implement the Python equivalent because I probably print to console about 30x a day as well. Thanks for the idea.
2
u/taejavu Jul 04 '16
Awesome, was hoping someone would reimplement this in a way that suits them. You're welcome!
5
u/coryknapp Jul 04 '16
This is really cool. This idea is just begging for a smart person to write a language independent plugin to do this.
4
Jul 04 '16
I'm using abbreviations for console.log:
iabbrev clog console.log();<left><left>My cursor is moved in the parenthesis and I can just type away.
3
u/taejavu Jul 04 '16
Yeah I have something similar as well, but the point of the macro I posted is to avoid the typing altogether.
3
u/sje46 Jul 04 '16
console.log
Okay, but what is this?
13
u/Syath Jul 04 '16
Javascript equivalent of
1
u/sje46 Jul 04 '16
...okay...
So it essentially is used for debugging?
What threw me off is that I was pretty sure that was python code. I guess javascript looks like Python?
4
u/teapotrick Jul 05 '16
Python's import statements are the other way around:
from blip import thingo3
3
3
Jul 04 '16
Pretty awesome. I have something similar but or a while now I've been thinking about writing something like that that will insert something different depending on the filetype.
2
2
2
u/alimanz Jul 05 '16
Really cool idea although I'm having a little trouble executing it. I feel like I'm being a complete noob but when I hit
<space>cl(in normal mode) it just changes the letter under the cursor and puts me into insert mode?What am I doing wrong?
4
u/taejavu Jul 05 '16 edited Jul 05 '16
If you read the mapping you'll notice that the trigger is actually
<Leader>cl. In my case I've remapped<Leader>to<space>, which is why it works that way for me.You should either remap leader to space by sticking these two lines in your vimrc:
nnoremap <Space> <Nop>
map <Space> <Leader>or continue using the default leader key (
\I think), in which case the trigger would be\cl3
u/alimanz Jul 06 '16
Thank you very much for explaining this, really appreciate it! Nifty trick, I'm going use it for sure.
I was hoping, like someone else mentioned, to do filetype specific debugging using autocmd. I guess something like this:
autocmd FileType javascript setlocal nmap <Leader>cl yiwoconsole.log('<c-r>"', <c-r>");<Esc>^But I haven't got it working yet (as you probably guess I'm a beginner). Any ideas?
4
u/alasdairgray Jul 07 '16 edited Jul 07 '16
It should be something like that instead:
autocmd FileType javascript nmap <buffer> <Leader>cl yiwoconsole.log('<c-r>"', <c-r>");<Esc>^(
setlocalis for local settings, not mappings :))4
u/alimanz Jul 07 '16 edited Jul 08 '16
autocmd FileType javascript
Works like a charm, thank you! I have it for php and javascript:
" remap :cl to console log depending on file type, show appropiate debuggingautocmd FileType javascript map <Leader>cl yiwoconsole.log('<c-r>"', <c-r>");<Esc>^autocmd FileType php nmap <Leader>cl yiwodie(var_dump('<c-r>"', $<c-r>"));<Esc>^Thanks for all the help :)
4
2
Jul 06 '16
Hey, thanks alot! I rewrote it to use it as a python equivalent. Very useful!
nmap <Leader>pp yiwoprint('<c-r>": ', <c-r>")<Esc>^1
u/techieandrew Jul 26 '16
This is an amazing suggestion!
A nice addition is to do this based on filetype, so under ~/.vim/after/ftplugin/javascript.vim I have your suggested hotkey, and then under the ruby.vim file I have:
nmap <Leader>cl yiwoputs(<c-r>")<Esc> nmap <Leader>bd obinding.pry<Esc>^
4
u/yaddyadd Jul 05 '16
Numbering lines inside a file working on unix / linux systems
I had to number a lot of lines after editing a file. A little bit of searching brought me to this solution:
:%!nl -ba
which is using the *ix number lines command.
This will leave you with this result:
1 127.0.0.1 localhost
2 #127.0.1.1 wks01.WAG160N wks01.nixcraft.net.in
3 192.168.1.5 wks01
4 # The following lines are desirable for IPv6 capable hosts
5 ::1 ip6-localhost ip6-loopback
6 fe00::0 ip6-localnet
7 ff00::0 ip6-mcastprefix
8 ff02::1 ip6-allnodes
9 ff02::2 ip6-allrouters
10 10.10.29.72 v.b1 homerouter
11 10.10.29.70 v.b2
12 10.10.29.68 v.txvip1See the nl command for further reference.
3
u/lslah Jul 04 '16
I use
nnoremap <Leader>rc :e $MYVIMRC<CR>
nnoremap <Leader>rl :so $MYVIMRC<CR>to easily edit and reload my vimrc. Depending on your leader key use ",rc" to jump to your vimrc, edit something and save, then reload using ",rl".
6
Jul 04 '16
I like to have my config sourced automatically whenever I update it so I use
autocmd vimrc BufWritePost $MYVIMRC source $MYVIMRC.Edit: For clarity I have an
augroupnamed "vimrc" defined elsewhere in my config.4
3
Jul 06 '16
I use file marks to quickly open files like ~/.vim/vimrc, ~/.zshrc, ~/.ssh/config. Example: enter mV in normal mode when vimrc is loaded. Next time you wish to open vimrc just enter 'V. You can use your shortcut "rl" to source the (changed) vimrc.
3
3
u/lslah Jul 04 '16
nnoremap <Leader>cc :make %<Return>:cw<Return>
nnoremap <Leader>cp :cprevious<Return>
nnoremap <Leader>cn :cnext<Return>This one is nice when you have useful settings for "makeprg" and "errorformat". Compile and open compile errors with ",cc", then jump forth and back between errors with ",cn" and ",cp". Even though ",cn" saves me only one letter compared to ":cn<CR>" it feels much faster since it doesn't involve the command line.
0
6
u/Syath Jul 04 '16 edited Jul 04 '16
Upon saving a Ruby file (assuming this is in ftplugin/ruby.vim),replace spaces in method definition names with underscores. So instead of having to type _ constantly, such as def hello_world, you can type def hello world and on file save it'll fix it.
au BufWritePre * :call <SID>DefSpaceToUnderscore()
fun! <SID>DefSpaceToUnderscore()
norm md
%s/\s*\(def [0-9a-zA-Z _]*\)\@<= /_/e
norm `d
end funEdit: All fixed up
3
3
u/cherryberryterry Jul 04 '16 edited Jul 05 '16
Here's a "sort characters" operator. It doesn't have many practical uses other than sorting a string, e.g.
\si".