Tuesday, September 30, 2008

SSH Remote Connections via Config

Thorsten has contributed the following ssh tip. Thanks Thorsten!

You wrote about ssh connects via gnome terminal:

http://dailyvim.blogspot.com/2008/09/gnome-terminal-tips.html

I'm using the config file of ssh to achieve the same goal:

just put the file "config" in your .ssh directory (chmod 600) with the
following lines:


host mysshhost
user root
identityfile /root/.ssh/my_ssh_key.ssh
port 34021
hostname 127.0.0.1
localforward 10100 localhost:10100
ForwardX11 yes


then back at the command line just try:

myhost:/ # ssh mysshhost

and voila, welcome to your ssh connected system (even the
bash-completion works with the config file, just do a ssh )

Monday, September 29, 2008

Question: Browsing Remote Filesystems

A Daily Vim reader asks the following question:

Travis, the feature that has me married to Kate (KDE text editor) is the file browser with bookmarks built in. Kate allows users to browse remote filesystems from within the app just like they could browse the local filesystem in a file manager. Furthermore, there is a bookmarks feature where users can bookmark specific directories / files on remote servers. Anythink like those two features in VIM?

Thanks!

Anybody have any ideas on this? I have a few ideas, but I'll wait and see if anybody else has a suggestions first.

Thursday, September 25, 2008

Whatprovides

