3

Often when configuring and adjusting unix-based systems I use a lot of one-line commands that are difficult to remember. For example, to determine what version of ntp (the time server) is running, you issue the command:

ntpq -c "rv 0 version"

This is the kind of stuff I am never going to remember. Is there some way or method to store all these one-liners so I can look them easily when I need to and paste them to the command line (I use vim btw, not emacs, so an emacs-centered solution will not really help me). I was thinking there might be a way to execute the current line of text from the buffer in the shell, but could not find any such command.

One idea I have had is to put the commands in a text file then in vimrc make this line:

nnoremap <F10> :.w !bash<CR>

Now, lets say I have this line in my file:

ntpq -c "rv 0 version"    # show current version of NTP which is running

Then, I put the cursor on this line and press F10 and it works (note that since the command is being sent to bash, you use bash comments (#), not vim comments). This seems like a step in the right direction. Any better ideas?

  • You could make your history very large and then execute stuff from tehere. And there's commandlinefu.com... And you could note them in a plain text file you keep around. I run a private habari instance where I put this kind of stuff (and more documentation)... – Jan Sep 12 '14 at 19:59
5

Have you tried to use aliases?

example

Create an alias for the new command:

alias ntpversion='ntpq -c "rv 0 version"'

Run the command:

ntpversion

Running ntpversion, after it is set as an alias, will provide the output for your one-liner.

http://www.linuxhowtos.org/Tips%20and%20Tricks/command_aliases.htm

  • +1 from me because you edited it as HalosGhost suggested. – John1024 Sep 12 '14 at 20:36
3

Running commands from vim

You asked about vim. In vim, you can create a file with your shortcuts. It would have lines such as:

!ntpq -c "rv 0 version"

You can then copy that line to the vim command line (:) to execute it. Because the line starts with !, vim will give it the the shell to execute when you press enter. You never have to leave vim.

With my vim setup, I copy the line into the X11 buffer with mouse clicks, then type : to open the command line and shift-insert to paste in the line. Vim is very configurable and your setup may be different. If so, have a look at this post for other ways to paste to the command line.

Saving commands in ~/.bashrc

Alternatively, you add bash aliases or functions to your ~/.bashrc file. A typical alias looks like:

alias ll="ls --color -l"

Just place the above line in your ~/.bashrc file (or another file that is sourced from there) and you will have them all in one convenient place when you want to review them.

Aliases are simple and handy but limited. If you want more features, such as process arguments, performing arithmetic, etc., you may want to read about bash functions. They can also be stored in ~/.bashrc .

  • Is there a way to put the cursor on a line of text and tell vim to execute that line in the shell? – Tyler Durden Sep 12 '14 at 20:31
  • @TylerDurden Above, I was referring to the vim command line (:) and, if the command begins with !, vim will execute in your default unix shell for you. I edited the answer to try to clarify that. – John1024 Sep 12 '14 at 20:34
2

You could make your history very large and then execute stuff from there.
And there's commandlinefu.com...
You could note all those one liners in a plain text file you keep around.

I run a private habari instance where I put this kind of stuff (and more documentation)...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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