If you're using a RedHat derivative that utilizes yum as it's package manager, you can use the `whatprovides' command to see what package provides a specific file.

Example:

yum whatprovides /usr/bin/vim

... grab a coffee ...

vim-enhanced.x86_64 1:6.3.046-0.40E.7.cent base
Matched from:
/usr/bin/vim

Debian derivatives provide a similar functionality via the apt-file package.

# sudo apt-get install apt-file
# sudo apt-file update
$ apt-file search /usr/bin/diff
diff: /usr/bin/diff

Friday, September 19, 2008

Unhighlight Current Search

If you want to un-highlight the currently highlighted search terms, issue the following command.

:noh

Friday, September 12, 2008

Search Register

You can manually push a value into the search register using the following syntax.

let @/='Some String'

This is particularly nice for script hacking when you want to highlight a group of strings in the current buffer.

Vimgrep Without Object Names

My friend Chris (Vim user extraordinaire) has contributed the following tip. It looks like it could be quite useful to any Java hackers in the crowd.

I constantly want to use :vimgrep to find usages/references to certain object types in my java code. However, when I :vimgrep for the object name, it (naturally) matches all of the 'import' statements, too. I've known for awhile that there was an idiom in the regex syntax to negate a previous atom (in this case, the 'import' at the beginning of the line) but I've never found a way to phrase it that works. Here it is:

.*ObjectName\&^\(import\)\@!

This matches any line meeting the following two constraints:
1) contains anything (.*) followed by ObjectName
2) starts (^) with something which isn't 'import' (\(import\)\@!)

the \@! says, match if the preceding atom does not match here -- since we put ^\(import\) this means match when the first thing in the line isn't import.

There may be better ways to do this, but it's the first I've found and I'm using it like crazy now... Enjoy!

Thanks Chris!

Question: Highlight From Insert?

Iyo writes:

"I offten do some coding in Vim and I miss an feature that will highlight
the word I`m writing in that moment and other same words. (It is mainly
for variables - sometimes I switched letters and afterwards I`m looking
for mistake for a quite long time. The NetBeans IDE has a feature like this.

I know about * but it`s for search in the text. I`m looking for something that work automaticly in insert mode."



Answer:

You can use ctrl-o from inside of insert mode to enter a command, but to me typing ctrl-o * while I'm hammering out some code feels awfully cumbersome. There's also the negative side-effect that * jumps forward, so you'd lose cursor position, which would assuredly interrupt workflow. Instead, I'd just define a simple mapping along the lines of the following.

imap <F2> <Esc>#*A

So, you're inside insert mode, you just typed a word, and you want to match all occurrences. You would simply hit F2, and it would backward match the word nearest the cursor. This has the negative side effect of jumping backwards; thereby, losing cursor position. To compensate, we add a * to jump forward followed by "A" to return to insert mode and insure you're at the end of the line. Obviously, you can substitute any keybinding you want for F2.

This seems to work well on my machine; although, there might be more optimal solutions. If anybody has one, feel free to share.

Thursday, September 11, 2008

Got a Question?

I had a reader of Daily Vim contact me yesterday about the possibility of periodically submitting questions regarding Vim best practices, etc. I told him that I'm very happy to assist in answering questions any Daily Vim readers might have but if I don't know the answer, I'll post the question here anyway, so someone smarter than me can ;-).

So, if you have a question regarding Vim, Linux, scripting or something along those lines, feel free to post them under this thread or email me directly via tinymountain at gmail dot com. I'll post the question and possibly an answer to the blog, and as usual, feedback is welcome.

To kick things off, I have a question of my own. Does anybody know of a Vim plugin or some method to spontaneously highlight every other line of a file? I frequently use Vim as a pager inside of MySQL, and this would be very handy for eyeballing all the values on a specific row.

UPDATE:

I solved this problem thanks in part to an initial suggestion to use syntax highlighting to perform the highlighting. Here's my finished solution. It seems to have issues with language files with pre-existing syntax highlighting, but for it's intended purpose (use within the mysql pager), it works beautifully. Just do "pager /usr/bin/vim -R -" inside the mysql client, add the code to your vimrc, run a query and hit F2.


hi AlternateLine ctermfg=grey ctermbg=darkblue

function! HiLine(lineno)
let tmpline = escape(getline(a:lineno), '/\[]')
exec 'syn match AlternateLine /.*' . tmpline . '.*/'
endfunction

function! DoHighlight()
global /^/if line('.')%2|call HiLine(line('.'))
normal 1G
endfunction

map <F2> :call DoHighlight()<cr>


Jump to Percent of File

Sometimes you don't know the exact line you want to go to in a file, but you can infer an approximate percentage. I was browsing a 200 meg logfile with Vim yesterday and guestimated the area of interest was about 10% back from where I was at (85%). You can easily jump to a desired percentage in a file by typing in the percentage followed by a percent symbol.

Jump to 50% of the file:

50%

Easy to remember to say the least ;-)

Wednesday, September 10, 2008

Scrollbind

You can :set scrollbind in any windows in Vim that you would like to scroll together.

Thanks to Chris for the tip.

Monday, September 8, 2008

Determining Linux Version

Linux distributions don't tend to standardize where they store information about what release is installed. Going further, there's not even a standard way to detect what distribution is installed. If a system is running a popular distro, this information can usually be found in one of the following files:


Debian /etc/debian_release, /etc/debian_version,
Fedora /etc/fedora-release
Gentoo /etc/gentoo-release
Mandrake /etc/mandrake-release
Novell SUSE /etc/SUSE-release
Red Hat /etc/redhat-release, /etc/redhat_version
Slackware /etc/slackware-release, /etc/slackware-version
Solaris/Sparc /etc/release
Sun JDS /etc/sun-release
Ubuntu /etc/lsb-release
UnitedLinux /etc/UnitedLinux-release
Yellow dog /etc/yellowdog-release


You can find more information on this problem as well as a shell script to assist with OS detection here. An even larger master list can be found here.

Ubuntu Audio Sharing

For a good while, audio sharing issues plauged my Ubuntu workstation. Whenever I'd hit a website that used Flash 9, none of my other audio applications would be able to access the audio device until my Gnome session was restarted. Googl'ing around for Ubuntu and Flash, I stumbled onto this package, which I'm happy to say solved the issue.

Ubuntu libflashsupport.

Obviously, you can install it quite easily with apt.

sudo apt-get install libflashsupport

Hope this helps somebody.

Friday, September 5, 2008

Repeat Last Command Line

To repeat the last command-line given in ex mode, you can simply enter a count followed by "@:".

Swap Characters

To swap two adjacent characters in Vim you can issue an "xp" command in normal mode.

Gnome Terminal Tips

I often use the profiles feature in gnome terminal to organize all my remote shell accounts. Inside of gnome terminal, you can go to Edit->Profiles to manage your profiles. From there, you can either edit an existing profile or create a new one. Either action will take you to the "Editing Profile" window. Once inside, click on "Title and Command" tab and check the box that says "Run a custom command instead of my shell". You can put your ssh login info inside of the "Custom command" box. An example would be:

ssh -i /home/username/.ssh/id_rsa.1 -p 7822 myuser@myhost.com

That would invoke an ssh session using port 7822 and the identity file stored in your .ssh directory. It's particularly useful to setup sessions with shared keys so you can just go File->Open Terminal and point to the desired session. If you have certain ssh session you use a lot, it might be worth adding a launcher to your panel and specifying the desired profile like so:

gnome-terminal --window-with-profile SomeProfile

It's also worth noting that gnome terminal allows you to color code your saved profiles. I find that this helps prevent stupid mistakes caused by thinking your logged into a different session than you actually are.

Since I use gnome terminal to store many remote login sessions, I sometimes need to retrieve this information whilst remotely logged into my workstation. An example would be when I ssh to my work PC from home over a VPN connection. Gnome terminal stores all of it's saved profile information in the following location.

/home/$username/.gconf/apps/gnome-terminal/profiles/$profilename/%gconf.xml

In this case, $username and $profilename would represent your username and desired gnome profile name respectively. Inside of the %gconf.xml file, there's a section for custom command that looks like this.

<entry name="custom_command" mtime="1193759925" type="string">
    <stringvalue>ssh -p 7822 username@somehost.org</stringvalue>
</entry>

From there, you should be able to extract whatever info is desired.

Bonus tip: you can open a new terminal window whilst inside an active gnome terminal session by doing ctrl-shift-N.